Пример #1
0
    def iterate(self, compmgr=None, defaults=True):
        """Iterate over the options in this section.

        If `compmgr` is specified, only return default option values for
        components that are enabled in the given `ComponentManager`.
        """
        options = set()
        name_str = self.name
        for setting in ProductSetting.select(self.env,
                                             where={'product': self.product,
                                                    'section': name_str}):
            option = self.optionxform(setting.option)
            options.add(option)
            yield option
        for parent in self.config.parents:
            for option in parent[self.name].iterate(defaults=False):
                loption = self.optionxform(option)
                if loption not in options:
                    options.add(loption)
                    yield option
        if defaults:
            for section, option in Option.get_registry(compmgr).keys():
                if section == self.name and \
                        self.optionxform(option) not in options:
                    yield option
Пример #2
0
    def iterate(self, compmgr=None, defaults=True):
        """Iterate over the options in this section.

        If `compmgr` is specified, only return default option values for
        components that are enabled in the given `ComponentManager`.
        """
        options = set()
        name_str = self.name
        for setting in ProductSetting.select(self.env,
                                             where={
                                                 'product': self.product,
                                                 'section': name_str
                                             }):
            option = self.optionxform(setting.option)
            options.add(option)
            yield option
        for parent in self.config.parents:
            for option in parent[self.name].iterate(defaults=False):
                loption = self.optionxform(option)
                if loption not in options:
                    options.add(loption)
                    yield option
        if defaults:
            for section, option in Option.get_registry(compmgr).keys():
                if section == self.name and \
                        self.optionxform(option) not in options:
                    yield option
Пример #3
0
    def get(self, key, default=''):
        """Return the value of the specified option.

        Valid default input is a string. Returns a string.
        """
        key = self.optionxform(key)
        cached = self._cache.get(key, _use_default)
        if cached is not _use_default:
            return cached
        name_str = self.name
        key_str = to_unicode(key)
        settings = ProductSetting.select(self.env, 
                where={'product':self.product, 'section':name_str,
                        'option':key_str})
        if len(settings) > 0:
            value = settings[0].value
        else:
            for parent in self.config.parents:
                value = parent[self.name].get(key, _use_default)
                if value is not _use_default:
                    break
            else:
                if default is not _use_default:
                    option = Option.registry.get((self.name, key))
                    value = option.default if option else _use_default
                else:
                    value = _use_default
        if value is _use_default:
            return default
        if not value:
            value = u''
        elif isinstance(value, basestring):
            value = to_unicode(value)
        self._cache[key] = value
        return value
Пример #4
0
 def _write(self, lines, product=None):
     r"""Override superclass method by writing configuration values
     to the database rather than ini file in the filesystem.
     """
     if product is None:
         product = self.default_product
     product = to_unicode(product)
     fp = StringIO(('\n'.join(lines + [''])).encode('utf-8'))
     parser = ConfigParser()
     parser.readfp(fp, 'bh-product-test')
     with self.env.db_transaction as db:
         # Delete existing setting for target product , if any
         for setting in ProductSetting.select(self.env, db,
                 {'product' : product}):
             setting.delete()
         # Insert new options
         for section in parser.sections():
             option_key = dict(
                     section=to_unicode(section),
                     product=to_unicode(product)
                 )
             for option, value in parser.items(section):
                 option_key.update(dict(option=to_unicode(option)))
                 setting = ProductSetting(self.env)
                 setting._data.update(option_key)
                 setting._data['value'] = to_unicode(value)
                 setting.insert()
Пример #5
0
 def _write(self, lines, product=None):
     r"""Override superclass method by writing configuration values
     to the database rather than ini file in the filesystem.
     """
     if product is None:
         product = self.default_product
     product = to_unicode(product)
     fp = StringIO(('\n'.join(lines + [''])).encode('utf-8'))
     parser = ConfigParser()
     parser.readfp(fp, 'bh-product-test')
     with self.env.db_transaction as db:
         # Delete existing setting for target product , if any
         for setting in ProductSetting.select(self.env, db,
                                              {'product': product}):
             setting.delete()
         # Insert new options
         for section in parser.sections():
             option_key = dict(section=to_unicode(section),
                               product=to_unicode(product))
             for option, value in parser.items(section):
                 option_key.update(dict(option=to_unicode(option)))
                 setting = ProductSetting(self.env)
                 setting._data.update(option_key)
                 setting._data['value'] = to_unicode(value)
                 setting.insert()
Пример #6
0
    def _dump_settings(self, config):
        product = config.product
        fields = ('section', 'option', 'value')
        rows = [tuple(getattr(s, f, None) for f in fields) for s in
                ProductSetting.select(config.env, where={'product' : product})]

        dump = []
        for section, group in groupby(sorted(rows), lambda row: row[0]):
            dump.append('[%s]\n' % (section,))
            for row in group:
                dump.append('%s = %s\n' % (row[1], row[2]))
        return dump
Пример #7
0
    def _dump_settings(self, config):
        product = config.product
        fields = ('section', 'option', 'value')
        rows = [
            tuple(getattr(s, f, None) for f in fields)
            for s in ProductSetting.select(config.env,
                                           where={'product': product})
        ]

        dump = []
        for section, group in groupby(sorted(rows), lambda row: row[0]):
            dump.append('[%s]\n' % (section, ))
            for row in group:
                dump.append('%s = %s\n' % (row[1], row[2]))
        return dump
Пример #8
0
    def get(self, key, default=''):
        """Return the value of the specified option.

        Valid default input is a string. Returns a string.
        """
        key = self.optionxform(key)
        cached = self._cache.get(key, _use_default)
        if cached is not _use_default:
            return cached
        name_str = self.name
        key_str = to_unicode(key)
        settings = ProductSetting.select(self.env,
                                         where={
                                             'product': self.product,
                                             'section': name_str,
                                             'option': key_str
                                         })
        if len(settings) > 0:
            value = settings[0].value
        else:
            for parent in self.config.parents:
                value = parent[self.name].get(key, _use_default)
                if value is not _use_default:
                    break
            else:
                if default is not _use_default:
                    option = Option.registry.get((self.name, key))
                    value = option.default if option else _use_default
                else:
                    value = _use_default
        if value is _use_default:
            return default
        if not value:
            value = u''
        elif isinstance(value, basestring):
            value = to_unicode(value)
        self._cache[key] = value
        return value