<commit_before>from __future__ import unicode_literals

import logging
from functools import update_wrapper

from pyramid import httpexceptions
from pyramid.renderers import render_to_response, get_renderer
from pyramid.response import Response
from zope.interface.interfaces import ComponentLookupError

from pyramid_views.utils import ImproperlyConfigured, classonlymethod, get_template_package
import pyramid_views

logger = logging.getLogger('django.request')


class ContextMixin(object):
    """
    A default context mixin that passes the keyword arguments received by
    get_context_data as the template context.
    """

    def get_context_data(self, **kwargs):
        if 'view' not in kwargs:
            kwargs['view'] = self
        self._context = kwargs
        return kwargs


class MacroMixin(object):
    macro_names = None

    def get_macro_names(self):
        """
        Return a directory of macro names.

        Values should be template paths, and keys will be used
        as the lookup key in the template. Eg. ``macros.<key>.<macro>``.
        """
        return self.macro_names or {}

    def get_macros(self):
        macro_names = self.get_macro_names()
        macros = {}
        for k, macro_name in macro_names.items():
            template = get_renderer(macro_name).implementation()
            if hasattr(template, 'macros'):
                macros[k] = template.macros
        return macros

    def get_context_data(self, **kwargs):
        context = super(MacroMixin, self).get_context_data(**kwargs)
        context['macros'] = self.get_macros()
        return context


class DbSessionMixin(object):
    """
    A default db session mixin which will provide access to the
    application's scoped database session
    """

    _db_session = None

    @property
    def db_session(self):
        if self._db_session:
            return self._db_session
        elif pyramid_views.session:
            return pyramid_views.session
        else:
            raise ImproperlyConfigured("DB session not available. Either set the view's 'session' "
                                       "attribute, or call pyramid_views.configure_views() with a "
                                       "scoped session.")

<commit_msg>Simplifying and fixing db session handling<commit_after>from __future__ import unicode_literals

import logging
from functools import update_wrapper

from pyramid import httpexceptions
from pyramid.renderers import render_to_response, get_renderer
from pyramid.response import Response
from zope.interface.interfaces import ComponentLookupError

from pyramid_views.utils import ImproperlyConfigured, classonlymethod, get_template_package
import pyramid_views

logger = logging.getLogger('django.request')


class ContextMixin(object):
    """
    A default context mixin that passes the keyword arguments received by
    get_context_data as the template context.
    """

    def get_context_data(self, **kwargs):
        if 'view' not in kwargs:
            kwargs['view'] = self
        self._context = kwargs
        return kwargs


class MacroMixin(object):
    macro_names = None

    def get_macro_names(self):
        """
        Return a directory of macro names.

        Values should be template paths, and keys will be used
        as the lookup key in the template. Eg. ``macros.<key>.<macro>``.
        """
        return self.macro_names or {}

    def get_macros(self):
        macro_names = self.get_macro_names()
        macros = {}
        for k, macro_name in macro_names.items():
            template = get_renderer(macro_name).implementation()
            if hasattr(template, 'macros'):
                macros[k] = template.macros
        return macros

    def get_context_data(self, **kwargs):
        context = super(MacroMixin, self).get_context_data(**kwargs)
        context['macros'] = self.get_macros()
        return context


class DbSessionMixin(object):
    """
    A default db session mixin which will provide access to the
    application's scoped database session
    """

    @property
    def db_session(self):
        if pyramid_views.session:
            return pyramid_views.session()
        else:
            raise ImproperlyConfigured("DB session not available. Either set the view's 'session' "
                                       "attribute, or call pyramid_views.configure_views() with a "
                                       "scoped session.")


{'fraction_non_alphanumeric': 0.07482078853046595, 'fraction_numerical': 0.0, 'mean_word_length': 2.618314424635332, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1332741', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1150, 'n_words': 361}