コード例 #1
0
ファイル: config.py プロジェクト: PabloRosales/im-core-python
def configure(config_dict='features', set_project_path=None,
              configure_functions=configure_functions,
              directory=None, local_paths=None, override='NONE'):
    """Allows easy configuration of core features. Format is
    feature => key (for use when loading the YAML file when the config file
    is not named after the feature). If the key is None then will load the
    config file with the feature name.

    If a callable is available in ``configure_functions`` for the feature will
    call it with the config loaded.

    Automatically loads the global config file.

    :param config_dict: A string or dict with the features to configure. If a \
    string then will search for a YAML file in configs directory with that \
    key.

    :param configure_functions: A dict with features and their callable, if \
    a callable for the feature is available it will be called with the config \
    loaded.

    :param local_paths: The paths to use for local configuration files loading,\
    if local_paths is None will use the default config paths.
    """
    global _local_paths
    
    if set_project_path is not None:
        _set_project_path(set_project_path)

    configs.load_config('global_config', config_class=YamlConfig, paths='/etc/im/')

    if local_paths is None:
        if _local_paths is None:
            _local_paths = [
                os.path.join(configs['global_config']['configs_path'], 'defaults'),
                os.path.join(configs['global_config']['configs_path'], 'globals'),
                os.path.join(get_project_path(), 'configs'),
            ]
        else:
            _local_paths.append(os.path.join(get_project_path(), 'configs'))
    else:
        _local_paths = local_paths

    if isinstance(config_dict, basestring):
        if directory:
            config_dict = os.path.join(directory, config_dict)
        config_dict = _get_features_config(config_dict, override=override)
        
    for key, value in config_dict.iteritems():
        if value is None:
            configs.load_config(key, override=override)
        else:
            configs.load_config(value, config_type=key, override=override)

        if key in configure_functions:
            configure_functions[key](directory)

    configs['project_path'] = get_project_path()
コード例 #2
0
def _configure_werkzeug(directory=None):
    global configs

    if not directory:
        directory = ''

    configs['werkzeug']['routes'] = YamlConfig('routes',
                                               paths=os.path.join(
                                                   get_project_path(),
                                                   'configs', directory))
コード例 #3
0
ファイル: config.py プロジェクト: PabloRosales/im-core-python
def _configure_werkzeug(directory=None):
    global configs

    if not directory:
        directory = ''

    configs['werkzeug']['routes'] = YamlConfig(
        'routes',
        paths=os.path.join(get_project_path(), 'configs', directory)
    )
コード例 #4
0
    def __init__(self,
                 template_name,
                 template_vars=None,
                 config=None,
                 _format='html',
                 template_lookup_class=TemplateLookup,
                 layout={}):

        self.config = self.default_configuration
        self.config.update_recursive(configs.get('templates', {}))
        if config:
            self.config.update_recursive(config)

        self.template_name = template_name

        # add project path templates
        project_templates = os.path.join(get_project_path(), 'templates')
        if not project_templates in self.config['directories']:
            self.config['directories'].insert(0, project_templates)

        template_lookup = TemplateLookup(
            directories=self.config.get('directories'),
            input_encoding=self.config.get('input_encoding'),
            output_encoding=self.config.get('output_encoding'),
            encoding_errors=self.config.get('encoding_errors'),
            strict_undefined=self.config.get('strict_undefined'),
            default_filters=self.config.get(
                'default_filters', self.config.get('default_filters')))

        self.template_format = _format

        self.template_inherit = None
        self.template_sections = None
        self.template_layout = None

        if layout:
            self.template_inherit = ".".join(
                [layout.get("master"),
                 layout.get("format")])
            self.template_layout = ".".join(
                [layout.get("layout"),
                 layout.get("format")])
            self.template_name = ".".join(
                ["/" + self.template_name,
                 layout.get("format")])
        else:
            self._check_for_layout()

        self.template = template_lookup.get_template(self.template_name)
        self.template_vars = self.config.get('default_vars')
        self.template_vars.update_recursive(template_vars or {})

        self.rendered = None
コード例 #5
0
    def __init__(self,
                 query,
                 params=None,
                 from_file=True,
                 model_class=None,
                 database='default',
                 sessions=sessions,
                 cached=False,
                 replace=None,
                 config=None,
                 cache_timeout=3600 * 12):

        self.config = self.default_configuration
        self.config.update_recursive(configs.get('mysql', {}))
        if config:
            self.config.update_recursive(config)

        # add project path queries directory if not already configured
        project_queries = os.path.join(get_project_path(), 'queries')
        if not project_queries in self.config['query_directories']:
            self.config['query_directories'].append(project_queries)

        if from_file:
            if query in file_queries:
                logger.debug('Getting query file from query files cache %s' %
                             query)
                self.query = file_queries[query]
            else:
                logger.debug('Getting query file %s' % query)
                file_queries[query] = self._get_from_file(query)
                self.query = file_queries[query]
        else:
            self.query = query

        if replace:
            self.query = self.query % replace

        self.params = params
        self.session = sessions.get(database)
        self.result = None
        self.model_class = model_class
        self.cached = cached
        self.query_cache = MemcachedCache(['127.0.0.1:11211'])
        self.cache_timeout = cache_timeout
        self.database = database
