def set(self, key, value): """Change a configuration value. These changes will be persistent right away. """ key_str = self.optionxform(key) value_str = to_unicode(value) self._cache.pop(key_str, None) option_key = { 'product': self.product, 'section': self.name, 'option': key_str, } try: setting = ProductSetting(self.env, option_key) except ResourceNotFound: if value is not None: # Insert new record in the database setting = ProductSetting(self.env) setting._data.update(option_key) setting._data['value'] = value_str self.env.log.debug('Writing option %s', setting._data) setting.insert() else: if value is None: # Delete existing record from the database # FIXME : Why bother with setting overriden self.overridden[key] = True setting.delete() else: # Update existing record setting._data['value'] = value setting.update()
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()
def remove(self, key): """Delete a key from this section. Like for `set()`, the changes won't persist until `save()` gets called. """ key_str = self.optionxform(key) option_key = { 'product': self.product, 'section': self.name, 'option': key_str, } try: setting = ProductSetting(self.env, keys=option_key) except ResourceNotFound: self.env.log.warning("No record for product option %s", option_key) else: self._cache.pop(key, None) setting.delete() self.env.log.info("Removing product option %s", option_key)