##// END OF EJS Templates
python3: fixed some compat problems
super-admin -
r4916:86c3a981 default
parent child Browse files
Show More
@@ -23,20 +23,20 b' import errno'
23 import logging
23 import logging
24
24
25 import msgpack
25 import msgpack
26 import redis
26 import gevent
27 import gevent
27 import redis
28
28
29 from dogpile.cache.api import CachedValue
29 from dogpile.cache.api import CachedValue
30 from dogpile.cache.backends import memory as memory_backend
30 from dogpile.cache.backends import memory as memory_backend
31 from dogpile.cache.backends import file as file_backend
31 from dogpile.cache.backends import file as file_backend
32 from dogpile.cache.backends import redis as redis_backend
32 from dogpile.cache.backends import redis as redis_backend
33 from dogpile.cache.backends.file import NO_VALUE, compat, FileLock
33 from dogpile.cache.backends.file import NO_VALUE, FileLock
34 from dogpile.cache.util import memoized_property
34 from dogpile.cache.util import memoized_property
35
35
36 from pyramid.settings import asbool
36 from pyramid.settings import asbool
37
37
38 from rhodecode.lib.memory_lru_dict import LRUDict, LRUDictDebug
38 from rhodecode.lib.memory_lru_dict import LRUDict, LRUDictDebug
39 from rhodecode.lib.utils import safe_str, safe_unicode
39 from rhodecode.lib.utils import safe_str
40
40
41
41
42 _default_max_size = 1024
42 _default_max_size = 1024
@@ -74,7 +74,7 b' class PickleSerializer(object):'
74
74
75 def _dumps(self, value, safe=False):
75 def _dumps(self, value, safe=False):
76 try:
76 try:
77 return compat.pickle.dumps(value)
77 return pickle.dumps(value)
78 except Exception:
78 except Exception:
79 if safe:
79 if safe:
80 return NO_VALUE
80 return NO_VALUE
@@ -83,7 +83,7 b' class PickleSerializer(object):'
83
83
84 def _loads(self, value, safe=True):
84 def _loads(self, value, safe=True):
85 try:
85 try:
86 return compat.pickle.loads(value)
86 return pickle.loads(value)
87 except Exception:
87 except Exception:
88 if safe:
88 if safe:
89 return NO_VALUE
89 return NO_VALUE
@@ -187,7 +187,7 b' class FileNamespaceBackend(PickleSeriali'
187
187
188 with self._dbm_file(True) as dbm:
188 with self._dbm_file(True) as dbm:
189 try:
189 try:
190 return filter(cond, dbm.keys())
190 return list(filter(cond, list(dbm.keys())))
191 except Exception:
191 except Exception:
192 log.error('Failed to fetch DBM keys from DB: %s', self.get_store())
192 log.error('Failed to fetch DBM keys from DB: %s', self.get_store())
193 raise
193 raise
@@ -300,7 +300,7 b' class BaseRedisBackend(redis_backend.Red'
300
300
301 def get_mutex(self, key):
301 def get_mutex(self, key):
302 if self.distributed_lock:
302 if self.distributed_lock:
303 lock_key = u'_lock_{0}'.format(safe_unicode(key))
303 lock_key = '_lock_{0}'.format(safe_str(key))
304 return get_mutex_lock(self.client, lock_key, self._lock_timeout,
304 return get_mutex_lock(self.client, lock_key, self._lock_timeout,
305 auto_renewal=self._lock_auto_renewal)
305 auto_renewal=self._lock_auto_renewal)
306 else:
306 else:
@@ -21,13 +21,13 b' import os'
21 import time
21 import time
22 import logging
22 import logging
23 import functools
23 import functools
24 import decorator
24 import threading
25 import threading
25
26
26 from dogpile.cache import CacheRegion
27 from dogpile.cache import CacheRegion
27 from dogpile.cache.util import compat
28
28
29 import rhodecode
29 import rhodecode
30 from rhodecode.lib.utils import safe_str, sha1
30 from rhodecode.lib.utils import safe_bytes, sha1
31 from rhodecode.lib.utils2 import safe_unicode, str2bool
31 from rhodecode.lib.utils2 import safe_unicode, str2bool
32 from rhodecode.model.db import Session, CacheKey, IntegrityError
32 from rhodecode.model.db import Session, CacheKey, IntegrityError
33
33
@@ -50,7 +50,7 b' class RhodeCodeCacheRegion(CacheRegion):'
50 self, namespace=None,
50 self, namespace=None,
51 expiration_time=None,
51 expiration_time=None,
52 should_cache_fn=None,
52 should_cache_fn=None,
53 to_str=compat.string_type,
53 to_str=str,
54 function_key_generator=None,
54 function_key_generator=None,
55 condition=True):
55 condition=True):
56 """
56 """
@@ -58,74 +58,19 b' class RhodeCodeCacheRegion(CacheRegion):'
58 condition isn't meet. This works a bit different than should_cache_fn
58 condition isn't meet. This works a bit different than should_cache_fn
59 And it's faster in cases we don't ever want to compute cached values
59 And it's faster in cases we don't ever want to compute cached values
60 """
60 """
61 expiration_time_is_callable = compat.callable(expiration_time)
61 expiration_time_is_callable = callable(expiration_time)
62
62
63 if function_key_generator is None:
63 if function_key_generator is None:
64 function_key_generator = self.function_key_generator
64 function_key_generator = self.function_key_generator
65
65
66 # workaround for py2 and cython problems, this block should be removed
67 # once we've migrated to py3
68 if 'cython' == 'cython':
69 def decorator(fn):
70 if to_str is compat.string_type:
71 # backwards compatible
72 key_generator = function_key_generator(namespace, fn)
73 else:
74 key_generator = function_key_generator(namespace, fn, to_str=to_str)
75
76 @functools.wraps(fn)
77 def decorate(*arg, **kw):
78 key = key_generator(*arg, **kw)
79
80 @functools.wraps(fn)
81 def creator():
82 return fn(*arg, **kw)
83
84 if not condition:
85 return creator()
86
87 timeout = expiration_time() if expiration_time_is_callable \
88 else expiration_time
89
90 return self.get_or_create(key, creator, timeout, should_cache_fn)
91
92 def invalidate(*arg, **kw):
93 key = key_generator(*arg, **kw)
94 self.delete(key)
95
96 def set_(value, *arg, **kw):
97 key = key_generator(*arg, **kw)
98 self.set(key, value)
99
100 def get(*arg, **kw):
101 key = key_generator(*arg, **kw)
102 return self.get(key)
103
104 def refresh(*arg, **kw):
105 key = key_generator(*arg, **kw)
106 value = fn(*arg, **kw)
107 self.set(key, value)
108 return value
109
110 decorate.set = set_
111 decorate.invalidate = invalidate
112 decorate.refresh = refresh
113 decorate.get = get
114 decorate.original = fn
115 decorate.key_generator = key_generator
116 decorate.__wrapped__ = fn
117
118 return decorate
119 return decorator
120
121 def get_or_create_for_user_func(key_generator, user_func, *arg, **kw):
66 def get_or_create_for_user_func(key_generator, user_func, *arg, **kw):
122
67
123 if not condition:
68 if not condition:
124 log.debug('Calling un-cached method:%s', user_func.func_name)
69 log.debug('Calling un-cached method:%s', user_func.__name__)
125 start = time.time()
70 start = time.time()
126 result = user_func(*arg, **kw)
71 result = user_func(*arg, **kw)
127 total = time.time() - start
72 total = time.time() - start
128 log.debug('un-cached method:%s took %.4fs', user_func.func_name, total)
73 log.debug('un-cached method:%s took %.4fs', user_func.__name__, total)
129 return result
74 return result
130
75
131 key = key_generator(*arg, **kw)
76 key = key_generator(*arg, **kw)
@@ -133,11 +78,11 b' class RhodeCodeCacheRegion(CacheRegion):'
133 timeout = expiration_time() if expiration_time_is_callable \
78 timeout = expiration_time() if expiration_time_is_callable \
134 else expiration_time
79 else expiration_time
135
80
136 log.debug('Calling cached method:`%s`', user_func.func_name)
81 log.debug('Calling cached method:`%s`', user_func.__name__)
137 return self.get_or_create(key, user_func, timeout, should_cache_fn, (arg, kw))
82 return self.get_or_create(key, user_func, timeout, should_cache_fn, (arg, kw))
138
83
139 def cache_decorator(user_func):
84 def cache_decorator(user_func):
140 if to_str is compat.string_type:
85 if to_str is str:
141 # backwards compatible
86 # backwards compatible
142 key_generator = function_key_generator(namespace, user_func)
87 key_generator = function_key_generator(namespace, user_func)
143 else:
88 else:
@@ -200,7 +145,7 b' def compute_key_from_params(*args):'
200 """
145 """
201 Helper to compute key from given params to be used in cache manager
146 Helper to compute key from given params to be used in cache manager
202 """
147 """
203 return sha1("_".join(map(safe_str, args)))
148 return sha1(safe_bytes("_".join(map(str, args))))
204
149
205
150
206 def backend_key_generator(backend):
151 def backend_key_generator(backend):
@@ -232,7 +177,7 b' def get_or_create_region(region_name, re'
232 if not region_obj:
177 if not region_obj:
233 raise EnvironmentError(
178 raise EnvironmentError(
234 'Region `{}` not in configured: {}.'.format(
179 'Region `{}` not in configured: {}.'.format(
235 region_name, region_meta.dogpile_cache_regions.keys()))
180 region_name, list(region_meta.dogpile_cache_regions.keys())))
236
181
237 region_uid_name = '{}:{}'.format(region_name, region_namespace)
182 region_uid_name = '{}:{}'.format(region_name, region_namespace)
238 if isinstance(region_obj.actual_backend, FileNamespaceBackend):
183 if isinstance(region_obj.actual_backend, FileNamespaceBackend):
@@ -24,8 +24,7 b' for Python. Build with server client arc'
24 """
24 """
25 import atexit
25 import atexit
26 import logging
26 import logging
27 import urlparse
27 from io import StringIO
28 from cStringIO import StringIO
29
28
30 import rhodecode
29 import rhodecode
31 from rhodecode.lib.vcs.conf import settings
30 from rhodecode.lib.vcs.conf import settings
@@ -33,10 +32,6 b' from rhodecode.lib.vcs.backends import g'
33 from rhodecode.lib.vcs.exceptions import (
32 from rhodecode.lib.vcs.exceptions import (
34 VCSError, RepositoryError, CommitError, VCSCommunicationError)
33 VCSError, RepositoryError, CommitError, VCSCommunicationError)
35
34
36 VERSION = (0, 5, 0, 'dev')
37
38 __version__ = '.'.join((str(each) for each in VERSION[:4]))
39
40 __all__ = [
35 __all__ = [
41 'get_version', 'get_vcs_instance', 'get_backend',
36 'get_version', 'get_vcs_instance', 'get_backend',
42 'VCSError', 'RepositoryError', 'CommitError', 'VCSCommunicationError'
37 'VCSError', 'RepositoryError', 'CommitError', 'VCSCommunicationError'
General Comments 0
You need to be logged in to leave comments. Login now