Skip to content

Commit

Permalink
Hide layout validation error if purposedly resetting layout config
Browse files Browse the repository at this point in the history
Lead to confusion on which error messages are relevant, see e.g. #32.
  • Loading branch information
Cimbali committed Jul 11, 2017
1 parent 3a197c5 commit 33fcb57
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions pympress/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,20 +627,23 @@ def rearrange_p_layout(self, layout):
def make_pwin(self):
""" Initializes the presenter window.
"""
default_notes_layout = '{"resizeable":true, "orientation":"horizontal", "children":["notes", {"resizeable":false, "children":["current", "next"], "orientation":"vertical"}], "proportions": [0.60, 0.40]}'
default_plain_layout = '{"resizeable":true, "orientation":"horizontal", "children":["current", {"resizeable":true, "orientation":"vertical", "children":["next", "annotations"], "proportions":[0.55, 0.45]}], "proportions":[0.67, 0.33]}'

# Log error and keep default layout
try:
self.notes_layout = pympress.util.layout_from_json(self.config.get('layout', 'notes'))
self.notes_layout = pympress.util.layout_from_json(self.config.get('layout', 'notes'), default_notes_layout)
self.validate_layout(self.notes_layout, set(self.placeable_widgets.keys()) - {"annotations"})
except ValueError as e:
logger.exception('Invalid layout')
self.notes_layout = pympress.util.layout_from_json('{"resizeable":true, "orientation":"horizontal", "children":["notes", {"resizeable":false, "children":["current", "next"], "orientation":"vertical"}], "proportions": [0.60, 0.40]}')
self.notes_layout = pympress.util.layout_from_json(default_notes_layout)

try:
self.plain_layout = pympress.util.layout_from_json(self.config.get('layout', 'plain'))
self.plain_layout = pympress.util.layout_from_json(self.config.get('layout', 'plain'), default_plain_layout)
self.validate_layout(self.plain_layout, set(self.placeable_widgets.keys()) - {"notes"})
except ValueError as e:
logger.exception('Invalid layout')
self.plain_layout = pympress.util.layout_from_json('{"resizeable":true, "orientation":"horizontal", "children":["current", {"resizeable":true, "orientation":"vertical", "children":["next", "annotations"], "proportions":[0.55, 0.45]}], "proportions":[0.67, 0.33]}')
self.plain_layout = pympress.util.layout_from_json(default_plain_layout)

self.rearrange_p_layout(self.notes_layout if self.notes_mode else self.plain_layout)

Expand Down
9 changes: 6 additions & 3 deletions pympress/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ def load_config():
config.set('presenter', 'show_annotations', 'off')

if not config.has_option('layout', 'notes'):
config.set('layout', 'notes', '{}')
config.set('layout', 'notes', '')

if not config.has_option('layout', 'plain'):
config.set('layout', 'plain', '{}')
config.set('layout', 'plain', '')

if not config.has_option('scribble', 'color'):
config.set('scribble', 'color', Gdk.RGBA(1., 0., 0., 1.).to_string())
Expand All @@ -228,11 +228,14 @@ def recursive_unicode_to_str(obj):
return obj


def layout_from_json(layout_string):
def layout_from_json(layout_string, default_string):
''' Load the layout from config, with all strings cast to type 'str' (even on python2 where they default to 'unicode')
Raises ValueError until python 3.4, json.decoder.JSONDecodeError afterwards, on invalid input.
'''

if not layout_string:
layout_string = default_string

layout = json.loads(layout_string)

try:
Expand Down

0 comments on commit 33fcb57

Please sign in to comment.