-
Notifications
You must be signed in to change notification settings - Fork 47
[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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Uh oh!
There was an error while loading. Please reload this page.