def deep_copy(old_dict, parent=None, depth=None, main=None): """Return a deep copy of a ConfigObj""" # Is this a copy starting from the top level? if isinstance(old_dict, configobj.ConfigObj): new_dict = configobj.ConfigObj( '', encoding=old_dict.encoding, default_encoding=old_dict.default_encoding, interpolation=old_dict.interpolation) else: # No. It's a copy of something deeper down. If no parent or main is given, then # adopt the parent and main of the incoming dictionary. new_dict = configobj.Section( parent if parent is not None else old_dict.parent, depth if depth is not None else old_dict.depth, main if main is not None else old_dict.main) for entry in old_dict: # Avoid interpolation by using the version of __getitem__ from dict old_value = dict.__getitem__(old_dict, entry) if isinstance(old_value, configobj.Section): new_value = deep_copy(old_value, new_dict, new_dict.depth + 1, new_dict.main) elif isinstance(old_value, list): # Make a copy new_value = list(old_value) elif isinstance(old_value, tuple): # Make a copy new_value = tuple(old_value) else: # It's a scalar new_value = old_value new_dict[entry] = new_value return new_dict
def config_update_from_argument_list(self, argument_list): """Intended for use in parsing command line options""" for arg in argument_list: match = argMatchRe.match(arg) if not match: raise BadOptionFormat(arg) section, option, value = match.groups() subtree = self.config for subsection in section.split('.'): if not subtree.has_key(subsection): subtree[subsection] = configobj.Section( subtree, subtree.depth + 1, self.config) subtree = subtree[subsection] subtree[option] = value
def deepcopy(obj): """Copy the section""" if type(obj) == configobj.Section: new_obj = configobj.Section(obj.parent, obj.depth, obj.main) elif type(obj) == configobj.ConfigObj: new_obj = configobj.ConfigObj() else: sys.exit("Copy for {0} not implemented".format(type(obj))) for scalar in obj.scalars: new_obj[scalar] = obj[scalar] for section in obj.sections: new_obj[section] = deepcopy(obj[section]) new_obj.sections = list(obj.sections) new_obj.scalars = list(obj.scalars) return new_obj
def deep_copy(old_dict, parent=None, depth=None, main=None): """Return a deep copy of a ConfigObj""" # Is this a copy starting from the top level? if isinstance(old_dict, configobj.ConfigObj): new_dict = configobj.ConfigObj( '', encoding=old_dict.encoding, default_encoding=old_dict.default_encoding, interpolation=old_dict.interpolation, indent_type=old_dict.indent_type) new_dict.initial_comment = list(old_dict.initial_comment) else: # No. It's a copy of something deeper down. If no parent or main is given, then # adopt the parent and main of the incoming dictionary. new_dict = configobj.Section( parent if parent is not None else old_dict.parent, depth if depth is not None else old_dict.depth, main if main is not None else old_dict.main) for entry in old_dict: # Avoid interpolation by using the version of __getitem__ from dict old_value = dict.__getitem__(old_dict, entry) if isinstance(old_value, configobj.Section): new_value = deep_copy(old_value, new_dict, new_dict.depth + 1, new_dict.main) elif isinstance(old_value, list): # Make a copy new_value = list(old_value) elif isinstance(old_value, tuple): # Make a copy new_value = tuple(old_value) else: # It's a scalar, possibly a string new_value = old_value new_dict[entry] = new_value # A comment is a list of strings. We need to make a copy of the list, but the strings # themselves are immutable, so we don't need to copy them. That means a simple shallow # copy will do: new_dict.comments[entry] = list(old_dict.comments[entry]) # An inline comment is either None, or a string. Either way, they are immutable, so # a simple assignment will work: new_dict.inline_comments[entry] = old_dict.inline_comments[entry] return new_dict
def get_section(cfgspec, config, config_widget): section_name = cfgspec.name self.verbose('section %s', pathname(cfgspec)) for name, dict_or_widget in list(config_widget.items()): if not isinstance(dict_or_widget, dict): option_name = name self.verbose('option %s', pathname(cfgspec, option_name)) if dict_or_widget.checkbox and not dict_or_widget.checkbox.checkState( ): self.verbose('option %s ignored, state is disabled', pathname(cfgspec, option_name)) continue if option_name not in cfgspec: for manyname in ('__many__', '___many___'): if not isinstance(cfgspec.get(manyname, None), (dict, type(None))): optspec = cfgman.getspec(cfgspec, manyname) break else: self.error( 'many option %s ignored, specification not found', pathname(cfgspec, option_name)) continue else: optspec = cfgman.getspec(cfgspec, option_name) optionvalue = get_widget(optspec, dict_or_widget) config[option_name] = optionvalue optiontype = optionvalue.__class__.__name__ self.debug('option %s = %r (%s)', pathname(cfgspec, option_name), optionvalue, optiontype) else: section_name = name if section_name not in cfgspec: for manyname in ('__many__', '___many___'): if isinstance( cfgspec.get(manyname, None), (configobj.ConfigObj, configobj.Section)): secspec = cfgspec[manyname] break else: self.error( 'many section %s ignored, specification not found', pathname(cfgspec, section_name)) continue else: secspec = cfgspec[section_name] config[section_name] = configobj.Section(config, config.depth + 1, main_config, indict={}, name=section_name) get_section(secspec, config[section_name], dict_or_widget) if not len(config[section_name]): self.verbose('remove empty section %s', pathname(cfgspec, section_name)) config.pop(section_name)
def _new_section(self, parent, level): """Creates a new section Mapping """ return configobj.Section(parent, level, self.base)