def __getitem__(self, path): if not isinstance(path, six.string_types): raise TypeError('arg 1 must be a string') path = Path(path) if path.is_root and self.is_root: return self if path.is_absolute and not self.is_root: raise InvalidPathError( 'Absolute paths can only be used on the root Group.' ) group = self for part in path.get_parts(): try: group = group.elements[part] except KeyError: raise InvalidPathError( 'Invalid path %s: element %s does not exist.' % (path, part) ) except AttributeError: raise InvalidPathError( 'Invalid path %s: element %s is not a Group.' % (path, part) ) if not isinstance(group, Group): raise InvalidPathError( 'Invalid path %s: element %s is not a Group.' % (path, part) ) return group
def move(self, path, position=-1, before=None, after=None): position = 0 if (before or after) else position path = Path(path) if not path.is_absolute: raise InvalidPathError( 'Invalid path %s: only absolute paths are allowed.' % path) target_group = self.order[str(path)] if target_group == self: raise InvalidPathError( 'Invalid path %s: cannot move Group into itself.' % path) target_group.insert(self.name, position=position, before=before, after=after)
def get(self, path): self.folder_ent.refresh() # Always up to date from scrunch.order import Path node = self for p_name in Path(path).get_parts(): try: node = node.get_child(p_name) except KeyError: raise InvalidPathError('Subfolder not found %s' % p) return node
def __init__(self, path): if not isinstance(path, six.string_types): raise TypeError('The path must be a string object') if six.PY2: regex_match = re.match(NAME_REGEX, path.decode('utf-8')) else: regex_match = re.match(NAME_REGEX, path) if not regex_match: raise InvalidPathError( 'Invalid path %s: it contains invalid characters.' % path) self.path = path
def get_child(self, name): by_name = self.folder_ent.by('name') by_alias = self.folder_ent.by('alias') # If found by alias, then it's a variable, return the variable if name in by_alias: return self.root.dataset[name] elif name in by_name: # Found by name, if it's not a folder, return the variable tup = by_name[name] if tup.type != 'folder': return self.root.dataset[name] return Folder(tup.entity, self.root, self) # Not a folder nor a variable path = self.path_pieces() + [name] raise InvalidPathError('Invalid path: | %s' % ' | '.join(path))
def _position_items(self, new_items, position, before, after): if before is not None or after is not None: # Before and After are strings target = before or after position = [x for x, c in enumerate(self.children) if c.alias == target] if not position: raise InvalidPathError("No child with name %s found" % target) position = position[0] if before is not None: position = position if position > 0 else 0 else: max_pos = len(self.folder_ent.graph) position = (position + 1) if position < max_pos else max_pos if position is not None: new_urls = {c.url for c in new_items} children = [c for c in self.children if c.url not in new_urls] for item in reversed(new_items): children.insert(position, item) return children return self.children # Nothing happened
def place(self, entity, path, position=-1, before=None, after=None): """ place an entity into a specific place in the order hierarchy """ position = 0 if (before or after) else position path = Path(path) if not path.is_absolute: raise InvalidPathError( 'Invalid path %s: only absolute paths are allowed.' % path ) target_group = self.group[str(path)] if isinstance(entity, scrunch.datasets.Variable): element = entity.alias elif isinstance(entity, scrunch.datasets.BaseDataset): element = entity.id else: raise TypeError('entity must be a `Variable` or `Dataset`') target_group.insert( element, position=position, before=before, after=after)