| 7 | |
| 8 | |
| 9 | class Urn(object): |
| 10 | |
| 11 | separate = "/" |
| 12 | |
| 13 | def __init__(self, path, directory=False): |
| 14 | |
| 15 | self._path = quote(path) |
| 16 | expressions = "/\.+/", "/+" |
| 17 | for expression in expressions: |
| 18 | self._path = sub(expression, Urn.separate, self._path) |
| 19 | |
| 20 | if not self._path.startswith(Urn.separate): |
| 21 | self._path = "{begin}{end}".format(begin=Urn.separate, end=self._path) |
| 22 | |
| 23 | if directory and not self._path.endswith(Urn.separate): |
| 24 | self._path = "{begin}{end}".format(begin=self._path, end=Urn.separate) |
| 25 | |
| 26 | def __str__(self): |
| 27 | return self.path() |
| 28 | |
| 29 | def path(self): |
| 30 | return unquote(self._path) |
| 31 | |
| 32 | def quote(self): |
| 33 | return self._path |
| 34 | |
| 35 | def filename(self): |
| 36 | |
| 37 | path_split = self._path.split(Urn.separate) |
| 38 | name = path_split[-2] + Urn.separate if path_split[-1] == '' else path_split[-1] |
| 39 | return unquote(name) |
| 40 | |
| 41 | def parent(self): |
| 42 | |
| 43 | path_split = self._path.split(Urn.separate) |
| 44 | nesting_level = self.nesting_level() |
| 45 | parent_path_split = path_split[:nesting_level] |
| 46 | parent = self.separate.join(parent_path_split) if nesting_level != 1 else Urn.separate |
| 47 | if not parent.endswith(Urn.separate): |
| 48 | return unquote(parent + Urn.separate) |
| 49 | else: |
| 50 | return unquote(parent) |
| 51 | |
| 52 | def nesting_level(self): |
| 53 | return self._path.count(Urn.separate, 0, -1) |
| 54 | |
| 55 | def is_dir(self): |
| 56 | return self._path[-1] == Urn.separate |
no outgoing calls
no test coverage detected