Ejemplo n.º 1
0
class CKANConfigLoader(object):
    def __init__(self, filename):
        self.config_file = filename.strip()
        self.config = dict()
        self.parser = ConfigParser()
        self.section = u'app:main'
        defaults = {u'__file__': os.path.abspath(self.config_file)}
        self._update_defaults(defaults)
        self._create_config_object()

    def _update_defaults(self, new_defaults):
        for key, value in new_defaults.items():
            self.parser._defaults[key] = value

    def _read_config_file(self, filename):
        defaults = {u'here': os.path.dirname(os.path.abspath(filename))}
        self._update_defaults(defaults)
        self.parser.read(filename)

    def _update_config(self):
        options = self.parser.options(self.section)
        for option in options:
            if option not in self.config or option in self.parser.defaults():
                value = self.parser.get(self.section, option)
                self.config[option] = value
                if option in self.parser.defaults():
                    self.config[u'global_conf'][option] = value

    def _create_config_object(self):
        self._read_config_file(self.config_file)

        # # The global_config key is to keep compatibility with Pylons.
        # # It can be safely removed when the Flask migration is completed.
        self.config[u'global_conf'] = self.parser.defaults().copy()

        self._update_config()

        schema, path = self.parser.get(self.section, u'use').split(u':')
        if schema == u'config':
            use_config_path = os.path.join(
                os.path.dirname(os.path.abspath(self.config_file)), path)
            self._read_config_file(use_config_path)
            self._update_config()

    def get_config(self):
        return self.config.copy()
Ejemplo n.º 2
0
    def defaults(self):
        """Return the defaults, with their values interpolated (with the
        defaults dict itself)

        Mainly to support defaults using values such as %(here)s
        """
        defaults = ConfigParser.defaults(self).copy()
        for key, val in iteritems(defaults):
            defaults[key] = self.get('DEFAULT', key) or val
        return defaults
Ejemplo n.º 3
0
    def defaults(self):
        """Return the defaults, with their values interpolated (with the
        defaults dict itself)

        Mainly to support defaults using values such as %(here)s
        """
        defaults = ConfigParser.defaults(self).copy()
        for key, val in iteritems(defaults):
            defaults[key] = self.get('DEFAULT', key) or val
        return defaults
Ejemplo n.º 4
0
def get_config():
    '''Returns a dict of config options from the config file'''
    cfgparse = ConfigParser()
    cfgparse.read(['.lpipyrc', path.expanduser('~/.lpipyrc')])
    config = cfgparse.defaults()
    local = os.environ.get('VIRTUAL_ENV', path.expanduser('~/.local/'))
    datadir = config.get('path', path.join(local, 'var', 'lpi'))

    return {
        "datadir": datadir,
    }
Ejemplo n.º 5
0
 def put_ini(self, text):
     """
     """
     context = self.context
     parser = ConfigParser()
     parser.readfp(cStringIO(text))
     for option, value in parser.defaults().items():
         prop_type = context.getPropertyType(option)
         if prop_type is None:
             context._setProperty(option, value, 'string')
         else:
             context._updateProperty(option, value)
Ejemplo n.º 6
0
 def _read(self):
     parser = ConfigParser()
     parser.read(self.path)
     self._globals = parser.defaults()
     data = {}
     for section in parser.sections():
         section_data = data.setdefault(section, {})
         for option in parser.options(section):
             if option in self._globals:
                 continue
             section_data[option] = parser.get(section, option)
     return data
 def put_ini(self, text):
     """
     """
     context = self.context
     parser = ConfigParser()
     try:
         parser.read_file(cStringIO(text))
     except AttributeError:  # Python 2
         parser.readfp(cStringIO(text))
     for option, value in parser.defaults().items():
         prop_type = context.getPropertyType(option)
         if prop_type is None:
             context._setProperty(option, value, 'string')
         else:
             context._updateProperty(option, value)
Ejemplo n.º 8
0
 def put_ini(self, text):
     """
     """
     context = self.context
     parser = ConfigParser()
     try:
         parser.read_file(cStringIO(text))
     except AttributeError:  # Python 2
         parser.readfp(cStringIO(text))
     for option, value in parser.defaults().items():
         prop_type = context.getPropertyType(option)
         if prop_type is None:
             context._setProperty(option, value, 'string')
         else:
             context._updateProperty(option, value)
Ejemplo n.º 9
0
class CKANConfigLoader(object):
    def __init__(self, filename):
        self.config_file = filename.strip()
        self.config = dict()
        self.parser = ConfigParser()
        self.section = u'app:main'
        defaults = {u'__file__': os.path.abspath(self.config_file)}
        self._update_defaults(defaults)
        self._create_config_object()

    def _update_defaults(self, new_defaults):
        for key, value in new_defaults.items():
            self.parser._defaults[key] = value

    def _read_config_file(self, filename):
        defaults = {u'here': os.path.dirname(os.path.abspath(filename))}
        self._update_defaults(defaults)
        self.parser.read(filename)

    def _update_config(self):
        options = self.parser.options(self.section)
        for option in options:
            if option not in self.config or option in self.parser.defaults():
                value = self.parser.get(self.section, option)
                self.config[option] = value
                if option in self.parser.defaults():
                    self.config[u'global_conf'][option] = value

    def _create_config_object(self):
        use_config_path = self.config_file
        self._read_config_file(use_config_path)

        # # The global_config key is to keep compatibility with Pylons.
        # # It can be safely removed when the Flask migration is completed.
        self.config[u'global_conf'] = self.parser.defaults().copy()

        self._update_config()

        loaded_files = [use_config_path]

        while True:
            schema, path = self.parser.get(self.section, u'use').split(u':')
            if schema == u'config':
                use_config_path = os.path.join(
                    os.path.dirname(os.path.abspath(use_config_path)), path)
                # Avoid circular references
                if use_config_path in loaded_files:
                    chain = ' -> '.join(loaded_files + [use_config_path])
                    raise CkanConfigurationException(
                        'Circular dependency located in '
                        f'the configuration chain: {chain}'
                    )
                loaded_files.append(use_config_path)

                self._read_config_file(use_config_path)
                self._update_config()
            else:
                break
        log.debug(
            u'Loaded configuration from the following files: %s',
            loaded_files
        )

    def get_config(self):
        return self.config.copy()