def _parse_contents(content, parser): is_attr = False is_node = False res = None for c in content: if ':' in c: outs = c.split(':') if len(outs) != 2: parser.error('incorrect format for content "%s"' % c) if is_attr: parser.error('cannot mix node-type contents ' 'with attribute-type contents') is_node = True if res is None: res = OrderedDict() outs = map(lambda x: x.strip(), outs) key, value = outs if key not in res: res[key] = value else: parser.error('duplicate name "%s" for node-type content' % key) else: if is_node: parser.error('cannot mix node-type contents ' 'with attribute-type contents') is_attr = True if res is None: res = [] res.append(c) if isinstance(res, list): if len(res) == 1: res = res[0] return res
def do_add_path(bucket, args, parser): root = bucket.root indexer = Indexer( elem=root, parser=parser, names=args.names, unordered=True, cache_lines=False, no_output=True, ) elem = indexer.get() path = eval_path(args.path) name = args.name or os.path.basename(path) content = OrderedDict([('path', path)]) elem.add(name, content) print('file "%s" added to %s "%s"' % (name, elem.type.lower(), elem))
def get_elems(self, elem_type=None): """ The generic method to get elements. :param elem_type: the element type. If None, then all types are included. Otherwise, it is one of "file", "directory", "node", and "attribute". """ elems = self.elems if elem_type is not None: items = self.elems.items() if elem_type not in ('file', 'directory', 'node', 'attribute'): raise NodeError('unknown element type') elem_class = getattr(sys.modules[__name__], elem_type.title()) items = filter(lambda x: isinstance(x[1], elem_class), items) elems = OrderedDict(items) return elems
def elems(self): """ Get elements of this node. """ if self._elems is None: this_is_dir = False this_is_file = False self._elems = OrderedDict() if not isinstance(self.content, dict): raise NodeError('invalid content format') for name in self.content: content = self.content[name] elem, this_is_dir, this_is_file = self._make_elem( name, content) self._elems[name] = elem self._update_class(this_is_dir, this_is_file) self._vals = self._elems.values() return self._elems