示例#1
0
def test_build_tree():
    def children_for(x):
        return range(0, x/2)

    expected = Tree(8, [ Tree(0)
                       , Tree(1)
                       , Tree(2, [ Tree(0)
                                 ])
                       , Tree(3, [ Tree(0)
                                 ])
                       ])

    assert_equals(expected, Tree.build_tree(8, children_for))
示例#2
0
    def _build_manifest(cls, path, f, ignored_directories=None):
        if not path:
            raise ValueError("path must be a non-empty string")

        if not f:
            raise ValueError("transform function must be specified")

        path = os.path.abspath(path)
        if not os.path.isdir(path):
            raise ValueError("{} does not name a directory".format(path))

        if not ignored_directories:
            ignored_directories = []

        def child_directories(dirname):
            names = [os.path.join(dirname, name) for name in os.listdir(dirname) if not name.startswith(".")]
            return [name for name in names if os.path.isdir(name) and not name in ignored_directories]

        directory_tree = Tree.build_tree(path, child_directories)
        manifest_tree = directory_tree.fmap(f)

        return { entry.path: entry for entry in itertools.chain.from_iterable(manifest_tree) }