Skip to content

[WIP] Format checker for notes.json file #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions usr/lib/sticky/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,25 @@ def __init__(self, settings, window):
self.notes_lists = {}

if os.path.exists(CONFIG_PATH):
self.load_notes()
self.load_notes(window)

self.settings.connect('changed::automatic-backups', self.check_backup)
self.settings.connect('changed::backup-interval', self.check_backup)
self.check_backup()

def load_notes(self, *args):
with open(CONFIG_PATH, 'r') as file:
info = json.loads(file.read())
def load_notes(self, window):
try:
with open(CONFIG_PATH, 'r') as file:
info = json.loads(file.read())

self.notes_lists = info
self.check_json(info)
self.notes_lists = info

except Exception as e:
message = Gtk.MessageDialog(text=_("Unable to load: invalid or corrupted file, exception: {}".format(e)),
buttons=Gtk.ButtonsType.CLOSE, transient_for=window)
message.run()
message.destroy()

def get_note_list(self, group_name):
return self.notes_lists[group_name]
Expand Down Expand Up @@ -233,20 +241,33 @@ def restore_backup(self, menuitem, window):

dialog.destroy()

def check_json(self,file):
for group in file:
for note in file[group]:
if 'x' not in note or 'y' not in note:
raise ValueError("No coordinate value found (x,y)")
if 'height' not in note or 'width' not in note:
raise ValueError("No height or width value found")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really only need title, text, and color to be there. The position and size info is not used in the manager, and is auto-generated by the note if it doesn't already exist. However, we might want to make sure the structure of the file is correct (ie the root item should be a dict, with group names as keys, and the values being lists of dicts).

Copy link
Contributor Author

@m8 m8 Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, should we consider value inside color, because if it's undefined color will be defaulted as gray?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's necessary. It wont cause a crash, and the only way to have an invalid color atm is to manually edit the file (in which case the user should see the issue quite quickly and know exactly how to fix it).

if 'color' not in note:
raise ValueError("No color value found")
if 'title' not in note:
raise ValueError("No title value found")
if 'text' not in note:
raise ValueError("No text value found")

def load_notes_from_path(self, path, window):
try:
with open(path, 'r') as file:
info = json.loads(file.read())

# todo: needs validation here to ensure the file type is correct, and while we're at it, the validation
# should really be added to load_notes() as well
self.check_json(info)

self.notes_lists = info
self.save_note_list()

self.emit('lists-changed')
except Exception as e:
message = Gtk.MessageDialog(text=_("Unable to restore: invalid or corrupted backup file"),
message = Gtk.MessageDialog(text=_("Unable to restore: invalid or corrupted backup file, exception: {}".format(e)),
buttons=Gtk.ButtonsType.CLOSE, transient_for=window)
message.run()
message.destroy()
Expand Down