From bdeed4ab84f46ce71fc229e19166b4d2d47f5899 Mon Sep 17 00:00:00 2001 From: Lars Yencken Date: Tue, 10 Sep 2013 14:57:12 +1000 Subject: [PATCH] Recurse subdirectories within a cheat dir. This lets you share cheats more easily with others. For example, within ~/.cheat I can have one subfolder of public cheats, one subfolder of private within-company cheats, and one I've cloned from a friend on github. --- cheat | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cheat b/cheat index a732faf..fa65eee 100755 --- a/cheat +++ b/cheat @@ -62,11 +62,19 @@ def cheat_directories(): def cheat_files(cheat_directories): "Assembles a dictionary of cheatsheets found in the above directories." cheats = {} - for cheat_dir in reversed(cheat_directories): - cheats.update(dict([(cheat, cheat_dir) - for cheat in os.listdir(cheat_dir) - if not cheat.startswith('.') - and not cheat.startswith('__')])) + queue = cheat_directories[:] + while queue: + cheat_dir = queue.pop() + for cheat in os.listdir(cheat_dir): + if not cheat.startswith('.') and not cheat.startswith('__'): + cheat_path = os.path.join(cheat_dir, cheat) + if os.path.isdir(cheat_path): + # recurse subdirectory + sub_dir = os.path.join(cheat_dir, cheat) + queue.append(sub_dir) + else: + cheats[cheat] = cheat_dir + return cheats def edit_cheatsheet(cheat, cheatsheets):