| 9 | | Helper functions |
|---|
| 10 | | ---------------- |
|---|
| 11 | | |
|---|
| 12 | | ``dir_level`` |
|---|
| 13 | | ~~~~~~~~~~~~~ |
|---|
| 14 | | |
|---|
| 15 | | The function ``dir_level`` returns the "depth" of a directory. For |
|---|
| 16 | | this it uses the heuristics of counting the path separators in a |
|---|
| 17 | | path. The following tests are intended to run under Posix systems:: |
|---|
| 18 | | |
|---|
| 19 | | >>> import converter |
|---|
| 20 | | |
|---|
| 21 | | >>> converter.dir_level("/the/root/directory") |
|---|
| 22 | | 3 |
|---|
| 23 | | >>> converter.dir_level("/dir") |
|---|
| 24 | | 1 |
|---|
| 25 | | |
|---|
| 26 | | As a special case, the root directory has a directory level of 0, |
|---|
| 27 | | since it's "above" a directory located in the root directory:: |
|---|
| 28 | | |
|---|
| 29 | | >>> converter.dir_level("/") |
|---|
| 30 | | 0 |
|---|
| 31 | | |
|---|
| 32 | | ``walk`` |
|---|
| 33 | | ~~~~~~~~ |
|---|
| 34 | | |
|---|
| 35 | | The ``walk`` function takes a path, a ``depth`` (which is the maximum |
|---|
| 36 | | number of directory levels to descend) and a "private" ``_max_level`` |
|---|
| 37 | | argument. The latter is only used internally to provide a criterion to |
|---|
| 38 | | stop the recursive call of the ``walk`` function. The return value is |
|---|
| 39 | | a generator function which returns a file system item for each |
|---|
| 40 | | iteration over the generator. |
|---|
| 41 | | |
|---|
| 42 | | To test the function, use it on a known directory, the |
|---|
| 43 | | Websourcebrowser project directory. Note that the test routine sets |
|---|
| 44 | | the project directory as the current directory, so the following code |
|---|
| 45 | | should work:: |
|---|
| 46 | | |
|---|
| 47 | | >>> for item in converter.walk("."): |
|---|
| 48 | | ... print item #doctest: +ELLIPSIS |
|---|
| 49 | | /home/... |
|---|
| 50 | | ... |
|---|
| 51 | | /home/schwa/sd/python/websourcebrowser/graphics/logo.png |
|---|
| 52 | | ... |
|---|
| 53 | | /home/schwa/sd/python/websourcebrowser/browser.py |
|---|
| 54 | | ... |
|---|
| 55 | | /home/schwa/sd/python/websourcebrowser/converter.py |
|---|
| 56 | | ... |
|---|
| 57 | | |
|---|
| 58 | | If we limit the directory depth (``depth``) to 1, the "lower" |
|---|
| 59 | | directory items should not be present:: |
|---|
| 60 | | |
|---|
| 61 | | >>> items = list(converter.walk(".", depth=1)) |
|---|
| 62 | | >>> "/home/schwa/sd/python/websourcebrowser/browser.py" in items |
|---|
| 63 | | True |
|---|
| 64 | | >>> "/home/schwa/sd/python/websourcebrowser/graphics" in items |
|---|
| 65 | | True |
|---|
| 66 | | >>> "/home/schwa/sd/python/websourcebrowser/graphics/logo.png" in items |
|---|
| 67 | | False |
|---|
| 68 | | |
|---|
| 69 | | ``_ignore_item`` |
|---|
| 70 | | ~~~~~~~~~~~~~~~~ |
|---|
| 71 | | |
|---|
| 72 | | The function ``_ignore_item`` is used to test whether a file system |
|---|
| 73 | | item should be excluded from a directory listing. This may, for |
|---|
| 74 | | example, apply to the pattern "*.pyc", i. e. Python bytecode files. |
|---|
| 75 | | The result of ``_ignore_item`` is a boolean flag which depends on the |
|---|
| 76 | | value of ``config.ignore_patterns`` which is a list of glob patterns:: |
|---|
| 77 | | |
|---|
| 78 | | >>> import config |
|---|
| 79 | | >>> config.ignore_patterns = ["*.pyc", "*.swp"] |
|---|
| 80 | | >>> for item in ("file.py", "file.pyc", "test.swpx", "test.swp"): |
|---|
| 81 | | ... print item, converter._ignore_item(item) |
|---|
| 82 | | file.py False |
|---|
| 83 | | file.pyc True |
|---|
| 84 | | test.swpx False |
|---|
| 85 | | test.swp True |
|---|
| 86 | | |
|---|
| 87 | | The function doesn't handle strings starts or endings specially, so to |
|---|
| 88 | | exclude all ".svn" directories and all items below them, you have to |
|---|
| 89 | | use *two* patterns. (This might change in the future.) |
|---|
| 90 | | |
|---|
| 91 | | >>> config.ignore_patterns = [".svn"] |
|---|
| 92 | | >>> converter._ignore_item("/test/.svn") |
|---|
| 93 | | False |
|---|
| 94 | | >>> config.ignore_patterns = ["*/.svn"] |
|---|
| 95 | | >>> converter._ignore_item("/test/.svn") |
|---|
| 96 | | True |
|---|
| 97 | | >>> converter._ignore_item("/test/.svn/entries") |
|---|
| 98 | | False |
|---|
| 99 | | >>> config.ignore_patterns = ["*/.svn", "*/.svn/*"] |
|---|
| 100 | | >>> converter._ignore_item("/test/.svn") |
|---|
| 101 | | True |
|---|
| 102 | | >>> converter._ignore_item("/test/.svn/entries") |
|---|
| 103 | | True |
|---|
| 104 | | |
|---|
| 105 | | |
|---|