Python script to list Neorg files with a certain tag
neorg-tags.py
118 lines 3.8 kB view raw
1#!/usr/bin/env python3 2import os 3import sys 4 5def main(): 6 # ----- Parse arguments ----- 7 input_cat = "" 8 neorg_dir = "" 9 for (i, arg) in enumerate(sys.argv): 10 if sys.argv[i-1] == "--category": 11 continue 12 if arg == "--category": 13 if len(sys.argv) <= i + 1: 14 print("Category not valid") 15 help() 16 return 17 input_cat = sys.argv[i + 1] 18 continue 19 elif len(sys.argv) - 1 < i: 20 help() 21 return 22 neorg_dir = arg 23 24 while not os.path.isdir(neorg_dir): 25 print("Input target directory") 26 print("> ", end="") 27 neorg_dir = os.path.abspath(os.path.expanduser(input())) 28 if not os.path.isdir(neorg_dir): 29 print("Directory not found") 30 31 # ----- Main Program ----- 32 33 categories = [] 34 category_dict = {} 35 no_categories = [] 36 fill_category_dict(neorg_dir, neorg_dir, categories, no_categories, category_dict) 37 38 # ----- Output Result ----- 39 40 if len(categories) == 0: 41 print("No Neorg files found in", neorg_dir) 42 return 43 44 # Category selection 45 if input_cat == "": 46 # Print all categories 47 print("Found", len(categories), "categories:") 48 for cat in categories: 49 print("\t", cat, sep="") 50 51 # Print empty warning 52 if len(no_categories) != 0: 53 print("Found", len(no_categories), "files with no categories, enter \"None\" to list them") 54 55 # Get user input 56 print("Please enter a category to search for:") 57 print("> ", end="") 58 input_cat = input() 59 60 # special case for no category files 61 if input_cat == "None": 62 print("Found", len(no_categories), "files with no categories:") 63 for file in no_categories: 64 print("\t", file) 65 return 66 67 # check if input is valid 68 if input_cat not in categories: 69 print("Category not valid") 70 return 71 72 # print found files 73 print("Found ", len(category_dict[input_cat]), " files with category ", input_cat, " in ", neorg_dir, ":", sep="") 74 for file in category_dict[input_cat]: 75 print("\t", file) 76 77 78def help(): 79 print("Usage: neorg-tags.py <path to neorg directory>") 80 print("\tOptionally add --category <category> to search for a specific category") 81 82 83def fill_category_dict(curr_dir, neorg_dir, categories, no_categories, category_dict): 84 for file in os.listdir(curr_dir): 85 86 # if file is a not hidden directory, recurse 87 if os.path.isdir(os.path.join(curr_dir, file)) and not file.startswith("."): 88 fill_category_dict(os.path.join(curr_dir, file), neorg_dir, categories, no_categories, category_dict) 89 90 if not file.endswith(".norg"): 91 continue 92 93 with open(os.path.join(curr_dir, file)) as f: 94 95 # get all the categories 96 curr_categories = [] 97 for line in f: 98 if not line.startswith("categories: "): 99 continue 100 101 curr_categories = line.split("categories: ")[1].split(" ") 102 curr_categories[-1] = curr_categories[-1].strip() # remove trailing newline 103 104 # add all categories to categories and category_dict 105 for category in curr_categories: 106 filepath = os.path.join(curr_dir, file).split(neorg_dir)[1] 107 if category == "": 108 no_categories.append(filepath) 109 continue 110 if category not in categories: 111 categories.append(category) 112 if category not in category_dict: 113 category_dict[category] = [] 114 category_dict[category].append(filepath) 115 116 117if __name__ == "__main__": 118 main()