| 59 | print('%s%s' % (' ' * indent, self._name)) |
| 60 | |
| 61 | class Folder(object): |
| 62 | bin_fmt = struct.Struct('IIII') |
| 63 | bin_item = namedtuple('dirent', 'type, name, data, size') |
| 64 | |
| 65 | def __init__(self, name): |
| 66 | self._name = name |
| 67 | self._children = [] |
| 68 | |
| 69 | @property |
| 70 | def name(self): |
| 71 | return self._name |
| 72 | |
| 73 | @property |
| 74 | def c_name(self): |
| 75 | # add _ to avoid conflict with C key words. |
| 76 | return '_' + self._name |
| 77 | |
| 78 | @property |
| 79 | def bin_name(self): |
| 80 | # Pad to 4 bytes boundary with \0 |
| 81 | pad_len = 4 |
| 82 | bn = self._name + '\0' * (pad_len - len(self._name) % pad_len) |
| 83 | return bn |
| 84 | |
| 85 | def walk(self): |
| 86 | # os.listdir will return unicode list if the argument is unicode. |
| 87 | # TODO: take care of the unicode names |
| 88 | for ent in os.listdir(u'.'): |
| 89 | if os.path.isdir(ent): |
| 90 | cwd = os.getcwd() |
| 91 | d = Folder(ent) |
| 92 | # depth-first |
| 93 | os.chdir(os.path.join(cwd, ent)) |
| 94 | d.walk() |
| 95 | # restore the cwd |
| 96 | os.chdir(cwd) |
| 97 | self._children.append(d) |
| 98 | else: |
| 99 | self._children.append(File(ent)) |
| 100 | |
| 101 | def sort(self): |
| 102 | def _sort(x, y): |
| 103 | if x.name == y.name: |
| 104 | return 0 |
| 105 | elif x.name > y.name: |
| 106 | return 1 |
| 107 | else: |
| 108 | return -1 |
| 109 | from functools import cmp_to_key |
| 110 | self._children.sort(key=cmp_to_key(_sort)) |
| 111 | |
| 112 | # sort recursively |
| 113 | for c in self._children: |
| 114 | if isinstance(c, Folder): |
| 115 | c.sort() |
| 116 | |
| 117 | def dump(self, indent=0): |
| 118 | print('%s%s' % (' ' * indent, self._name)) |
no outgoing calls
no test coverage detected