def load(self, uri): self.file_uri = uri xml_file = gio.File(uri) self.document = XMLReader(ignore_errors=True).fromFile(xml_file.read()) self.document.finalize() self.window.registry.add(self.document) self.window._info_bar.show_info("Loading of %s done!" % (xml_file.get_basename()))
def test_read_write(self): doc = Document("author") sec = Section("name", "type") doc.append(sec) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) if sys.version_info < (3, 0): str_doc = unicode(XMLWriter(doc)) else: str_doc = str(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] p = new_sec.properties["strprop"] assert (p.dtype == "string") if sys.version_info < (3, 0): assert (type(p.value[0]) == unicode) else: assert (type(p.value[0]) == str) p = new_sec.properties["txtprop"] assert (p.dtype == "text") if sys.version_info < (3, 0): assert (type(p.value[0]) == unicode) else: assert (type(p.value[0]) == str) p = new_sec.properties["intprop"] assert (p.dtype == "int") assert (type(p.value[0]) == int) p = new_sec.properties["floatprop"] assert (p.dtype == "float") assert (type(p.value[0]) == float) p = new_sec.properties["datetimeprop"] assert (p.dtype == "datetime") assert (type(p.value[0]) == dt) p = new_sec.properties["dateprop"] assert (p.dtype == "date") assert (type(p.value[0]) == date) p = new_sec.properties["timeprop"] assert (p.dtype == "time") assert (type(p.value[0]) == time) p = new_sec.properties["boolprop"] assert (p.dtype == "boolean") assert (type(p.value[0]) == bool)
def test_read_write(self): doc = Document("author") sec = Section("name", "type", parent=doc) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) str_doc = str(XMLWriter(doc)) new_doc = XMLReader().from_string(str_doc) new_sec = new_doc.sections[0] prop = new_sec.properties["strprop"] self.assertEqual(prop.dtype, "string") self.assertIsInstance(prop.values[0], str) prop = new_sec.properties["txtprop"] self.assertEqual(prop.dtype, "text") self.assertIsInstance(prop.values[0], str) prop = new_sec.properties["intprop"] self.assertEqual(prop.dtype, "int") self.assertIsInstance(prop.values[0], int) prop = new_sec.properties["floatprop"] self.assertEqual(prop.dtype, "float") self.assertIsInstance(prop.values[0], float) prop = new_sec.properties["datetimeprop"] self.assertEqual(prop.dtype, "datetime") self.assertIsInstance(prop.values[0], dt) prop = new_sec.properties["dateprop"] self.assertEqual(prop.dtype, "date") self.assertIsInstance(prop.values[0], date) prop = new_sec.properties["timeprop"] self.assertEqual(prop.dtype, "time") self.assertIsInstance(prop.values[0], time) prop = new_sec.properties["boolprop"] self.assertEqual(prop.dtype, "boolean") self.assertIsInstance(prop.values[0], bool)
def test_read_write(self): doc = Document("author") sec = Section("name", "type") doc.append(sec) sec.append(Property("strprop", "somestring")) sec.append(Property("txtprop", "some\ntext")) sec.append(Property("intprop", 200)) sec.append(Property("floatprop", 2.00)) sec.append(Property("datetimeprop", dt.now())) sec.append(Property("dateprop", dt.now().date())) sec.append(Property("timeprop", dt.now().time())) sec.append(Property("boolprop", True)) str_doc = unicode(XMLWriter(doc)) new_doc = XMLReader().fromString(str_doc) new_sec = new_doc.sections[0] v = new_sec.properties["strprop"].value assert (v.dtype == "string") assert (type(v.data) == unicode) v = new_sec.properties["txtprop"].value assert (v.dtype == "text") assert (type(v.data) == unicode) v = new_sec.properties["intprop"].value assert (v.dtype == "int") assert (type(v.data) == int) v = new_sec.properties["floatprop"].value assert (v.dtype == "float") assert (type(v.data) == float) v = new_sec.properties["datetimeprop"].value assert (v.dtype == "datetime") assert (type(v.data) == dt) v = new_sec.properties["dateprop"].value assert (v.dtype == "date") assert (type(v.data) == date) v = new_sec.properties["timeprop"].value assert (v.dtype == "time") assert (type(v.data) == time) v = new_sec.properties["boolprop"].value assert (v.dtype == "boolean") assert (type(v.data) == bool)
class EditorTab(object): """ Represents a Document Object in the Editor """ file_uri = None edited = 0 def __init__(self, window, cmdm=None): if cmdm is None: cmdm = CommandManager() cmdm.enable_undo = self.enable_undo cmdm.enable_redo = self.enable_redo cmdm.error_func = window.command_error self.command_manager = cmdm self.document = None self.window = window self._clones = [self] def new(self, doc=None): """ initialize a new document """ if doc is None: doc = odml.Document() sec = odml.Section(name="Default Section") doc.append(sec) self.window.registry.add(doc) self.document = doc self.file_uri = None def load(self, uri): self.file_uri = uri xml_file = gio.File(uri) self.document = XMLReader(ignore_errors=True).fromFile(xml_file.read()) self.document.finalize() self.window.registry.add(self.document) self.window._info_bar.show_info("Loading of %s done!" % (xml_file.get_basename())) # TODO select default section def reset(self): self.edited = 0 # initialize the edit stack position self.command_manager.reset() self.enable_undo(enable=False) self.enable_redo(enable=False) @property def is_modified(self): return self.edited != len(self.command_manager) def save_if_changed(self): """ if the document was modified, ask the user if he or she wants to save the document returns false if the user cancelled the action """ if not self.is_modified: return True dialog = gtk.MessageDialog( self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, "%s has been modified. Do you want to save your changes?" % self.file_uri) dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) dialog.set_title("Save changes?") dialog.set_default_response(gtk.RESPONSE_CANCEL) response = dialog.run() dialog.destroy() if response == gtk.RESPONSE_CANCEL: return False if response == gtk.RESPONSE_NO: return True return self.window.save(None) def save(self, uri): self.document.clean() doc = XMLWriter(self.document) gf = gio.File(uri) try: data = unicode(doc) except Exception, e: self._info_bar.show_info("Save failed: %s" % e.message) return xml_file = gf.replace(etag='', make_backup=False) # TODO make backup? xml_file.write(doc.header) xml_file.write(data) xml_file.close() self.document.finalize() # undo the clean self.window._info_bar.show_info("%s was saved" % (gf.get_basename())) self.edited = len(self.command_manager) return True # TODO return false on any error and notify the user
class EditorTab(object): """ Represents a Document Object in the Editor """ file_uri = None edited = 0 def __init__(self, window, cmdm=None): if cmdm is None: cmdm = CommandManager() cmdm.enable_undo = self.enable_undo cmdm.enable_redo = self.enable_redo cmdm.error_func = window.command_error self.command_manager = cmdm self.document = None self.window = window self._clones = [self] def new(self, doc=None): """ initialize a new document """ if doc is None: doc = odml.Document() sec = odml.Section(name="Default Section") doc.append(sec) self.window.registry.add(doc) self.document = doc self.file_uri = None def load(self, uri): self.file_uri = uri xml_file = gio.File(uri) self.document = XMLReader(ignore_errors=True).fromFile(xml_file.read()) self.document.finalize() self.window.registry.add(self.document) self.window._info_bar.show_info("Loading of %s done!" % (xml_file.get_basename())) # TODO select default section def reset(self): self.edited = 0 # initialize the edit stack position self.command_manager.reset() self.enable_undo(enable=False) self.enable_redo(enable=False) @property def is_modified(self): return self.edited != len(self.command_manager) def save_if_changed(self): """ if the document was modified, ask the user if he or she wants to save the document returns false if the user cancelled the action """ if not self.is_modified: return True dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, "%s has been modified. Do you want to save your changes?" % self.file_uri) dialog.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) dialog.set_title("Save changes?") dialog.set_default_response(gtk.RESPONSE_CANCEL) response = dialog.run() dialog.destroy() if response == gtk.RESPONSE_CANCEL: return False if response == gtk.RESPONSE_NO: return True return self.window.save(None) def save(self, uri): self.document.clean() doc = XMLWriter(self.document) gf = gio.File(uri) try: data = unicode(doc) except Exception, e: self._info_bar.show_info("Save failed: %s" % e.message) return xml_file = gf.replace(etag='', make_backup=False) # TODO make backup? xml_file.write(doc.header) xml_file.write(data) xml_file.close() self.document.finalize() # undo the clean self.window._info_bar.show_info("%s was saved" % (gf.get_basename())) self.edited = len(self.command_manager) return True # TODO return false on any error and notify the user