Mercurial > hg > config
comparison python/tree.py @ 425:5417eb6364ee
stubbing
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Tue, 06 Aug 2013 18:26:42 -0700 |
| parents | cccfe246452e |
| children | 866c5b1bc4e8 |
comparison
equal
deleted
inserted
replaced
| 424:d214e0f38ab1 | 425:5417eb6364ee |
|---|---|
| 2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
| 3 | 3 |
| 4 """ | 4 """ |
| 5 tree in python | 5 tree in python |
| 6 """ | 6 """ |
| 7 | |
| 8 # TODO: script2package | |
| 7 | 9 |
| 8 import optparse | 10 import optparse |
| 9 import os | 11 import os |
| 10 import sys | 12 import sys |
| 11 | 13 |
| 21 'vertical_line' : '│', | 23 'vertical_line' : '│', |
| 22 'item_marker' : '├', | 24 'item_marker' : '├', |
| 23 'last_child' : '└' | 25 'last_child' : '└' |
| 24 } | 26 } |
| 25 | 27 |
| 28 | |
| 26 def depth(directory): | 29 def depth(directory): |
| 27 """returns the integer depth of a directory or path relative to '/' """ | 30 """returns the integer depth of a directory or path relative to '/' """ |
| 28 | 31 |
| 29 directory = os.path.abspath(directory) | 32 directory = os.path.abspath(directory) |
| 30 level = 0 | 33 level = 0 |
| 32 directory, remainder = os.path.split(directory) | 35 directory, remainder = os.path.split(directory) |
| 33 level += 1 | 36 level += 1 |
| 34 if not remainder: | 37 if not remainder: |
| 35 break | 38 break |
| 36 return level | 39 return level |
| 40 | |
| 41 | |
| 42 ### stuff for tree generalization | |
| 43 | |
| 44 class Tree(object): | |
| 45 """tree structure in python""" | |
| 46 | |
| 47 def __init__(self, parent=None): | |
| 48 self.parent = parent | |
| 49 | |
| 50 def children(self): | |
| 51 """returns children of the tree""" | |
| 52 | |
| 53 def add(self, item): | |
| 54 """add a child to the tree root""" | |
| 55 | |
| 56 def update(self, tree): | |
| 57 """add a subtree to the tree""" | |
| 58 self.add(tree) | |
| 59 tree.parent = self # XXX .add should probably do this for scary reasons | |
| 60 | |
| 61 def output(self, serializer): | |
| 62 """output the tree via the given serializer""" | |
| 63 # XXX or should this method exist at all and instead the | |
| 64 # __call__ method of serializers take a Tree object? | |
| 65 | |
| 66 class DirectoryTree(Tree): | |
| 67 """directory structure as a tree""" | |
| 68 | |
| 69 ### | |
| 37 | 70 |
| 38 def tree(directory, | 71 def tree(directory, |
| 39 item_marker=unicode_delimeters['item_marker'], | 72 item_marker=unicode_delimeters['item_marker'], |
| 40 vertical_line=unicode_delimeters['vertical_line'], | 73 vertical_line=unicode_delimeters['vertical_line'], |
| 41 last_child=unicode_delimeters['last_child'], | 74 last_child=unicode_delimeters['last_child'], |
