This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bugs with read broken notes file and add
--init
flag
- Loading branch information
Showing
1 changed file
with
70 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,96 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import json, argparse | ||
import argparse | ||
import json | ||
from json import JSONDecodeError | ||
|
||
from rofi import Rofi | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"-f", "--file", help="specify path to notes notes file", required=True | ||
) | ||
parser.add_argument( | ||
"--init", help="clear and initialize notes file", action="store_true" | ||
) | ||
args = parser.parse_args() | ||
|
||
with open(args.file) as f: | ||
notes = json.load(f) | ||
|
||
titles = [] | ||
r = Rofi() | ||
|
||
|
||
def init_file(): | ||
inote = {"notes": [{"name": "empty", "description": "empty"}]} | ||
with open(args.file, "w") as f: | ||
json.dump(inote, f, indent=2) | ||
print("Notes are reset and initialized again.") | ||
r.error("Notes are reset and initialized again.") | ||
exit() | ||
|
||
|
||
if args.init: | ||
init_file() | ||
|
||
|
||
try: | ||
with open(args.file) as f: | ||
notes = json.load(f) | ||
except JSONDecodeError: | ||
print("JSONDecodeError") | ||
init_file() | ||
except KeyError: | ||
print("KeyError") | ||
init_file() | ||
|
||
|
||
def show_titles(): | ||
for i in range(len(notes["notes"])): | ||
titles.append(notes["notes"][i]["name"]) | ||
|
||
r = Rofi() | ||
|
||
while True: | ||
titles = [] | ||
show_titles() | ||
try: | ||
while True: | ||
titles = [] | ||
show_titles() | ||
|
||
index, key = r.select( | ||
"Notes", | ||
titles, | ||
key1=("Alt+d", "delete note"), | ||
key2=("Alt+a", "add note"), | ||
rofi_args=["-i", "-no-custom", "-l", "15"], | ||
) | ||
index, key = r.select( | ||
"Notes", | ||
titles, | ||
key1=("Alt+d", "delete note"), | ||
key2=("Alt+a", "add note"), | ||
rofi_args=["-i", "-no-custom", "-l", "15"], | ||
) | ||
|
||
if key == 0: | ||
r.error(notes["notes"][index]["description"]) | ||
if key == 0: | ||
r.error(notes["notes"][index]["description"]) | ||
|
||
elif key == 1: | ||
del notes["notes"][index] | ||
elif key == 1: | ||
del notes["notes"][index] | ||
|
||
with open(args.file, "w") as f: | ||
json.dump(notes, f, indent=2) | ||
with open(args.file, "w") as f: | ||
json.dump(notes, f, indent=2) | ||
|
||
r.error("Note deleted.") | ||
r.error("Note deleted.") | ||
|
||
elif key == 2: | ||
name = r.text_entry("Enter note name", rofi_args=["-l", "0"]) | ||
elif key == 2: | ||
name = r.text_entry("Enter note name", rofi_args=["-l", "0"]) | ||
|
||
if name: | ||
desc = r.text_entry("Enter node description", rofi_args=["-l", "0"]) | ||
new_note = {"name": name, "description": desc} | ||
notes["notes"].append(new_note) | ||
if name: | ||
desc = r.text_entry("Enter node description", rofi_args=["-l", "0"]) | ||
new_note = {"name": name, "description": desc} | ||
notes["notes"].append(new_note) | ||
|
||
with open(args.file, "w") as f: | ||
json.dump(notes, f, indent=2) | ||
with open(args.file, "w") as f: | ||
json.dump(notes, f, indent=2) | ||
|
||
r.error("Note added.") | ||
|
||
r.error("Note added.") | ||
else: | ||
break | ||
|
||
else: | ||
break | ||
except IndexError: | ||
print("IndexError") | ||
init_file() | ||
except KeyError: | ||
print("KeyError") | ||
init_file() |