0

I've got this algorithm to generate MPTT from my folder structure: https://gist.github.com/unbracketed/946520

Found on github and works perfectly for my needs. Currently I have requirement to add to it functionality of skipping some folders in tree. For example I want to skip everything in/under /tmp/A/B1/C2. So my tree will not contain anything from C2 (including C2).

I'm not so useless in python so I've created that queries (and passed extra list to function):

def is_subdir(path, directory):
    path = os.path.realpath(path)
    directory = os.path.realpath(directory)
    relative = os.path.relpath(path, directory)
    return not relative.startswith(os.pardir + os.sep)

/Now we can add somewhere
for single in ignorelist:
    if fsprocess.is_subdir(node,single):

But my question is where to stick in the function? I've tried to this on the top and in if do return but that exiting my whole application. It's recurring invoking itself so I'm pretty lost.

Any good advices? I've tried contact script creator on github no owner. Really good job with this algorithm, save me a lot of the time and it's perfect for our project requirements.

FiR3WaLL
  • 33
  • 1
  • 7

1 Answers1

0
def generate_mptt(root_dir):
    """
    Given a root directory, generate a calculated MPTT
    representation for the file hierarchy
    """
    for root, dirs, _ in os.walk(root_dir):

Your check should go here:

        if any(is_subdir(root, path) for path in ignorelist):
           del dirs[:] # don't descend
           continue

Sort of like that. Assuming that is_subdir(root, path) returns True if root is a subdirectory of path.

        dirs.sort()
        tree[root] = dirs
    preorder_tree(root_dir, tree[root_dir])
    mptt_list.sort(key=lambda x: x.left)
Dan D.
  • 73,243
  • 15
  • 104
  • 123