snippet_watcher.py (3323B)
1 # from talon import app, fs 2 # import os, csv, re 3 # from os.path import isfile, join 4 # from itertools import islice 5 # from pathlib import Path 6 # import json 7 # from jsoncomment import JsonComment 8 9 # parser = JsonComment(json) 10 11 # pattern = re.compile(r"[A-Z][a-z]*|[a-z]+|\d") 12 13 # # todo: should this be an action that lives elsewhere?? 14 # def create_spoken_form(text, max_len=15): 15 # return " ".join(list(islice(pattern.findall(text), max_len))) 16 17 18 # class snippet_watcher: 19 # directories = {} 20 # snippet_dictionary = {} 21 # callback_function = None 22 # file_snippet_cache = {} 23 24 # def __notify(self): 25 # # print("NOTIFY") 26 # self.snippet_dictionary = {} 27 # for key, val in self.file_snippet_cache.items(): 28 # self.snippet_dictionary.update(val) 29 30 # # print(str(self.snippet_dictionary)) 31 # if self.callback_function: 32 # self.callback_function(self.snippet_dictionary) 33 34 # def __update_all_snippets(self): 35 # for directory, file_list in self.directories.items(): 36 # if os.path.isdir(directory): 37 # for f in file_list: 38 # path = os.path.join(directory, f) 39 # self.__process_file(path) 40 41 # # print(str(self.snippet_dictionary)) 42 # self.__notify() 43 44 # def __process_file(self, name): 45 # path_obj = Path(name) 46 # directory = os.path.normpath(path_obj.parents[0]) 47 # file_name = path_obj.name 48 # file_type = path_obj.suffix 49 # self.file_snippet_cache[str(path_obj)] = {} 50 51 # print("{}, {}, {}, {}".format(name, directory, file_name, file_type)) 52 # if directory in self.directories and file_name in self.directories[directory]: 53 # if file_type.lower() == ".json": 54 # jsonDict = {} 55 56 # if os.path.isfile(name): 57 # with open(name, "r") as f: 58 # jsonDict = parser.load(f) 59 # # else: 60 # # print("snippet_watcher.py: File {} does not exist".format(directory)) 61 62 # for key, data in jsonDict.items(): 63 # self.file_snippet_cache[str(path_obj)][ 64 # create_spoken_form(key) 65 # ] = data["prefix"] 66 67 # def __on_fs_change(self, name, flags): 68 # self.__process_file(name) 69 70 # # print(str(self.snippet_dictionary)) 71 # self.__notify() 72 73 # def __init__(self, dirs, callback): 74 # self.directories = dirs 75 # self.callback_function = callback 76 # self.snippet_dictionary = {} 77 # self.file_snippet_cache = {} 78 # # none = process all directories 79 # self.__update_all_snippets() 80 81 # for directory in self.directories.keys(): 82 # if os.path.isdir(directory): 83 # fs.watch(directory, self.__on_fs_change) 84 # # else: 85 # # print( 86 # # "snippet_watcher.py: directory {} does not exist".format(directory) 87 # # ) 88 89 90 # # Test = snippet_watcher( 91 # # {os.path.expandvars(r"%AppData%\Code\User\snippets"): ["python.json"]}, 92 # # None 93 # # # {os.path.expandvars(r"%USERPROFILE%\.vscode\extensions\ms-dotnettools.csharp-1.22.1\snippets": ["csharp.json"]}, 94 # # ) 95 # # print(str(Test.directories))