"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/http_prompt/tree.py" (5 Mar 2021, 2309 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "tree.py" see the Fossies "Dox" file reference documentation.

    1 """Tree data structure for ls command to work with OpenAPI specification."""
    2 
    3 from __future__ import unicode_literals
    4 
    5 
    6 class Node(object):
    7 
    8     def __init__(self, name, data=None, parent=None):
    9         if name in ('.', '..'):
   10             raise ValueError("name cannot be '.' or '..'")
   11 
   12         self.name = name
   13         self.data = data or {}
   14         self.parent = parent
   15         self.children = set()
   16 
   17     def __str__(self):
   18         return self.name
   19 
   20     def __repr__(self):
   21         return "Node('{}', '{}')".format(self.name, self.data.get('type'))
   22 
   23     def __lt__(self, other):
   24         ta = self.data.get('type')
   25         tb = other.data.get('type')
   26         if ta != tb:
   27             return ta < tb
   28         return self.name < other.name
   29 
   30     def __eq__(self, other):
   31         return self.name == other.name and self.data == other.data
   32 
   33     def __hash__(self):
   34         return hash((self.name, self.data.get('type')))
   35 
   36     def add_path(self, *path, **kwargs):
   37         node_type = kwargs.get('node_type', 'dir')
   38         name = path[0]
   39         tail = path[1:]
   40         child = self.find_child(name, wildcard=False)
   41         if not child:
   42             data = {'type': 'dir' if tail else node_type}
   43             child = Node(name, data=data, parent=self)
   44             self.children.add(child)
   45 
   46         if tail:
   47             child.add_path(*tail, node_type=node_type)
   48 
   49     def find_child(self, name, wildcard=True):
   50         for child in self.children:
   51             if child.name == name:
   52                 return child
   53 
   54         # Attempt to match wildcard like /users/{user_id}
   55         if wildcard:
   56             for child in self.children:
   57                 if child.name.startswith('{') and child.name.endswith('}'):
   58                     return child
   59 
   60         return None
   61 
   62     def ls(self, *path):
   63         success = True
   64         cur = self
   65         for name in path:
   66             if not name or name == '.':
   67                 continue
   68             elif name == '..':
   69                 if cur.parent:
   70                     cur = cur.parent
   71             else:
   72                 child = cur.find_child(name)
   73                 if child:
   74                     cur = child
   75                 else:
   76                     success = False
   77                     break
   78         if success:
   79             for node in sorted(cur.children):
   80                 yield node