def __init__(self, context=None, get_user_category=None, **kwds): # init log self.log = logging.getLogger(__name__ + ".DjangoContextAdapter") # init parent, filling in default context object if context is None: context = CryptContext() super(DjangoContextAdapter, self).__init__(context=context, **kwds) # setup user category if get_user_category: assert callable(get_user_category) self.get_user_category = get_user_category # install lru cache wrappers from django.utils.lru_cache import lru_cache self.get_hashers = lru_cache()(self.get_hashers) # get copy of original make_password from django.contrib.auth.hashers import make_password if make_password.__module__.startswith("passlib."): make_password = _PatchManager.peek_unpatched_func(make_password) self._orig_make_password = make_password # get other django helpers from django.contrib.auth.hashers import is_password_usable self.is_password_usable = is_password_usable # init manager mlog = logging.getLogger(__name__ + ".DjangoContextAdapter._manager") self._manager = _PatchManager(log=mlog)
def __init__(self, context=None, get_user_category=None, **kwds): # init log self.log = logging.getLogger(__name__ + ".DjangoContextAdapter") # init parent, filling in default context object if context is None: context = CryptContext() super(DjangoContextAdapter, self).__init__(context=context, **kwds) # setup user category if get_user_category: assert callable(get_user_category) self.get_user_category = get_user_category # install lru cache wrappers from django.utils.lru_cache import lru_cache self.get_hashers = lru_cache()(self.get_hashers) # get copy of original make_password from django.contrib.auth.hashers import make_password if make_password.__module__.startswith("passlib."): make_password = _PatchManager.peek_unpatched_func(make_password) self._orig_make_password = make_password # get other django helpers from django.contrib.auth.hashers import is_password_usable self.is_password_usable = is_password_usable # init manager mlog = logging.getLogger(__name__ + ".DjangoContextAdapter._manager") self._manager = _PatchManager(log=mlog)
def cached_func(user, *args, **kwargs): func_cache_name = '_djangocms_cached_func_%s' % func.__name__ if not hasattr(user, func_cache_name): cached_func = lru_cache(maxsize=None)(func) setattr(user, func_cache_name, cached_func) return getattr(user, func_cache_name)(user, *args, **kwargs)
def cached_func(user, *args, **kwargs): func_cache_name = '_djangocms_cached_func_%s' % func.__name__ if not hasattr(user, func_cache_name): cached_func = lru_cache(maxsize=None)(func) setattr(user, func_cache_name, cached_func) return getattr(user, func_cache_name)(user, *args, **kwargs)
def __init__(self, user, instance, permission_structure, **kwargs): self.MODEL = apps.get_model(self.MODEL_NAME) self.user = user self.user_groups = self.user.groups.values_list('name', flat=True) self.instance = instance self.condition_group_valid = lru_cache(maxsize=16)(self.condition_group_valid) self.permission_structure = permission_structure self.all_model_fields = get_all_field_names(self.MODEL) self.all_model_fields += self.EXTRA_FIELDS
def __init__(self, user, instance, permission_structure, **kwargs): self.MODEL = apps.get_model(self.MODEL_NAME) self.user = user self.user_groups = self.user.groups.values_list('name', flat=True) self.instance = instance self.condition_group_valid = lru_cache(maxsize=16)( self.condition_group_valid) self.permission_structure = permission_structure self.all_model_fields = get_all_field_names(self.MODEL) self.all_model_fields += self.EXTRA_FIELDS
def ignore_unhashable_lru_cache(maxsize: int = 128, typed: bool = False) -> DECORATOR: """ This is a wrapper over lru_cache function. It adds following features on top of lru_cache: * It will not cache result of functions with unhashable arguments. * It will clear cache whenever zerver.lib.cache.KEY_PREFIX changes. """ internal_decorator = lru_cache(maxsize=maxsize, typed=typed) def decorator(user_function: Callable[..., Any]) -> Callable[..., Any]: if settings.DEVELOPMENT and not settings.TEST_SUITE: # nocoverage # In the development environment, we want every file # change to refresh the source files from disk. return user_function cache_enabled_user_function = internal_decorator(user_function) def wrapper(*args: Any, **kwargs: Any) -> Any: if not hasattr(cache_enabled_user_function, 'key_prefix'): cache_enabled_user_function.key_prefix = KEY_PREFIX if cache_enabled_user_function.key_prefix != KEY_PREFIX: # Clear cache when cache.KEY_PREFIX changes. This is used in # tests. cache_enabled_user_function.cache_clear() cache_enabled_user_function.key_prefix = KEY_PREFIX try: return cache_enabled_user_function(*args, **kwargs) except TypeError: # args or kwargs contains an element which is unhashable. In # this case we don't cache the result. pass # Deliberately calling this function from outside of exception # handler to get a more descriptive traceback. Otherise traceback # can include the exception from cached_enabled_user_function as # well. return user_function(*args, **kwargs) setattr(wrapper, 'cache_info', cache_enabled_user_function.cache_info) setattr(wrapper, 'cache_clear', cache_enabled_user_function.cache_clear) return wrapper return decorator
def ignore_unhashable_lru_cache(maxsize: int=128, typed: bool=False) -> DECORATOR: """ This is a wrapper over lru_cache function. It adds following features on top of lru_cache: * It will not cache result of functions with unhashable arguments. * It will clear cache whenever zerver.lib.cache.KEY_PREFIX changes. """ internal_decorator = lru_cache(maxsize=maxsize, typed=typed) def decorator(user_function: Callable[..., Any]) -> Callable[..., Any]: if settings.DEVELOPMENT and not settings.TEST_SUITE: # nocoverage # In the development environment, we want every file # change to refresh the source files from disk. return user_function cache_enabled_user_function = internal_decorator(user_function) def wrapper(*args: Any, **kwargs: Any) -> Any: if not hasattr(cache_enabled_user_function, 'key_prefix'): cache_enabled_user_function.key_prefix = KEY_PREFIX if cache_enabled_user_function.key_prefix != KEY_PREFIX: # Clear cache when cache.KEY_PREFIX changes. This is used in # tests. cache_enabled_user_function.cache_clear() cache_enabled_user_function.key_prefix = KEY_PREFIX try: return cache_enabled_user_function(*args, **kwargs) except TypeError: # args or kwargs contains an element which is unhashable. In # this case we don't cache the result. pass # Deliberately calling this function from outside of exception # handler to get a more descriptive traceback. Otherise traceback # can include the exception from cached_enabled_user_function as # well. return user_function(*args, **kwargs) setattr(wrapper, 'cache_info', cache_enabled_user_function.cache_info) setattr(wrapper, 'cache_clear', cache_enabled_user_function.cache_clear) return wrapper return decorator
def __init__(self, context=None, get_user_category=None, **kwds): self.log = logging.getLogger(__name__ + '.DjangoContextAdapter') if context is None: context = CryptContext() super(DjangoContextAdapter, self).__init__(context=context, **kwds) if get_user_category: self.get_user_category = get_user_category from django.utils.lru_cache import lru_cache self.get_hashers = lru_cache()(self.get_hashers) from django.contrib.auth.hashers import make_password if make_password.__module__.startswith('passlib.'): make_password = _PatchManager.peek_unpatched_func(make_password) self._orig_make_password = make_password from django.contrib.auth.hashers import is_password_usable self.is_password_usable = is_password_usable mlog = logging.getLogger(__name__ + '.DjangoContextAdapter._manager') self._manager = _PatchManager(log=mlog) return
def patch_check_function(org): """ Patches the check_for_language function so that it always returns true for pseudo localizations. :param org: the original function. :return: the patched function. """ def check_for_language(lang): if lang.startswith("pse"): return True return org(lang) if django.VERSION < (1, 7): from django.utils.functional import memoize check_for_language = memoize(check_for_language, {}, 1) else: from django.utils import lru_cache check_for_language = lru_cache.lru_cache(1000)(check_for_language) return check_for_language
It makes use of the assumption that data is static throughout the runtime of the application and therefore any data imports or updates will require an application restart before they take effect. """ from django.conf import settings from django.utils.lru_cache import lru_cache from frontend.models import Practice from .connection import MatrixStore from .row_grouper import RowGrouper # Create a memoize decorator (i.e. a decorator which caches the return value # for a given set of arguments). Here `maxsize=None` means "don't apply any # cache eviction, just keep values for ever" memoize = lru_cache(maxsize=None) @memoize def get_db(): """ Return a singleton instance of the current live version of the MatrixStore """ return MatrixStore.from_file(settings.MATRIXSTORE_LIVE_FILE) @memoize def get_row_grouper(org_type): """ Return a "row grouper" function which will group the rows of a practice level matrix by the supplied `org_type`
def _get_translation_field_names(): """ Returns Translation base model field names (excepted "id" field). """ from .models import Translation fields = [f.name for f in Translation._meta.get_fields()] fields.remove("id") return fields try: from django.utils.lru_cache import lru_cache get_translation_field_names = lru_cache()(_get_translation_field_names) except ImportError: from django.utils.functional import memoize get_translation_field_names = memoize(_get_translation_field_names, {}, 0) @python_2_unicode_compatible class CachedTranslation(object): def __init__(self, **kwargs): self.fields = get_translation_field_names() attrs = self.fields + ["instance", "translation"] for attr in attrs: setattr(self, attr, None)
import django from django.utils.safestring import mark_safe from fluent_contents.models import ContentItemOutput from fluent_contents.utils.search import get_cleaned_string from .core import PlaceholderRenderingPipe, SkipItem, ResultTracker from .utils import get_dummy_request if django.VERSION >= (1, 8): # Use modern lru_cache, avoids deprecation warnings. Needs Python 2.7+ from django.utils.lru_cache import lru_cache _get_dummy_search_request = lru_cache()(get_dummy_request) else: from django.utils.functional import memoize _SEARCH_REQUEST_CACHE = {} _get_dummy_search_request = memoize(get_dummy_request, _SEARCH_REQUEST_CACHE, 1) class SearchResultTracker(ResultTracker): def store_output(self, contentitem, output): # Strip all output from HTML tags while collecting # (the output is already cached at this point) output.html = get_cleaned_string(output.html) output.cacheable = False super(SearchResultTracker, self).store_output(contentitem, output) class SearchRenderingPipe(PlaceholderRenderingPipe): """ Variation of the rendering, suitable for search indexers. """
Looks up a NAV object from a metric path. :param metric: A Graphite metric path. :type metric: str :return: If a match was found, a Model instance from any model in the nav.models package. """ for pattern, func in _reverse_handlers: match = pattern.search(metric) if match: return func(**match.groupdict()) # pylint: disable=C0103 lookup = lru_cache(maxsize=200)(_lookup) def reverses(pattern): """Decorator to map regex patterns to reverse lookup functions""" try: pattern.pattern except AttributeError: pattern = re.compile(pattern) def _decorator(func): _reverse_handlers.append((pattern, func)) return func return _decorator
import django from django.utils.safestring import mark_safe from fluent_contents.models import ContentItemOutput from fluent_contents.utils.search import get_cleaned_string from .core import PlaceholderRenderingPipe, SkipItem, ResultTracker from .utils import get_dummy_request if django.VERSION >= (1, 8): # Use modern lru_cache, avoids deprecation warnings. Needs Python 2.7+ from django.utils.lru_cache import lru_cache _get_dummy_search_request = lru_cache()(get_dummy_request) else: from django.utils.functional import memoize _SEARCH_REQUEST_CACHE = {} _get_dummy_search_request = memoize(get_dummy_request, _SEARCH_REQUEST_CACHE, 1) class SearchResultTracker(ResultTracker): def store_output(self, contentitem, output): # Strip all output from HTML tags while collecting # (the output is already cached at this point) output.html = get_cleaned_string(output.html) output.cacheable = False super(SearchResultTracker, self).store_output(contentitem, output) class SearchRenderingPipe(PlaceholderRenderingPipe): """ Variation of the rendering, suitable for search indexers. """
def memoize(func, cache, num_args): return lru_cache()(func)
def _get_translation_field_names(): """ Returns Translation base model field names (excepted "id" field). """ from .models import Translation fields = Translation._meta.get_all_field_names() fields.remove('id') return fields try: from django.utils.lru_cache import lru_cache get_translation_field_names = lru_cache()(_get_translation_field_names) except ImportError: from django.utils.functional import memoize get_translation_field_names = memoize(_get_translation_field_names, {}, 0) @python_2_unicode_compatible class CachedTranslation(object): def __init__(self, **kwargs): self.fields = get_translation_field_names() attrs = self.fields + ['instance', 'translation'] for attr in attrs:
def reverse_lookup(addresses): """Do a reverse lookup on addresses""" reverses = asyncdns.reverse_lookup(addresses) for addr, response in sorted(reverses.items(), key=address_sorter): if isinstance(response, Exception): yield {'addr': addr, 'error': response.__class__.__name__} else: for name in response: yield {'addr': addr, 'name': name} def _get_host_info(host): """Returns a dictionary containing DNS information about the host""" if is_valid_ip(host, use_socket_lib=True): addresses = list(reverse_lookup([host])) else: try: addresses = forward_lookup(host) or [] except UnicodeError: # Probably just some invalid string that cannot be represented using IDNA # encoding. Let's just pretend it never happened (i.e. we can't look it up) addresses = [] if addresses: addresses = list(reverse_lookup(addresses)) return {'host': host, 'addresses': addresses} get_host_info = lru_cache()(_get_host_info)
from sequere.registry import registry from sequere.backends.redis.connection import manager as backend from .exceptions import ActionDoesNotExist def _get_actions(): return dict((action.verb, action) for model_class in registry.values() for action in getattr(model_class, 'actions', [])) try: from django.utils.lru_cache import lru_cache get_actions = lru_cache()(_get_actions) except ImportError: from django.utils.functional import memoize get_actions = memoize(_get_actions, {}, 0) @python_2_unicode_compatible class Action(object): verb = None def __init__(self, actor, target=None, date=None, **kwargs): self.actor = actor self.target = target self.kwargs = kwargs.pop('kwargs', {}) self.date = date or datetime.now()
def handle_new_alerts(new_alerts): """Handles new alerts on the queue""" memoized_check_alert = lru_cache()(check_alert_against_filtergroupcontents) logger = logging.getLogger('nav.alertengine.handle_new_alerts') accounts = [] def subscription_sort_key(subscription): """Return a key to sort alertsubscriptions in a prioritized order.""" sort_order = [ AlertSubscription.NOW, AlertSubscription.NEXT, AlertSubscription.DAILY, AlertSubscription.WEEKLY, ] try: return sort_order.index(subscription.type) except ValueError: return subscription.type # Build datastructure that contains accounts and corresponding # filtergroupcontent_set so that we don't redo db queries to much for account in Account.objects.filter( alertpreference__active_profile__isnull=False): profile = account.get_active_profile() time_period = profile.get_active_timeperiod() if profile else None if not time_period: continue current_alertsubscriptions = sorted( time_period.alertsubscription_set.all(), key=subscription_sort_key) tmp = [] for alertsubscription in current_alertsubscriptions: tmp.append( (alertsubscription, alertsubscription.filter_group.filtergroupcontent_set.all())) if tmp: permissions = [] for filtergroup in FilterGroup.objects.filter( group_permissions__accounts__in=[account]): permissions.append(filtergroup.filtergroupcontent_set.all()) accounts.append((account, tmp, permissions)) del permissions del tmp del current_alertsubscriptions del account # Remember which alerts are sent where to avoid duplicates dupemap = set() # Check all acounts against all their active subscriptions for account, alertsubscriptions, permissions in accounts: logger.debug("Checking new alerts for account '%s'", account) for alert in new_alerts: _check_match_and_permission(account, alert, alertsubscriptions, dupemap, logger, memoized_check_alert, permissions) del alert del account del permissions del memoized_check_alert del new_alerts gc.collect()
def memoize(function, *args): return lru_cache()(function)
from .sanitizer import Sanitizer def _get_sanitizer(name="default"): sanitizers = getattr(settings, "HTML_SANITIZERS", {}) if name in sanitizers: return Sanitizer(sanitizers[name]) elif name == "default": return Sanitizer() raise ImproperlyConfigured( "Unknown sanitizer %r, did you define HTML_SANITIZERS[%r] in your" " Django settings module?" % (name, name) ) get_sanitizer = lru_cache.lru_cache(maxsize=None)(_get_sanitizer) @checks.register() def check_configuration(app_configs, **kwargs): errors = [] sanitizers = ["default"] + list(getattr(settings, "HTML_SANITIZERS", {})) for name in sorted(set(sanitizers)): try: _get_sanitizer(name) except TypeError as exc: errors.append( checks.Error( "Invalid sanitizer configuration '%s': %s" % (name, exc), id="html_sanitizer.E001", )
def u(val): if not isinstance(val, str): return User.objects.get(pk=val) if '@' in val: return User.objects.get(email=val) if 'username' in [x.attname for x in User._meta.fields]: return User.objects.get(username=val) raise User.DoesNotExist() try: from django.utils.lru_cache import lru_cache u = lru_cache()(u) except ImportError: from django.utils.functional import memoize u = memoize(u, {}, 1) try: from django.apps import apps get_models = apps.get_models except ImportError: get_models = models.get_models for model in get_models(): globals()['%s_%s' % (model._meta.app_label, model._meta.object_name)] = model del model
def memoize(function, *args): return lru_cache()(function)
def u(val): if not isinstance(val, six.string_types): return User.objects.get(pk=val) if "@" in val: return User.objects.get(email=val) if "username" in [x.attname for x in User._meta.fields]: return User.objects.get(username=val) raise User.DoesNotExist() try: from django.utils.lru_cache import lru_cache u = lru_cache()(u) except ImportError: from django.utils.functional import memoize u = memoize(u, {}, 1) def DGET(url): import urllib from django.core.files.base import ContentFile return ContentFile(urllib.urlopen(url).read()) try: from django.apps import apps get_models = apps.get_models