# Path: pyvac/helpers/ldap.py
# class LdapCache(object):
#     """ Ldap cache class singleton """
#     _instance = None
# 
#     def __new__(cls, *args, **kwargs):
#         if not cls._instance:
#             raise RuntimeError('Ldap is not initialized')
# 
#         return cls._instance
# 
#     @classmethod
#     def configure(cls, settings):
#         cls._instance = cls.from_config(settings)
# 
#     @classmethod
#     def from_config(cls, config, **kwargs):
#         """
#         Return a Ldap client object configured from the given configuration.
#         """
#         return LdapWrapper(config)
# 
# Path: pyvac/helpers/i18n.py
# def locale_negotiator(request):
#     """
#     Locale negotiator for pyramid views.
# 
#     This version differs from Pyramid's :py:func:`default_locale_negotiator
#     <pyramid.i18n.default_locale_negotiator>` in that it gets the locale from
#     the url parameter or the cookie, and fallbacks to the user's lang.
#     """
# 
#     login = authenticated_userid(request)
#     if login:
#         from pyvac.models import DBSession, User
#         session = DBSession()
#         user = User.by_login(session, login)
#         if user.country == 'us':
#             return 'en'
#         if user.country == 'zh':
#             return 'en'
#         return user.country
# 
#     return None
# 
# Path: pyvac/helpers/holiday.py
# def init_override(content):
#     """Load a yaml file for holidays override.
# 
#     You can override holidays for a country and a year through
#     usage of a configuration setting:
#     pyvac.override_holidays_file = %(here)s/conf/holidays.yaml
# 
#     here is a sample:
# zh:
#   2016:
#     '2016-01-01': 'New Years Day'
#     '2016-02-07': 'Chinese New Years Eve'
#     """
#     if not content:
#         return
#     override.update(content)


import os.path
import yaml
from pyramid.interfaces import IBeforeRender
from pyramid.security import has_permission
from pyramid.url import static_path, route_path
from pyramid.exceptions import Forbidden
from pyramid_jinja2 import renderer_factory
from pyramid.settings import asbool
    from yaml import CSafeLoader as YAMLLoader
    from yaml import SafeLoader as YAMLLoader
    from logging.config import dictConfig
    from logutils.dictconfig import dictConfig
from pyvac.helpers.ldap import LdapCache
from pyvac.helpers.i18n import locale_negotiator
from pyvac.helpers.holiday import init_override
                from celery import current_app as default_app
                from celery.app import default_app
        # XXX: must call loader for celery to register all the tasks
        default_app.loader.import_default_modules()

    return conf


def add_urlhelpers(event):
    """
    Add helpers to the template engine.
    """
    event['static_url'] = lambda x: static_path(x, event['request'])
    event['route_url'] = lambda name, *args, **kwargs: route_path(
        name, event['request'], *args, **kwargs)
    event['has_permission'] = lambda perm: has_permission(
        perm, event['request'].context, event['request'])


def includeme(config):
    """
    Pyramid includeme file for the :class:`pyramid.config.Configurator`
    """

    ldap = False
    settings = config.registry.settings
    if 'pyvac.celery.yaml' in settings:
        configure(settings['pyvac.celery.yaml'])

    if 'pyvac.use_ldap' in settings:
        ldap = asbool(settings['pyvac.use_ldap'])
        if ldap:

# Path: pyvac/helpers/ldap.py
# class LdapCache(object):
#     """ Ldap cache class singleton """
#     _instance = None
# 
#     def __new__(cls, *args, **kwargs):
#         if not cls._instance:
#             raise RuntimeError('Ldap is not initialized')
# 
#         return cls._instance
# 
#     @classmethod
#     def configure(cls, settings):
#         cls._instance = cls.from_config(settings)
# 
#     @classmethod
#     def from_config(cls, config, **kwargs):
#         """
#         Return a Ldap client object configured from the given configuration.
#         """
#         return LdapWrapper(config)
# 
# Path: pyvac/helpers/i18n.py
# def locale_negotiator(request):
#     """
#     Locale negotiator for pyramid views.
# 
#     This version differs from Pyramid's :py:func:`default_locale_negotiator
#     <pyramid.i18n.default_locale_negotiator>` in that it gets the locale from
#     the url parameter or the cookie, and fallbacks to the user's lang.
#     """
# 
#     login = authenticated_userid(request)
#     if login:
#         from pyvac.models import DBSession, User
#         session = DBSession()
#         user = User.by_login(session, login)
#         if user.country == 'us':
#             return 'en'
#         if user.country == 'zh':
#             return 'en'
#         return user.country
# 
#     return None
# 
# Path: pyvac/helpers/holiday.py
# def init_override(content):
#     """Load a yaml file for holidays override.
# 
#     You can override holidays for a country and a year through
#     usage of a configuration setting:
#     pyvac.override_holidays_file = %(here)s/conf/holidays.yaml
# 
#     here is a sample:
# zh:
#   2016:
#     '2016-01-01': 'New Years Day'
#     '2016-02-07': 'Chinese New Years Eve'
#     """
#     if not content:
#         return
#     override.update(content)


# Path: pyvac/config.py
import os.path
import yaml
from pyramid.interfaces import IBeforeRender
from pyramid.security import has_permission
from pyramid.url import static_path, route_path
from pyramid.exceptions import Forbidden
from pyramid_jinja2 import renderer_factory
from pyramid.settings import asbool
    from yaml import CSafeLoader as YAMLLoader
    from yaml import SafeLoader as YAMLLoader
    from logging.config import dictConfig
    from logutils.dictconfig import dictConfig
from pyvac.helpers.ldap import LdapCache
from pyvac.helpers.i18n import locale_negotiator
from pyvac.helpers.holiday import init_override
                from celery import current_app as default_app
                from celery.app import default_app

        # XXX: must call loader for celery to register all the tasks
        default_app.loader.import_default_modules()

    return conf


def add_urlhelpers(event):
    """
    Add helpers to the template engine.
    """
    event['static_url'] = lambda x: static_path(x, event['request'])
    event['route_url'] = lambda name, *args, **kwargs: route_path(
        name, event['request'], *args, **kwargs)
    event['has_permission'] = lambda perm: has_permission(
        perm, event['request'].context, event['request'])


def includeme(config):
    """
    Pyramid includeme file for the :class:`pyramid.config.Configurator`
    """

    ldap = False
    settings = config.registry.settings
    if 'pyvac.celery.yaml' in settings:
        configure(settings['pyvac.celery.yaml'])

    if 'pyvac.use_ldap' in settings:
        ldap = asbool(settings['pyvac.use_ldap'])
        if ldap:

            LdapCache.configure(settings['pyvac.ldap.yaml'])