Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
Fix bugs with read broken notes file and add --init flag
Browse files Browse the repository at this point in the history
  • Loading branch information
wzykubek committed Mar 29, 2020
1 parent 3ae12cf commit c626f7d
Showing 1 changed file with 70 additions and 33 deletions.
103 changes: 70 additions & 33 deletions rofi-todo.py
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()

0 comments on commit c626f7d

Please sign in to comment.