3

Coming from Python recursively appending list function Trying to recursively get a list of permissions associated with a file structure.

I have this function:

def get_child_perms(self, folder, request, perm_list):
        # Folder contains other folders
        if folder.get_children():
            # For every sub-folder
            return [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
        return folder.has_read_permission(request)

That returns all the results except the folders that contain other folders.

folder <- Missing (allowed)
    subfolder <- Missing (restricted)
        subsubfolder <- Get this (restricted)
            files

Output from function would be [True, False, False]

another case would be, where A = allowed, R = restricted

folder  A
    subfolder   A
        subsubfolder    R
            files
        files
    subfolder   R
        files
    subfolder   A
        subsubfolder    A
            files
        files
    subfolder   A
        files
    files

Output would be [True,True,False,False,True,True,True]

4
  • 2
    What are you really trying to do? Seems like list is not the correct choice of data structure in your case. Commented Aug 19, 2015 at 18:55
  • 1
    @AnandSKumar and others, for reference: stackoverflow.com/questions/32102420/… Commented Aug 19, 2015 at 18:56
  • 1
    I know there is a way to recursively store build perm_list to hold all these results, also added output for simple case Commented Aug 19, 2015 at 19:00
  • Are you sure you want them all in a flattened list? Commented Aug 19, 2015 at 19:03

2 Answers 2

3

The basic issue occurs you are only returning the folder permission , when folder does not have any children , when it has children, you are not including the folder.has_read_permission(request) in your return result , which is most probably causing you issue. You need to do -

def get_child_perms(self, folder, request, perm_list):
        # Folder contains other folders
        if folder.get_children():
            # For every sub-folder
            return [folder.has_read_permission(request)] + [self.get_child_perms(subfolder, request, perm_list) for subfolder in folder.get_children()]
        return [folder.has_read_permission(request)]

This should result in (not tested) -

[folderperm [subfolderperm [subsubfolderperm]]

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly, list is not flattened but might be better
0

why not os.walk

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

for example you can build generator (lazy list) that generates only non restricted directories

for (dirpath, dirnames, filenames) in os.walk("top_path"):
    if restricted(dirpath):
        del dirnames
        continue
    yield (dirpath,tuple(filenames))

2 Comments

Not an actual filesystem, just django-filer
when you can yield booleans

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.