Created
August 12, 2020 01:52
-
-
Save techieji/d1db5311dd56a2d2086bdd2f5b7dedea to your computer and use it in GitHub Desktop.
Note.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| A simple language made for notetaking and data storage in the form of bullets. | |
| """ | |
| from pprint import pprint | |
| import pickle | |
| import os.path | |
| def linstore(code: str) -> list: | |
| lines = code.split("\n") | |
| contents = [] | |
| levels = [] | |
| for line in lines: | |
| count = 0 | |
| content = "" | |
| flag = True | |
| for char in line: | |
| if flag and char == " ": | |
| count += 1 | |
| elif char == "-" and flag: | |
| flag = False | |
| elif not flag: | |
| content += char | |
| if content.strip(): | |
| contents.append(content.strip()) | |
| levels.append(count // 2) | |
| return zip(contents, levels) | |
| def compileNote(code: str, filename: str) -> list: | |
| stored = linstore(code) | |
| with open(filename, "bw") as f: | |
| pickle.dump(stored, f) | |
| return stored | |
| def getNote(filename: str) -> list: | |
| with open(filename, "br") as f: | |
| return list(pickle.load(f)) | |
| def compileNoteFile(filename): | |
| with open(filename) as f: | |
| compileNote(f.read(), filename + '.c') | |
| replace = lambda string, old, new: new.join(string.split(old)) | |
| def replace(old: str, new: str) -> callable: | |
| def f(string: str) -> str: | |
| new.join(string.split(old)) | |
| return f | |
| def exists(linstored: iter, entry: str) -> bool: | |
| for text, level in linstored: | |
| if text == entry: | |
| return True | |
| return False | |
| def find(linstored: iter, entry: str) -> list: | |
| path = {} | |
| for element, level in linstored: | |
| # print(path) | |
| path[level] = element | |
| if element == entry: | |
| ans = [] | |
| for x in path.values(): | |
| ans.append(x) | |
| if x == entry: | |
| return ans | |
| def subnote(linstored: iter, entry: str) -> list: | |
| iterator = iter(linstored) | |
| element, level = next(iterator) | |
| while element != entry: | |
| element, level = next(iterator) | |
| marker = level | |
| lines = [(entry, level)] | |
| element, level = next(iterator) | |
| try: | |
| while level > marker: | |
| lines.append((element, level)) | |
| element, level = next(iterator) | |
| except StopIteration: | |
| pass | |
| return (marker, lines) | |
| def search(linstored: iter, text: str) -> list: | |
| linstored = iter(linstored) | |
| ret = [] | |
| path = {} | |
| for content, level in linstored: | |
| path[level] = content | |
| if text in content: | |
| sub = [] | |
| for x in path.values(): | |
| sub.append(x) | |
| if x == content: | |
| break | |
| ret.append(sub) | |
| return ret | |
| def dispatch(linstored: zip, command: str, filename: str = "") -> str: | |
| cmd = list(filter(bool, command.split(" "))) | |
| # print(cmd) | |
| ins = { | |
| "refresh": -2, | |
| "c": -1, | |
| "exit": 0, | |
| "exists?": 1, | |
| "subnote": 2, | |
| "find": 3, | |
| "search": 4, | |
| "add": 5 | |
| }[cmd[0]] | |
| rest = " ".join(cmd[1:]) | |
| if ins == -2: | |
| compileNote(linstored, filename) | |
| if ins == -1: | |
| compileNoteFile(cmd[2]) | |
| elif ins == 0: | |
| raise KeyboardInterrupt | |
| elif ins == 1: | |
| return str(exists(linstored, cmd[1])) | |
| elif ins == 2: | |
| lis = subnote(linstored, cmd[1]) | |
| return "\n".join([f"{' ' * (l - lis[0])} - {e}" for e, l in lis[1]]) | |
| elif ins == 3: | |
| return " -> ".join(list(find(linstored, cmd[1]))) | |
| elif ins == 4: | |
| return "\n".join([" -> ".join(x) for x in search(linstored, cmd[1])]) | |
| else: | |
| return None | |
| def getfile() -> list: | |
| print("Enter a valid filename:") | |
| while True: | |
| filename = input("? ") | |
| try: | |
| return (filename, getNote(filename)) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| def cmdmode(): | |
| filename, stored = getfile() | |
| print("Entering cmd-mode...") | |
| try: | |
| while True: | |
| try: | |
| cmd = input('> ') | |
| if cmd == "load!": | |
| filename, stored = getfile() | |
| else: | |
| print(dispatch(stored, cmd, filename)) | |
| except Exception as e: | |
| if type(e) == KeyboardInterrupt: | |
| raise | |
| else: | |
| if type(e) == KeyError: | |
| print("Unrecognised command") | |
| if type(e) == TypeError: | |
| print("Cannot traverse to node") | |
| else: | |
| print(f"Error: {e}") | |
| except KeyboardInterrupt: | |
| pass | |
| if __name__ == "__main__": | |
| testing = 0 | |
| if testing: | |
| testdata = getNote("Note/test.n.c") | |
| pprint(search(testdata, "the")) | |
| else: | |
| cmdmode() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment