Get directory structure in python
I'm interested in mapping all of the synced folders on a box account using python. I've come up with a solution which looks like this:
def perform_full_update(client, root_dir):
do_call = lambda l: client.folder(folder_id = l).get_items(
limit = 100, offset = 0,
fields = ['modified_at','name','sync_state'])
fold_pred = lambda i: i['type'] == 'folder'
sync_pred = lambda i: i['sync_state'] != 'not_synced'
def recurse(item):
try:
fid = item['id']
all_items = do_call(fid)
dir_items = filter(fold_pred, all_items)
dir_items = filter(sync_pred, dir_items)
sleep(0.1)
return (item['name'], list(map(recurse, dir_items)))
except exception.BoxAPIException as e:
if e.status != 404: raise e
else: return (item['name'], [])
root = client.folder(folder_id = '0').get()
pprint(recurse(root))
which generally works but makes lots of API calls and relies on the questionable principal of using an exception as a recursion base case. Does the Python SDK offer a better way of managing this?
サインインしてコメントを残してください。
コメント
0件のコメント