def _copy_with_new_properties(self, getnew, properties, on_error): newdic = OrderedDict(self._dic) for prop in properties: try: newprop = getnew(prop) except ConfigError as error: on_error(error) continue newdic[newprop.key] = newprop return self.__class__(newdic.values())
class ConfigBuilder(object): def __init__(self): self.models = OrderedDict() def __getitem__(self, key): return self.models.setdefault(key, _PropertyModel(key)) def properties(self): return itertools.chain(*(_PropertyModel.model_family_to_properties(m) for m in self.models.values())) def to_configuration(self): return Configuration.from_properties(self.properties())
class ConfigBuilder(object): def __init__(self): self.models = OrderedDict() def __getitem__(self, key): return self.models.setdefault(key, _PropertyModel(key)) def properties(self): return itertools.chain( *(_PropertyModel.model_family_to_properties(m) for m in self.models.values())) def to_configuration(self): return Configuration.from_properties(self.properties())
def __init__(self, properties=()): dic = OrderedDict((p.key, p) for p in properties) sortedkeys = sorted(dic, key=lambda k: Key(k).normal) inherit = _InheritanceViewer(dic) for key in sortedkeys: dic[key] = inherit.property_with_inherited_attributes(key) self._dic = dic
def from_configparser(filepath): """Have an ini file that the python configparser can understand? Pass the filepath to this function, and a matching Configuration will magically be returned.""" if not os.path.exists(filepath): logging.error(_('configuration file not found: %(filepath)s'), {'filepath': filepath}) return None if not os.path.isfile(filepath): logging.error(_('configuration path is not a file: %(filepath)s'), {'filepath': filepath}) return None try: from configparser import ConfigParser except ImportError: from backport.configparser import ConfigParser cfgp = ConfigParser() with open(filepath, encoding='utf-8') as fp: cfgp.readfp(fp) dic = OrderedDict() for section_name in cfgp.sections(): if 'DEFAULT' == section_name: section_name = '' for name, value in cfgp.items(section_name): value += '' # inner workaround for python 2.6+ # transforms ascii str to unicode because # of unicode_literals import dic[Key(section_name) + name] = value return Configuration.from_mapping(dic)
def enumerate_fs_with_db(self, startpath, itemfactory=None): ''' Starting at `startpath`, enumerates path items containing representations for each path as it exists in the filesystem and the database, respectively. `startpath` and `basedir` need to be absolute paths, with `startpath` being a subtree of `basedir`. However, no checks are being promised to enforce the latter requirement. Iteration is depth-first, but each path is returned before its children are determined, to enable recursive corrective action like deleting a whole directory from the database at once. Accordingly, the first item to be returned will represent `startpath`. This item is guaranteed to be returned, even if `startpath` does not exist in filesystem and database; all other items will have at least one existing representation. `basedir`, should it happen to equal `startpath`, will be returned as an item. It is up to the caller to properly deal with it. Each item has the following attributes: `infs`, a File object representing the path in the filesystem; `indb`, a File object representing the path in the database; and `parent`, the parent item. All three can be None, signifying non-existence. It is possible to customize item creation by providing an `itemfactory`. The argument must be a callable with the following parameter signature:: itemfactory(infs, indb, parent [, optional arguments]) and must return an object satisfying the above requirements for an item. ''' from backport.collections import OrderedDict basedir = cherry.config['media.basedir'] startpath = os.path.normcase(startpath).rstrip(os.path.sep) Item = itemfactory if Item is None: from collections import namedtuple Item = namedtuple('Item', 'infs indb parent') assert os.path.isabs(startpath), _('argument must be an abolute path: "%s"') % startpath assert startpath.startswith(basedir), _('argument must be a path in basedir (%s): "%s"') % (basedir, startpath) if not os.path.exists(startpath): fsobj = None elif startpath == basedir: fsobj = File(basedir) elif startpath > basedir: pathparent, pathbase = os.path.split(startpath) fsparent = self.db_find_file_by_path(pathparent, create=True) assert fsparent is not None, _('parent path not in database: %r') % pathparent fsobj = File(pathbase, fsparent) del pathparent, pathbase, fsparent else: assert False, _("shouldn't get here! (argument path not in basedir)") dbobj = self.db_find_file_by_path(startpath) stack = deque() stack.append(Item(fsobj, dbobj, None)) while stack: item = stack.pop() yield item dbchildren = {} if item.indb: dbchildren = OrderedDict(( (f.basename, f) for f in self.fetch_child_files(item.indb) )) if item.infs and item.infs.isdir: for fs_child in File.inputfilter(item.infs.children()): db_child = dbchildren.pop(fs_child.basename, None) stack.append(Item(fs_child, db_child, item)) for db_child in dbchildren.values(): stack.append(Item(None, db_child, item)) del dbchildren
def __init__(self): self.models = OrderedDict()