def _gen(self, node, path, prefix): global args tail, heads = self._name_node(path), [] edges = node.sufs.keys() sort = args.sort == "all" or (args.sort == "root" and not path) if sort: edges.sort() for i, edge in enumerate(edges): child = node.sufs[edge] path.append(i) prefix.append(edge) self._gen_node(child, path, "".join(prefix)) head = self._name_node(path) if heads and sort: self._gen_edge(heads[-1], head, style="invisible", dir="none") heads.append(head) edge_label = [cgi.escape(edge)] edge_attrs = {'label': edge_label, 'tooltip': edge_label} if len(path) == 1: root = self._name_node([i + len(node.sufs)]) self._print(root, self._stringify_attrs(style="invisible")) self._gen_edge(root + ":e", head + ":w", **edge_attrs) else: self._gen_edge(tail, head, **edge_attrs) self._gen(child, path, prefix) path.pop() prefix.pop() if sort and heads: self._print("{ rank=same", " ".join(head for head in heads), "}")
def url2operationsNew(url, base): # http://localhost:8080/image_service/00-cQF6rVVAs3iYXWt4Xdh2i/sub/2/@/slice:,,1,1/tile:2,26,9,512/depth:8,d,u,cs/format:jpeg subpath = None query = [] path = filter(None, url.split('/')) # split and remove empty path = path[path.index(base)+1:] # isolate query part if path[0].lower() in ['image', 'images']: path = path[1:] resource_id = path.pop(0) # parse subpath if '@' in path: sp = [] p = path.pop(0) while p != '@' and p is not None: sp.append(unquote(p)) p = path.pop(0) if len(sp)>0: subpath = '/%s'%('/'.join(sp)) for p in path: v = p.split(':', 1) name = unquote(v[0].replace('+', ' ')) try: value = unquote(v[1].replace('+', ' ')) except IndexError: value = '' query.append((name, value)) return resource_id, subpath, query
def get_folder_subpath(self, path): path = list(path) folder = [] while len(path) > 1: folder.insert(0, self.model[tuple(path)][self.M_KEY]) path.pop(-1) return '/'.join(folder)
def _read_structure_lazy(infile=None, include_hosts=True): '''Determine and return the organizational structure from a given host file. ''' path, hosts, structure = [], [], [] lines = _read_lines_lazy(infile) for line in lines: if not _iscomment(line): if include_hosts: hosts.append(line) continue match = _match_open.search(line) if match: path.append(match.group(1)) continue match = _match_close.search(line) if match: if include_hosts: yield (list(path), list(hosts)) hosts = [] else: yield list(path) path.pop() return
def format_path(path): if modules_only: path = path.split('::') if len(path[-1]) == 0 or path[-1][0].isupper(): path.pop() path = '::'.join(path) return path
def _dfs(node, plain_text, math_text, path=None, in_math=None): path = path or [] path.append(node) in_math = in_math or False is_plain_text = isinstance(node, plasTeX.DOM.Text) text = node.lower() if is_plain_text else (node.nodeName + ' ') #print('{indent} {nodeType} {in_math} {is_plain_text} "{nodeRepr}" "{nodeName}"'.format( # indent=' ' * len(path), # nodeType=type(node), # in_math=in_math, # is_plain_text=is_plain_text, # nodeRepr=repr(node), # nodeName=repr(node.nodeName) # ) # ) if in_math: math_text.append(text) elif is_plain_text: plain_text.append(text) elif node.nodeName == 'bgroup': # add spaces between groups, so that they are separated later when # we join the tokens plain_text.append(' ') math_text.append(' ') elif node.nodeName == 'math': math_text.append(' ') in_math = True for child in node.childNodes: _dfs(child, plain_text, math_text, path, in_math) path.pop()
def get_item_by_path(self, path): path = path.split(os.path.sep) if path[0] == '': curr = self.root_item path = path[1:] else: curr = self.cwd_item while len(path) > 0 and path[-1] == '': path.pop() for p in path: if p == '.': pass #curr = curr elif p == '..': if curr['parent_pos'] > 0: curr = self.get_item_by_pos(curr['parent_pos']) else: found = None self.read_item_data(curr, get_subitem_list=True) for sub_pos in curr['subitem_list']: sub = self.get_item_by_pos(sub_pos) self.read_item_data(sub, get_name=True) if sub['name'] == p: found = sub_pos break if not found: raise FileNotFoundError(2, path) curr = self.get_item_by_pos(found) return curr
def constructAlignment(path, source, str1, str2): s1, s2 = "","" space = "" i1, i2 = 0,0 arrow = path.pop() while (arrow != 0): if (arrow == 1): #Advance str2 but not str1 so deletion s1 += '-' s2 += str2[i2] i2 += 1 space += '|' elif (arrow == 2): #Advance str1 but not str2 so insertion s1 += str1[i1] i1 += 1 s2 += '-' space += '|' elif (arrow == 3): #Both strings advance so match/mismatch if str1[i1] != str2[i2]: space += '*' else: space += ' ' s1 += str1[i1] i1 += 1 s2 += str2[i2] i2 += 1 arrow = path.pop() result = "Alignment of " + source + ':\n' + s1 + '\n' + space + '\n' + s2 return result
def format_term_match(path): # When a term matches, we get a path from a term in a document that is # included in search results, through term relationships, to a term that # actually matched the search query, which might be in another document. # # The data structure is like this: # [ # ("term", { resource }, None), # ... # ("term", { resource }, "defined-by"), # ("term", { resource }, "defined-by"), # ] # Where each relationship (e.g. "defined-by") indicates the relationship # between the tuple entry in which it occurs and the previous tuple entry. # The first one is always None because there is no previous entry. # # The output is something like: # # X [term is defined by term "Y" in document "Z", which is defined by # ....] ret, base_document, dummy = path.pop(0) # When there is more than one entry, then indicate the path as contexual # info. first = True if len(path) > 0: ret += " <span class=\"from-cited-document\">" while len(path) > 0: # Some initial static text. if first: ret += "term " # as in, "term is defined by..." first = False else: ret += ", which " # Get the next item. context, document, relation = path.pop(0) # How to describe this relation? if relation == "defined-by": relation_descr = "is defined by" else: relation_descr = "has same meaning as" # More text. ret += html.escape(relation_descr + " term “") + context + html.escape("”") # Only show "in <xxx document>" starting when an element in # the path is not in the same document as the first element's # document. i.e. So long as the path remains in the first # document, there is no need to be explicit about what document # the term appears in. if document != base_document: ret += " in " + html.escape(document.get("short-title", document["id"])) base_document = None # show the document in all future elements ret += "</span>" return ret
def getFixedUpPath(self): "return the fixed up path so we can use it for traversal" url = self.REQUEST.URLPATH0.split('/') cpath = self.absolute_url_path().split('/') path = url[len(cpath):] if path[0] == 'index_html': path.pop() return path
def get_L_groundtruth(self): from sys import path import os dir = os.path.abspath(self.datadir) path.insert(0, dir) # make the configuration available, temporarilly from config import cfg as cfg_gt path.pop(0) # restore the path again return cfg_gt["world"]["L"]
def _build_item_paths(self, leaf, path): if 'children' in leaf and len(leaf['children']) > 0: path.append(leaf) for name, child_leaf in leaf['children'].items(): self._build_item_paths(child_leaf, path) path.pop() elif leaf['type'] != LeafType.ROOT: self._tree_path[leaf['item']] = path + [leaf]
def searchPath(graph, point, path): for i in graph: if point == i[0]: p = i if p[0] == graph[-1][0]: return path for link in p[1]: path.append(link) finalPath = searchPath(graph, link, path) if finalPath != None: return finalPath else: path.pop()
def explore(obj, path, ids, errs): """ :param obj: object (scalar or complex type) we're traversing :param path: key path we took to get to obj e.g. `{foo:{bar:1}` would result in path of [foo,bar] when obj=1 :param ids: id values that have already been seen in this traversal :param errs: any errors we've seen so far. modifies this as an "out" parameter """ if isinstance(obj, dict): for key in iter(obj.keys()): path.append(key) if not isinstance( key, _VALID_KEY_TYPES) or not _VALID_KEY_REX.match(key): errs.append({ 'err_type': 'Invalid Key', 'src_file': src_file, 'item': key, 'item_type': type(key), 'path': copy.deepcopy(path), }) if key == 'id': validate_id(obj[key], path, ids, errs, src_file) if key == 'use_journal_mnt': errs.append({ 'err_type': 'Deprecated Key (Use `journal_dir: null` instead.)', 'src_file': src_file, 'item': key, 'item_type': type(key), 'path': copy.deepcopy(path), }) explore(obj[key], path, ids, errs) path.pop() elif isinstance(obj, list): index = 0 for item in iter(obj): path.append(index) explore(item, path, ids, errs) path.pop() index = index + 1 elif not isinstance(obj, _VALID_SCALAR_TYPES): errs.append({ 'err_type': 'Invalid Value Type', 'src_file': src_file, 'item': obj, 'item_type': type(obj), 'path': copy.deepcopy(path) }) elif obj in _INVALID_SCALAR_VALUES: errs.append({ 'err_type': 'Invalid Value', 'src_file': src_file, 'item': obj, 'item_type': type(obj), 'path': copy.deepcopy(path) })
def traverse_element(item, course_id, path, course, output_dir): if item["Type"] == 1: download_file(item, path, course, output_dir) else: path.append(item) download_module(item, path, course, output_dir) for sub_element in get_module(item["Id"], course_id): traverse_element(sub_element, course_id, path, course, output_dir) path.pop()
def visit(self, callback, path=None, *args, **kw): if path is None: path = [self.board] #print 'visiting', path[-1] callback(path, *args, **kw) for c in path[-1].children: if isinstance(c, BaseElement): path.append(c) self.visit(callback, path, *args, **kw) path.pop(-1)
def _get_videos(self, soup, serie): for a in soup.select(self.index['selector']['videos']): path = a['href'].split('/') vid = path.pop() if vid == '': vid = path.pop() self.videos[vid] = { 'url': self._ccma_url(a['href']), 'title': a.text.strip() }
def path_from_filename(filename, source_dir): filename = filename.replace(source_dir, '').strip(os.sep) path = filename.split(os.sep) if path[-1] == 'mod.rs': path.pop() elif path[-1].endswith('.rs'): path[-1] = path[-1][:-3] return '::'.join(['crate'] + path)
def ensure_dir_exists(path): path = path.split('/') path.pop() if len(path) == 0: return pwd = '' for _dir in path: pwd += f"{_dir}/" if not os.path.exists(pwd): os.mkdir(pwd)
def installDependencies(self): path = os.__file__.split('\\') path.pop() path.pop() path.append('python') path = '\\'.join(path) command1 = '%s -m pip install opencv-python --user' % (path) command2 = '%s -m pip install pyrsgis --user' % (path) os.system(command1) os.system(command2)
def format_path(path, modules_only): """ Formats Rust paths. :param path: A Rust path delimited by `::` :param modules_only: Whether to cut off the last component of the path :return: """ if modules_only: path = path.split('::') if len(path[-1]) == 0 or path[-1][0].isupper(): path.pop() path = '::'.join(path) return path
def tree(*pipeline, validate=False): """ Return a tree of XIST nodes from the event stream :obj:`pipeline`. :obj:`pipeline` must output only events that contain XIST nodes, i.e. the event types ``"xmldeclnode"``, ``"doctypenode"``, ``"commentnode"``, ``"textnode"``, ``"enterelementnode"``, ``"leaveelementnode"``, ``"procinstnode"`` and ``"entitynode"``. If :obj:`validate` is true, the tree is validated, i.e. it is checked if the structure of the tree is valid (according to the :obj:`model` attribute of each element node), if no undeclared elements or attributes have been encountered, all required attributes are specified and all attributes have allowed values. The node returned from :func:`tree` will always be a :class:`Frag` object. Example:: >>> from ll.xist import xsc, parse >>> from ll.xist.ns import xml, html, chars >>> doc = parse.tree( ... parse.URL("http://www.python.org/"), ... parse.Tidy(), ... parse.NS(html), ... parse.Node(pool=xsc.Pool(xml, html, chars)) ... ) >>> doc[0] <element ll.xist.ns.html.html xmlns='http://www.w3.org/1999/xhtml' (7 children/3 attrs) location='https://www.python.org/:?:?' at 0x110a4ecd0> """ path = [xsc.Frag()] for (evtype, node) in events(*pipeline): if evtype == "enterelementnode": path[-1].append(node) path.append(node) elif evtype == "leaveelementnode": if validate: for warning in node.validate(False, path): warnings.warn(warning) path.pop() else: path[-1].append(node) if validate: for warning in node.validate(False, path): warnings.warn(warning) return path[0]
def resolve_select(self, element, select): if select is None: return element, None path = select.split("/") while path: p0 = path.pop(0) if p0 == "": newelement = element.getroottree().getroot() p0 = path.pop(0) continue else: newelement = element.xpath("*[@ctx-id=$ctxid]", ctxid=p0) if newelement: newelement = newelement[0] else: newelement = None toupdate = element.xpath("*[not(@ctx-id)]") for e in toupdate: self.xfinal.apply_one_id(e) if e.get("ctx-id") == p0: newelement = e if newelement is None: # Intentar mas a fondo: m = re.match("(\w+)\[(\w+),(\d+)\]", p0) if m: tagname = m.group(1) number = int(m.group(3)) - 1 elist = element.xpath(tagname) try: newelement = elist[number] except Exception, e: self.iface.warn(e) newelement = None if newelement is None: # Intentar con nombre *muy* parecido: alternatives = element.xpath("*/@ctx-id") close_matches = difflib.get_close_matches( p0, alternatives, 1, 0.92) if close_matches: newelement = element.xpath("*[@ctx-id=$ctxid]", ctxid=close_matches[0]) if newelement: newelement = newelement[0] else: newelement = None if newelement is None: return element, "/".join([p0] + path) elif newelement.get("ctx-id") != p0: self.iface.info2("Seleccionamos %s cuando buscabamos %s" % (newelement.get("ctx-id"), p0)) element = newelement
def GetDirectory(drive, path): path = path.split('/') if len(path) > 0 and path[0] == '': path.pop(0) if len(path) > 0 and path[-1] == '': path.pop(-1) d = drive.CreateFile({'id': 'root'}) for c in path: files = drive.ListFile({'q': u"title='%s' and '%s' in parents and trashed=false and mimeType='application/vnd.google-apps.folder'" % (c, d.get('id'))}).GetList() if len(files) == 0: return None d = files[0] return d
def build_recipe_model (self, path, val): m = self.models[path] = self.new_model() searches = [{'column':'deleted','operator':'=','search':False}] path = path.split('>') while path: textval = path.pop() attr = path.pop() if val is None: val = None searches.append({'column':attr,'search':val,'operator':'='}) else: searches.append({'column':attr,'search':val}) for recipe in self.rd.search_recipes(searches): pb = get_recipe_image(recipe) m.append((recipe.id,recipe.title,pb,None))
def get_package(obj): """ Get the package name associated with an object """ if inspect.isclass(obj): path = obj.__module__ elif inspect.isfunction(obj): path = obj.__module__ elif inspect.ismodule(obj): path = obj.__name__ else: path = '' path = path.split('.') path.pop() return '.'.join(path)
def _process_path(self, path): """ Take a path and set the space and parent """ path = normalize_path(self.path).split("/") if not self.has_space(): spacePath = path.pop(0) try: self.space = Space.objects.get(path=spacePath) except ObjectDoesNotExist: # Space might not be included in path, add it back to path path.insert(0, spacePath) parentPath = path[0:-1] if len(path) >= 1: self.path = path[-1] else: self.path = "" parentPath = filter(len, parentPath) # remove empty path sections if parentPath: self.parent = Document.objects.get_by_path( parentPath, space=self.space, create=True)
def pathFinish(self): self.pathcounter -= 1 self.pathmode = 'path' if self.paths and (self.pathcounter < 1): path = self.paths.pop() if not path: pass elif len(path) == 1: term = path.pop() if self.mode and self.mode[-1] == 'list': self.lists[-1].append(term) else: self.triples[-1].append(term) else: # A path traversal objt, path = path[0], path[1:] for (i, pred) in enumerate(path): if (i % 2) != 0: subj = objt objt = BNode() if path[i - 1] == '!': self.triple(subj, pred, objt) elif path[i - 1] == '^': self.triple(objt, pred, subj) # @@ nested paths? if self.mode and self.mode[-1] == 'list': self.lists[-1].append(objt) else: self.triples[-1].append(objt)
def _read_xml(stream): document = XmlNode() current_node = document path = [] while not stream.atEnd(): stream.readNext() if stream.isStartElement(): node = XmlNode() attrs = stream.attributes() for i in xrange(attrs.count()): attr = attrs.at(i) node.attribs[_node_name(attr.name())] = unicode(attr.value()) current_node.append_child(_node_name(stream.name()), node) path.append(current_node) current_node = node elif stream.isEndElement(): current_node = path.pop() elif stream.isCharacters(): current_node.text += unicode(stream.text()) return document
def traverseContainer(self, container=None, path=None, url=None, queryDict=None): "used for the dynamic menu system" queryDict = queryDict or {} if path is None: path = self.getFixedUpPath() path.reverse() try: selected = path.pop() except IndexError: selected = None if url is None: url = self.absolute_url_path() if container is None: container = self.getOrigin() temp = [] if container is not None: containers = container.objectItems() containers = sorted((name, object) for name, object in containers if self.isOkay(name, object)) for name, object in containers: if name == selected: temp.append((os.path.join(url, name), name, 1, '', queryDict, '')) temp.append(self.traverseContainer(object, path, url=os.path.join(url, selected), queryDict=queryDict)) else: temp.append((os.path.join(url, name), name, 0, '', queryDict, '')) return temp
def solution(data, return_to_base=False): pois = {} open_spaces = set() for y, line in enumerate(data): for x, ch in enumerate(line): if ch != "#": open_spaces.add((x, y)) if ch in "0123456789": point = int(ch) pois[point] = (x, y) poi_dist = {} for src, dest in product(pois.keys(), repeat=2): sp = shortest_path(open_spaces, pois[src], pois[dest]) poi_dist[(src, dest)] = sp result = set() non_zero_pots = [x for x in pois.keys() if x != 0] for path in permutations(non_zero_pots): p = 0 path = list(path) steps = 0 while path: n = path.pop(0) dist = poi_dist[(p, n)] steps += dist p = n if return_to_base: steps += poi_dist[(p, 0)] result.add(steps) return min(result)
def cyclic(graph): """Return True if the directed graph has a cycle. The graph must be represented as a dictionary mapping vertices to iterables of neighbouring vertices. For example: >>> cyclic({1: (2,), 2: (3,), 3: (1,)}) True >>> cyclic({1: (2,), 2: (3,), 3: (4,)}) False """ visited = set() path = [object()] path_set = set(path) stack = [iter(graph)] while stack: for v in stack[-1]: if v in path_set: return True elif v not in visited: visited.add(v) path.append(v) path_set.add(v) stack.append(iter(graph.get(v, ()))) break else: path_set.remove(path.pop()) stack.pop() return False
def url2operationsOld(url, base): # http://localhost:8080/image_service/00-cQF6rVVAs3iYXWt4Xdh2i/sub/2?slice=,,1,1&tile=2,26,9,512&depth=8,d,u,cs&fuse=0,0,255;255,255,255;255,0,0;0,255,0;:m&format=jpeg resource_id = None subpath = None # process UUID and path path = filter(None, url.split('?', 1)[0].split('/')) # split and remove empty path = path[path.index(base)+1:] # isolate query part if path[0].lower() in ['image', 'images']: path = path[1:] resource_id = path.pop(0) if len(path)>0: path = [unquote(p) for p in path] subpath = '/%s'%('/'.join(path)) # process query string scheme, netloc, url, params, querystring, fragment = urlparse(url) #pairs = [s2 for s1 in querystring.split('&') for s2 in s1.split(';')] pairs = [s1 for s1 in querystring.split('&')] query = [] for name_value in pairs: if not name_value: continue nv = name_value.split('=', 1) if len(nv) != 2: nv.append('') name = unquote(nv[0].replace('+', ' ')) value = unquote(nv[1].replace('+', ' ')) query.append((name, value)) return resource_id, subpath, query
def _redact(self, target, path, text=None): """Replace the value of a key in `target`. The key can be at the top level by specifying a list with a single key as the path. Nested dictionaries are also supported by passing a list of keys to be navigated to find the one that should be replaced. In this case the last one is the one that will be replaced. :param dict target: the dictionary that may have a key to be redacted; modified in place :param list path: a list representing the nested structure in `target` that should be redacted; modified in place :param string text: optional text to use as a replacement for the redacted key. if text is not specified, the default text will be sha1 hash of the value being redacted """ key = path.pop() # move to the most nested dict for p in path: try: target = target[p] except KeyError: return if key in target: if text: target[key] = text elif target[key] is not None: # because in python3 byte string handling is ... ug value = target[key].encode('utf-8') sha1sum = hashlib.sha1(value) # nosec target[key] = "{SHA1}%s" % sha1sum.hexdigest()
def find_entry(self, path): if len(path) == 0: return None if path[0] == self.name: if len(path) == 1: return self else: path.pop(0) for i in self.entries: ret = i.find_entry(path) if ret is not None: return ret return None else: return None
def path_from_filename(filename, source_dir): """ Returns the `crate::` Rust path from a filename. :param filename: :param source_dir: :return: """ filename = filename.replace(source_dir, '').strip(os.sep) path = filename.split(os.sep) if path[-1] == 'mod.rs': path.pop() elif path[-1].endswith('.rs'): path[-1] = path[-1][:-3] return '::'.join(['crate'] + path)
def pathFinish(self): self.pathcounter -= 1 self.pathmode = 'path' if self.paths and (self.pathcounter < 1): path = self.paths.pop() if not path: pass elif len(path) == 1: term = path.pop() if self.mode and self.mode[-1] == 'list': self.lists[-1].append(term) else: self.triples[-1].append(term) else: # A path traversal objt, path = path[0], path[1:] for (i, pred) in enumerate(path): if (i % 2) != 0: subj = objt objt = BNode() if path[i-1] == '!': self.triple(subj, pred, objt) elif path[i-1] == '^': self.triple(objt, pred, subj) # @@ nested paths? if self.mode and self.mode[-1] == 'list': self.lists[-1].append(objt) else: self.triples[-1].append(objt)
def _make_route(r): output = [] for regex, target in r: rpath.append(regex) regex = re.compile(regex) if isinstance(target, (list, tuple)): if target in path: raise Exception('Loop found in route! Path: {}'.format(rpath)) path.append(target) target = _make_route(target) path.pop() elif not hasattr(target, '__call__'): raise Exception('Unsupported target type! Path: {}'.format(rpath)) rpath.pop() output.append((regex, target)) return output
def walk(self, folder, path=None): path = path or [] folders = [] db_nodes = [] path.append(folder.name) logger.info(f"Path: {path}") for node in self.get_nodes(folder): node.path = path if node.mimeType == FOLDER: folders.append(node) db_nodes.append(node) if db_nodes: Gphoto.objects.insert(db_nodes) for folder in folders: self.walk(folder, path) path.pop()
def normalize_splitted_path(splitted_path): """ given a list of steps forming a path (as result of split_nixpath() for example), it returns the path normalized so: - steps '.' are ignored - steps '..' remove previous non-'..' steps """ # get rid of '.' path = [step for step in splitted_path if step != '.'] if not path: return [''] # get rid of '..' result = [] while path: step = path.pop(0) if step == '..': if result: if result[-1] == '/': pass # do not remove root elif result[-1] == '..': result.append(step) else: result.pop() else: result.append(step) else: result.append(step) return result
def resolve_select(self, element, select): if select is None: return element, None path = select.split("/") while path: p0 = path.pop(0) if p0 == "": newelement = element.getroottree().getroot() p0 = path.pop(0) continue else: newelement = element.xpath("*[@ctx-id=$ctxid]",ctxid = p0) if newelement: newelement = newelement[0] else: newelement = None toupdate = element.xpath("*[not(@ctx-id)]") for e in toupdate: self.xfinal.apply_one_id(e) if e.get("ctx-id") == p0: newelement = e if newelement is None: # Intentar mas a fondo: m = re.match("(\w+)\[(\w+),(\d+)\]", p0) if m: tagname = m.group(1) number = int(m.group(3))-1 elist = element.xpath(tagname) try: newelement = elist[number] except Exception, e: self.iface.warn(e) newelement = None if newelement is None: # Intentar con nombre *muy* parecido: alternatives = element.xpath("*/@ctx-id") close_matches = difflib.get_close_matches(p0, alternatives, 1, 0.92) if close_matches: newelement = element.xpath("*[@ctx-id=$ctxid]",ctxid = close_matches[0]) if newelement: newelement = newelement[0] else: newelement = None if newelement is None: return element, "/".join([p0] + path) elif newelement.get("ctx-id") != p0: self.iface.info2("Seleccionamos %s cuando buscabamos %s" % (newelement.get("ctx-id"), p0)) element = newelement
def _path_to_node(self, path): node = self.root path = filter(len, path.split('/')) path.reverse() if len(path) and path[-1] == '~trash': node = self.trash path.pop() try: while path: part = path.pop() node = node[part] except KeyError: return None return node
def drag_receive_signal(self, widget, drag_context, x, y, selection_data, info, timestamp): """something was dropped on a tree row""" drop_info = self.tv.get_dest_row_at_pos(x, y) if drop_info: drop_row, pos = drop_info drop_iter = self.model.get_iter(drop_row) data = selection_data.data if selection_data.type == "tag-tree-row": paths = data.split("-") iters = [] for path in paths: iters.append(self.model.get_iter(path)) for it in iters: path = list(self.model.get_path(drop_iter)) rownum = path.pop() if self.model[it] < 2: continue ##gtk.TREE_VIEW_DROP_BEFORE, gtk.TREE_VIEW_DROP_AFTER, gtk.TREE_VIEW_DROP_INTO_OR_BEFORE or gtk.TREE_VIEW_DROP_INTO_OR_AFTER if self.model[drop_iter][self.M_TYPE] == 3: if pos in [gtk.TREE_VIEW_DROP_AFTER, gtk.TREE_VIEW_DROP_INTO_OR_AFTER]: rownum += 1 drop_iter = self.model.iter_parent(drop_iter) print drop_iter, path, rownum self.move_row_and_children(it, drop_iter, rownum) else: if pos in [gtk.TREE_VIEW_DROP_INTO_OR_BEFORE, gtk.TREE_VIEW_DROP_INTO_OR_AFTER]: self.move_row_and_children(it, drop_iter) else: if pos == gtk.TREE_VIEW_DROP_AFTER: pos += 1 self.move_row_and_children(it, drop_iter, pos) elif selection_data.type == "image-filename": model_path = list(self.model.get_path(drop_iter)) if len(model_path) <= 1 or model_path[0] == 1: return path = data from picty import baseobjects item = baseobjects.Item(path) ind = self.worker.active_collection.find(item) if ind < 0: return False thumb_pb = self.worker.active_collection(ind).thumb if thumb_pb: width, height = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU) width = width * 3 / 2 height = height * 3 / 2 tw = thumb_pb.get_width() th = thumb_pb.get_height() if width / height > tw / th: height = width * th / tw else: width = height * tw / th thumb_pb = thumb_pb.scale_simple(width * 3 / 2, height * 3 / 2, gtk.gdk.INTERP_BILINEAR) self.set_and_save_user_bitmap(drop_iter, thumb_pb) ## get the thumbnail and set the drop_iter row pixbuf and pixpath accordingly pass
def _get_subpage_path(self): # The following needs to work: # Foo/bar -> Foo#dir/bar # Foo/bar/baz -> Foo#dir/bar#dir/baz # Foo/ -> Foo#dir/#data path = self.path.split("/") name = path.pop() if not name: name = "#data" return "/".join(x+"#dir" for x in path), name
def find_common_path(self, paths): paths = [path.split("/") for path in paths] common_path = [] while 0 not in [len(path) for path in paths]: next_segment = list(set([path.pop(0) for path in paths])) if len(next_segment) == 1: common_path += next_segment else: break return "/".join(common_path) + "/"
def find_common_path(self, paths): paths = [path.replace("\"", "") for path in paths] paths = [path.split(os.path.sep) for path in paths] common_path = [] while 0 not in [len(path) for path in paths]: next_segment = list(set([path.pop(0) for path in paths])) if len(next_segment) == 1: common_path += next_segment else: break return os.path.sep.join(common_path) + os.path.sep
def _traverse(self, path, node): if len(path) == 0: return node fn = path.pop(0) next_node = node.get_child(fn) if node.dirtype == DirectoryType.root: self._lazy_load_subreddit(next_node, fn) return self._traverse(path, next_node)
def walk(path, tree, visited=None): if visited == None: visited = [] segm = path.pop() if segm == '': segm = '/' visited.append(segm) if path: children = walk(path, tree[segm], visited) else: children = tree[segm].keys() children = [os.path.join(*visited + [x]) for x in children] return children
def circuit(thisnode, startnode, component): closed = False # set to True if elementary path is closed path.append(thisnode) if len(path) <= maxlen: blocked[thisnode] = True for nextnode in component[thisnode]: # direct successors of thisnode if nextnode == startnode: if len(path) >= minlen: add_cycle(path + [startnode]) closed = True elif not blocked[nextnode]: if circuit(nextnode, startnode, component): closed = True if closed: _unblock(thisnode) else: for nextnode in component[thisnode]: if thisnode not in B[nextnode]: B[nextnode].append(thisnode) path.pop() # remove thisnode from path return closed
def mod_sub_tree(tree, sub_t, path): t = tree path = [p for p in path] # deepcopy # can't mod the entire thing without a ref to its parent if len(path) == 0: assert False # traverse to the right point in the tree # end result is that path is a list w one element while len(path) > 1: t = t[path.pop(0)] # now tree is the direct parent this modifies tree. t[path[0]] = sub_t
def getTable(filepath, tab_name, mod=None): if isinstance(filepath, str): path = {filepath : tab_name} for p in path: if mod: with open(p) as f: cont = f.read() with open(p.replace('.tex', '_mod.tex'), 'w') as f: f.write(mod(cont)) path[p.replace('.tex', '._mod.tex') ] = path.pop(p) return dict( (t+'.tex', p) for p, t in path.iteritems() ).items()
def find_sub_tree(tree, basedir): """Return a subtree of the given tree Keyword arguments: tree -- the git.Tree root in which we look for the subtree basedir -- the path where the subtree is located """ path = basedir.split('/') if len(path) > 0: path.reverse() dirname = path.pop() path.reverse() for subtree in tree.trees: if subtree.name == dirname: return find_sub_tree(subtree, '/'.join(path)) logging.info("Using " + tree.name + " subdirectory") return tree