Skip to content

Commit

Permalink
Merge pull request #4064 from pymedusa/develop
Browse files Browse the repository at this point in the history
Merge develop for release 0.2.2
  • Loading branch information
p0psicles authored Apr 21, 2018
2 parents d6eb72d + 46e31bc commit 77b2091
Show file tree
Hide file tree
Showing 1,920 changed files with 100,383 additions and 44,239 deletions.
6 changes: 3 additions & 3 deletions .codebeatignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
static/js/vender.js
static/js/lib/*

ext/**
lib/**

themes/**
themes-default/**
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ medusa.egg-info
/tests/cache.db*
/tests/failed.db
/tests/data/
.pytest_cache

# Compiled source #
######################
Expand Down
2 changes: 2 additions & 0 deletions SickBeard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*
"""Script for backwards compatibility."""
from __future__ import unicode_literals

from medusa.__main__ import main

if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions ext/_dummy_thread/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import absolute_import
import sys
__future_module__ = True

if sys.version_info[0] < 3:
from dummy_thread import *
else:
raise ImportError('This package should not be accessible on Python 3. '
'Either you are trying to run from the python-future src folder '
'or your installation of python-future is corrupted.')
10 changes: 10 additions & 0 deletions ext/_markupbase/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import absolute_import
import sys
__future_module__ = True

if sys.version_info[0] < 3:
from markupbase import *
else:
raise ImportError('This package should not be accessible on Python 3. '
'Either you are trying to run from the python-future src folder '
'or your installation of python-future is corrupted.')
10 changes: 10 additions & 0 deletions ext/_thread/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import absolute_import
import sys
__future_module__ = True

if sys.version_info[0] < 3:
from thread import *
else:
raise ImportError('This package should not be accessible on Python 3. '
'Either you are trying to run from the python-future src folder '
'or your installation of python-future is corrupted.')
12 changes: 12 additions & 0 deletions ext/builtins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import absolute_import
import sys
__future_module__ = True

if sys.version_info[0] < 3:
from __builtin__ import *
# Overwrite any old definitions with the equivalent future.builtins ones:
from future.builtins import *
else:
raise ImportError('This package should not be accessible on Python 3. '
'Either you are trying to run from the python-future src folder '
'or your installation of python-future is corrupted.')
2 changes: 1 addition & 1 deletion ext/cachecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
__author__ = 'Eric Larson'
__email__ = '[email protected]'
__version__ = '0.12.3'
__version__ = '0.12.4'

from .wrapper import CacheControl
from .adapter import CacheControlAdapter
Expand Down
6 changes: 5 additions & 1 deletion ext/cachecontrol/adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import types
import functools
import zlib

from requests.adapters import HTTPAdapter

Expand Down Expand Up @@ -37,7 +38,10 @@ def send(self, request, cacheable_methods=None, **kw):
"""
cacheable = cacheable_methods or self.cacheable_methods
if request.method in cacheable:
cached_response = self.controller.cached_request(request)
try:
cached_response = self.controller.cached_request(request)
except zlib.error:
cached_response = None
if cached_response:
return self.build_response(request, cached_response,
from_cache=True)
Expand Down
8 changes: 5 additions & 3 deletions ext/cachecontrol/caches/redis_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import division

from datetime import datetime
from cachecontrol.cache import BaseCache


def total_seconds(td):
Expand All @@ -13,7 +14,7 @@ def total_seconds(td):
return int((ms + secs * 10**6) / 10**6)


class RedisCache(object):
class RedisCache(BaseCache):

def __init__(self, conn):
self.conn = conn
Expand All @@ -25,7 +26,7 @@ def set(self, key, value, expires=None):
if not expires:
self.conn.set(key, value)
else:
expires = expires - datetime.now()
expires = expires - datetime.utcnow()
self.conn.setex(key, total_seconds(expires), value)

def delete(self, key):
Expand All @@ -38,4 +39,5 @@ def clear(self):
self.conn.delete(key)

def close(self):
self.conn.disconnect()
"""Redis uses connection pooling, no need to close the connection."""
pass
97 changes: 57 additions & 40 deletions ext/cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,51 @@ def cache_url(cls, uri):
return cls._urlnorm(uri)

def parse_cache_control(self, headers):
"""
Parse the cache control headers returning a dictionary with values
for the different directives.
"""
known_directives = {
# https://tools.ietf.org/html/rfc7234#section-5.2
'max-age': (int, True,),
'max-stale': (int, False,),
'min-fresh': (int, True,),
'no-cache': (None, False,),
'no-store': (None, False,),
'no-transform': (None, False,),
'only-if-cached' : (None, False,),
'must-revalidate': (None, False,),
'public': (None, False,),
'private': (None, False,),
'proxy-revalidate': (None, False,),
's-maxage': (int, True,)
}

cc_headers = headers.get('cache-control',
headers.get('Cache-Control', ''))

retval = {}

cc_header = 'cache-control'
if 'Cache-Control' in headers:
cc_header = 'Cache-Control'

if cc_header in headers:
parts = headers[cc_header].split(',')
parts_with_args = [
tuple([x.strip().lower() for x in part.split("=", 1)])
for part in parts if -1 != part.find("=")
]
parts_wo_args = [
(name.strip().lower(), 1)
for name in parts if -1 == name.find("=")
]
retval = dict(parts_with_args + parts_wo_args)
for cc_directive in cc_headers.split(','):
parts = cc_directive.split('=', 1)
directive = parts[0].strip()

try:
typ, required = known_directives[directive]
except KeyError:
logger.debug('Ignoring unknown cache-control directive: %s',
directive)
continue

if not typ or not required:
retval[directive] = None
if typ:
try:
retval[directive] = typ(parts[1].strip())
except IndexError:
if required:
logger.debug('Missing value for cache-control '
'directive: %s', directive)
except ValueError:
logger.debug('Invalid value for cache-control directive '
'%s, must be %s', directive, typ.__name__)

return retval

def cached_request(self, request):
Expand Down Expand Up @@ -156,8 +180,8 @@ def cached_request(self, request):
freshness_lifetime = 0

# Check the max-age pragma in the cache control header
if 'max-age' in resp_cc and resp_cc['max-age'].isdigit():
freshness_lifetime = int(resp_cc['max-age'])
if 'max-age' in resp_cc:
freshness_lifetime = resp_cc['max-age']
logger.debug('Freshness lifetime from max-age: %i',
freshness_lifetime)

Expand All @@ -173,18 +197,12 @@ def cached_request(self, request):
# Determine if we are setting freshness limit in the
# request. Note, this overrides what was in the response.
if 'max-age' in cc:
try:
freshness_lifetime = int(cc['max-age'])
logger.debug('Freshness lifetime from request max-age: %i',
freshness_lifetime)
except ValueError:
freshness_lifetime = 0
freshness_lifetime = cc['max-age']
logger.debug('Freshness lifetime from request max-age: %i',
freshness_lifetime)

if 'min-fresh' in cc:
try:
min_fresh = int(cc['min-fresh'])
except ValueError:
min_fresh = 0
min_fresh = cc['min-fresh']
# adjust our current age by our min fresh
current_age += min_fresh
logger.debug('Adjusted current age from min-fresh: %i',
Expand Down Expand Up @@ -260,10 +278,10 @@ def cache_response(self, request, response, body=None,

# Delete it from the cache if we happen to have it stored there
no_store = False
if cc.get('no-store'):
if 'no-store' in cc:
no_store = True
logger.debug('Response header has "no-store"')
if cc_req.get('no-store'):
if 'no-store' in cc_req:
no_store = True
logger.debug('Request header has "no-store"')
if no_store and self.cache.get(cache_url):
Expand Down Expand Up @@ -292,13 +310,12 @@ def cache_response(self, request, response, body=None,
# the cache.
elif 'date' in response_headers:
# cache when there is a max-age > 0
if cc and cc.get('max-age'):
if cc['max-age'].isdigit() and int(cc['max-age']) > 0:
logger.debug('Caching b/c date exists and max-age > 0')
self.cache.set(
cache_url,
self.serializer.dumps(request, response, body=body),
)
if 'max-age' in cc and cc['max-age'] > 0:
logger.debug('Caching b/c date exists and max-age > 0')
self.cache.set(
cache_url,
self.serializer.dumps(request, response, body=body),
)

# If the request can expire, it means we should cache it
# in the meantime.
Expand Down
2 changes: 1 addition & 1 deletion ext/cachecontrol/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def expire_after(delta, date=None):
date = date or datetime.now()
date = date or datetime.utcnow()
return date + delta


Expand Down
Loading

0 comments on commit 77b2091

Please sign in to comment.