Skip to content

Commit

Permalink
Added error checking & warnings if baseline data file doesn't match s…
Browse files Browse the repository at this point in the history
…ize of current document
  • Loading branch information
robmcmullen committed May 1, 2016
1 parent 6186a12 commit d9da264
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
14 changes: 10 additions & 4 deletions omnivore/framework/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ def set_emulator(self, emu):
self.emulator = emu
self.emulator_change_event = True

def load_baseline(self, uri):
guess = FileGuess(uri)
print "Loading baseline", guess
d = Document(metadata=guess.metadata, bytes=guess.numpy)
def init_baseline(self, metadata, bytes):
d = Document(metadata=metadata, bytes=bytes)
d.parse_segments([])
self.baseline_document = d

def del_baseline(self):
self.baseline_document = None

def update_baseline(self):
if self.baseline_document is not None:
self.change_count += 1
Expand All @@ -206,3 +207,8 @@ def update_baseline(self):
def clear_baseline(self):
self.change_count += 1
self.global_segment.clear_style_bits(diff=True)

@property
def has_baseline(self):
return self.baseline_document is not None

37 changes: 36 additions & 1 deletion omnivore/framework/editor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os

# Major package imports.
import numpy as np
from fs.opener import opener
import wx
import fs
import jsonpickle

# Enthought library imports.
from traits.api import on_trait_change, Any, Bool, Int, Unicode, Property, Dict, List
from pyface.api import YES, NO
from pyface.tasks.api import Editor
from pyface.action.api import ActionEvent

Expand Down Expand Up @@ -77,6 +79,10 @@ class FrameworkEditor(Editor):

mouse_mode = Any

baseline_present = Bool

diff_highlight = Bool

_metadata_dirty = Bool(transient=True)

#### trait default values
Expand Down Expand Up @@ -175,6 +181,34 @@ def process_extra_metadata(self, doc, e):
""" Set up any additional metadata from the dict argument
"""
pass

def load_baseline(self, uri, doc=None):
if doc is None:
doc = self.document
try:
guess = FileGuess(uri)
except Exception, e:
self.window.error("Failed opening baseline document file\n\n%s\n\nError: %s" % (uri, str(e)), "Baseline Document Loading Error")
return
bytes = guess.numpy
difference = len(bytes) - len(doc)
if difference > 0:
if self.task.confirm("Truncate baseline data by %d bytes?" % difference, "Baseline Size Difference") == YES:
bytes = bytes[0:len(doc)]
else:
bytes = []
elif difference < 0:
if self.task.confirm("Pad baseline data with %d zeros?" % (-difference), "Baseline Size Difference") == YES:
bytes = np.pad(bytes, (0, -difference), "constant", constant_values=0)
else:
bytes = []
if len(bytes) > 0:
doc.init_baseline(guess.metadata, bytes)
else:
doc.del_baseline()
if doc == self.document:
self.baseline_present = doc.has_baseline
self.diff_highlight = self.baseline_present

def view_document(self, doc, old_editor=None):
""" Change the view to the specified document
Expand All @@ -191,7 +225,8 @@ def view_document(self, doc, old_editor=None):
def rebuild_document_properties(self):
""" Recreate any editor attributes for the new document
"""
pass
self.baseline_present = self.document.has_baseline
self.diff_highlight = self.diff_highlight and self.baseline_present

def copy_view_properties(self, old_editor):
""" Copy editor properties to the new view
Expand Down
4 changes: 1 addition & 3 deletions omnivore/tasks/hex_edit/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,7 @@ def perform(self, event):
dialog = FileDialog(parent=event.task.window.control)
if dialog.open() == OK:
e = self.active_editor
e.document.load_baseline(dialog.path)
e.diff_highlight = True
e.baseline_present = True
e.load_baseline(dialog.path)
e.compare_to_baseline()
e.refresh_panes()

Expand Down
13 changes: 3 additions & 10 deletions omnivore/tasks/hex_edit/hex_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class HexEditor(FrameworkEditor):

last_anchor_end_index = Int(0)

baseline_present = Bool

diff_highlight = Bool

can_copy_baseline = Bool

#### Events ####
Expand Down Expand Up @@ -159,13 +155,9 @@ def process_extra_metadata(self, doc, e):
if 'initial segment' in e:
self.initial_segment = e['initial segment']
if 'baseline document' in e:
try:
doc.load_baseline(e['baseline document'])
except Exception, e_:
self.window.error("Failed opening baseline document file\n\n%s\n\nError: %s" % (e['baseline document'], str(e_)), "Baseline Document Loading Error")
self.baseline_present = doc.baseline_document is not None
self.load_baseline(e['baseline document'], doc)
if 'diff highlight' in e:
self.diff_highlight = self.baseline_present and bool(e['diff highlight'])
self.diff_highlight = doc.has_baseline and bool(e['diff highlight'])

def get_extra_metadata(self, mdict):
mdict["serialized user segments"] = list(self.document.user_segments)
Expand All @@ -184,6 +176,7 @@ def get_extra_metadata(self, mdict):
mdict["diff highlight"] = self.diff_highlight

def rebuild_document_properties(self):
FrameworkEditor.rebuild_document_properties(self)
self.find_segment()
self.update_emulator()
self.compare_to_baseline()
Expand Down

0 comments on commit d9da264

Please sign in to comment.