コード例 #6
0
ファイル: mako.py プロジェクト: PabloRosales/im-core-python
    def __init__(self, template_name, template_vars=None, config=None,
            _format='html', template_lookup_class=TemplateLookup, layout={}):

        self.config = self.default_configuration
        self.config.update_recursive(configs.get('templates', {}))
        if config:
            self.config.update_recursive(config)

        self.template_name = template_name

        # add project path templates
        project_templates = os.path.join(get_project_path(), 'templates')
        if not project_templates in self.config['directories']:
            self.config['directories'].insert(0,project_templates)
            
        template_lookup = TemplateLookup(
            directories=self.config.get('directories'),
            input_encoding=self.config.get('input_encoding'),
            output_encoding=self.config.get('output_encoding'),
            encoding_errors=self.config.get('encoding_errors'),
            strict_undefined=self.config.get('strict_undefined'),
            default_filters=self.config.get('default_filters', self.config.get('default_filters'))
        )

        self.template_format = _format

        self.template_inherit = None
        self.template_sections = None
        self.template_layout = None

        if layout:
            self.template_inherit = ".".join([layout.get("master"),layout.get("format")])
            self.template_layout = ".".join([layout.get("layout"),layout.get("format")])
            self.template_name = ".".join(["/" + self.template_name,layout.get("format")])
        else:
            self._check_for_layout()
        
        self.template = template_lookup.get_template(self.template_name)
        self.template_vars = self.config.get('default_vars')
        self.template_vars.update_recursive(template_vars or {})

        self.rendered = None
コード例 #7
0
ファイル: mysql.py プロジェクト: PabloRosales/im-core-python
    def __init__(self, query, params=None, from_file=True, model_class=None,
                 database='default', sessions=sessions, cached=False,
                 replace=None, config=None, cache_timeout=3600*12):

        self.config = self.default_configuration
        self.config.update_recursive(configs.get('mysql', {}))
        if config:
            self.config.update_recursive(config)

        # add project path queries directory if not already configured
        project_queries = os.path.join(get_project_path(), 'queries')
        if not project_queries in self.config['query_directories']:
            self.config['query_directories'].append(project_queries)

        if from_file:
            if query in file_queries:
                logger.debug('Getting query file from query files cache %s' %
                             query)
                self.query = file_queries[query]
            else:
                logger.debug('Getting query file %s' % query)
                file_queries[query] = self._get_from_file(query)
                self.query = file_queries[query]
        else:
            self.query = query

        if replace:
            self.query = self.query % replace

        self.params = params
        self.session = sessions.get(database)
        self.result = None
        self.model_class = model_class
        self.cached = cached
        self.query_cache = MemcachedCache(['127.0.0.1:11211'])
        self.cache_timeout = cache_timeout
        self.database = database
コード例 #8
0
def configure(config_dict='features',
              set_project_path=None,
              configure_functions=configure_functions,
              directory=None,
              local_paths=None,
              override='NONE'):
    """Allows easy configuration of core features. Format is
    feature => key (for use when loading the YAML file when the config file
    is not named after the feature). If the key is None then will load the
    config file with the feature name.

    If a callable is available in ``configure_functions`` for the feature will
    call it with the config loaded.

    Automatically loads the global config file.

    :param config_dict: A string or dict with the features to configure. If a \
    string then will search for a YAML file in configs directory with that \
    key.

    :param configure_functions: A dict with features and their callable, if \
    a callable for the feature is available it will be called with the config \
    loaded.

    :param local_paths: The paths to use for local configuration files loading,\
    if local_paths is None will use the default config paths.
    """
    global _local_paths

    if set_project_path is not None:
        _set_project_path(set_project_path)

    configs.load_config('global_config',
                        config_class=YamlConfig,
                        paths='/etc/im/')

    if local_paths is None:
        if _local_paths is None:
            _local_paths = [
                os.path.join(configs['global_config']['configs_path'],
                             'defaults'),
                os.path.join(configs['global_config']['configs_path'],
                             'globals'),
                os.path.join(get_project_path(), 'configs'),
            ]
        else:
            _local_paths.append(os.path.join(get_project_path(), 'configs'))
    else:
        _local_paths = local_paths

    if isinstance(config_dict, basestring):
        if directory:
            config_dict = os.path.join(directory, config_dict)
        config_dict = _get_features_config(config_dict, override=override)

    for key, value in config_dict.iteritems():
        if value is None:
            configs.load_config(key, override=override)
        else:
            configs.load_config(value, config_type=key, override=override)

        if key in configure_functions:
            configure_functions[key](directory)

    configs['project_path'] = get_project_path()
コード例 #9
0
def _get_local_query_files():
    return _get_all_files_in_dir(os.path.join(get_project_path(), 'queries'))
コード例 #10
0
def _get_local_config_files():
    return _get_all_files_in_dir(os.path.join(get_project_path(), 'configs'))