def on_do_action(self, action_name): descr = introsp.description(self.edition.current.__class__) actions = descr.actions_for(self.edition.current, self.edition.parent) for action in actions: if action.name == action_name: if isinstance(action, introsp.ActionOnAChild): descr = introsp.description(self.edition.parent.__class__) descr.do_action(action, undoredo.stack, self.edition.parent, self.edition.current) else: descr.do_action(action, undoredo.stack, self.edition.current) break else: if action_name == "Add": descr = introsp.description(self.edition.parent.__class__) index = descr.children_of(self.edition.parent).index(self.edition.current) + 1 actions = descr.actions_for(self.edition.parent) for action in actions: if action.name == action_name: descr.do_action(action, undoredo.stack, self.edition.parent, index) break if action_name == "Remove": # On ne peut plus éditer l'objet en cours car il a été supprimé ! self.edition.edit(self.edition.root, None) self.send_edition()
def edit(self, parent, object): self.p = parent self.o = object self.set_button_visibilities( self.p and self.o and introsp.ACTION_MOVE_UP .filter(self.p, self.o), (self.o and introsp.ACTION_ADD in introsp.description(self.o.__class__).actions_for(self.o)) or (self.p and introsp.ACTION_ADD in introsp.description(self.p.__class__).actions_for(self.p)), self.p and self.o and introsp.ACTION_REMOVE in introsp.description(self.o.__class__).actions_for(self.o, self.p), self.p and self.o and introsp.ACTION_MOVE_DOWN.filter(self.p, self.o), )
def on_add(self, *args): if self.o and introsp.ACTION_ADD in introsp.description(self.o.__class__).actions_for(self.o): o = self.o index = None else: o = self.p try: index = introsp.description(o.__class__).children_of(o).index(self.o) + 1 except: index = None introsp.description(o.__class__).do_action(introsp.ACTION_ADD, self.undo_stack, o, index)
def update(self): self.updating = 1 try: v = self.descr.get(self.o, self.attr) if (v is introsp.NonConsistent) and isinstance(self.o, introsp.ObjectPack): v = introsp.ObjectPack( [ introsp.description(o.__class__).get(o, self.attr) for o in self.o.objects if self.attr in introsp.description(o.__class__).attrs_of(o) ] ) self.attribute_pane.edit(v) finally: self.updating = 0
def on_add(self, *args): if self.o and introsp.ACTION_ADD in introsp.description(self.o.__class__).actions_for(self.o): o = self.o index = None else: o = self.p try: index = introsp.description(o.__class__).children_of(o).index(self.o) + 1 except: index = None new_child = introsp.description(o.__class__).do_action(introsp.ACTION_ADD, self.undo_stack, o, index) if self.hierarchy_pane: self.hierarchy_pane.expand_object(o) self.hierarchy_pane.select_object(new_child)
def __init__(self, gui, master, o, attr, undo_stack): self.o = o self.attr = attr self.descr = introsp.description(o.__class__) self.master = master self.undo_stack = undo_stack self.updating = 0
def on_add_instrument(self, event = None): try: if isinstance(self.selected_partition, introsp.ObjectPack): selected_partition = self.selected_partition.objects[-1] else: selected_partition = self.selected_partition insert_at = self.song.partitions.index(selected_partition) + 1 except: insert_at = len(self.song.partitions) new_child = introsp.description(self.song.__class__).do_action(introsp.ACTION_ADD, self.undo_stack, self.song, insert_at)
def _action_activated(self, drop_it, o, action, parent): if action.pass_editor_in_args: if isinstance(self.master, EditorPane): if isinstance(self.master.master, EditorDialog): editor = self.master.master else: editor = self.master else: editor = self if isinstance(action, introsp.ActionOnAChild): descr = introsp.description(parent.__class__) descr.do_action(action, self.undo_stack, parent, o, editor) else: descr = introsp.description(o.__class__) descr.do_action(action, self.undo_stack, o, editor) else: if isinstance(action, introsp.ActionOnAChild): descr = introsp.description(parent.__class__) descr.do_action(action, self.undo_stack, parent, o) else: descr = introsp.description(o.__class__) descr.do_action(action, self.undo_stack, o)
def set_value(self, value): if self.updating: return self.updating = 1 try: if isinstance(self.o, introsp.ObjectPack): objects = [o for (i, o) in enumerate(self.o.objects) if self.attr in self.o.attrs[i]] old_values = [introsp.description(o.__class__).get(o, self.attr) for o in objects] def do_it (): for o in objects: introsp.description(o.__class__).set(o, self.attr, value) def undo_it(): for i, o in enumerate(objects): introsp.description(o.__class__).set(o, self.attr, old_values[i]) a = undoredo.UndoableOperation(do_it, undo_it, editobj2.TRANSLATOR("change of %s") % editobj2.TRANSLATOR(self.attr), self.undo_stack) else: old_value = introsp.description(self.o.__class__).get(self.o, self.attr) def do_it (): self.descr.set(self.o, self.attr, value) def undo_it(): self.descr.set(self.o, self.attr, old_value) a = undoredo.UndoableOperation(do_it, undo_it, editobj2.TRANSLATOR("change of %s") % editobj2.TRANSLATOR(self.attr), self.undo_stack) finally: self.updating = 0
def edit(self, o): if o is self.o: return if self.root_node: self.root_node.destroy() self.o = o if not o is None: self.descr = introsp.description(o.__class__) self.root_node = self.Node(self.tree, o) if self.childhood_pane: self.childhood_pane.edit(None, o)
def __init__(self, parent_node, o): self.descr = introsp.description(o.__class__) self.o = o self.o_children_getter = self.descr.children_getter_of(self.o) self.o_children = None self.o_has_children = self.descr.has_children(self.o) super(HierarchyNode, self).__init__(parent_node) observe.observe(self.o, self._listener) if isinstance(self.o_has_children, list) or isinstance(self.o_has_children, set) or isinstance(self.o_has_children, dict): observe.observe(self.o_has_children, self._listener)
def set_value(self, value): if isinstance(self.o, introsp.ObjectPack): o = self.o.objects[0] old = introsp.description(o.__class__).get(o, self.attr) else: old = self.get_value() if (old is True) or (old is False): if value: MultiGUIField.set_value(self, True) else: MultiGUIField.set_value(self, False) else: if value: MultiGUIField.set_value(self, 1) else: MultiGUIField.set_value(self, 0)
def test_getset_1(self): d = introsp.description(IntrospTest) assert set(d.attributes.keys()) == set(["b", "c", "x", "y", "z"]) assert d.attributes["b"].getter is None assert d.attributes["b"].setter is not None assert d.attributes["c"].getter == "get_c" assert d.attributes["c"].setter is not None assert d.attributes["x"].getter == "get_x" assert d.attributes["x"].setter == "set_x" assert d.attributes["y"].getter == "getY" assert d.attributes["y"].setter == "setY" assert d.attributes["z"].getter is not None assert d.attributes["z"].setter == "set_z"
def edit(self, o): if o is self.o: return if not self.o is None: observe.unobserve(self.o, self._listener) self.o = o self.descr = introsp.description(o.__class__) if not o is None: self._update() observe.observe(self.o, self._listener) else: self._set_icon_filename_label_details("", "", "")
def on_about(self, *args): class About(object): def __init__(self): self.details = _(u"__about__") self.icon_filename = os.path.join(globdef.DATADIR, "songwrite_about.png") self.url = u"http://home.gna.org/oomadness/en/songwrite/index.html" self.licence = u"GPL" self.authors = "Jean-Baptiste 'Jiba' Lamy <*****@*****.**>" def __unicode__(self): return "Songwrite 2 version %s" % model.VERSION descr = introsp.description(About) descr.set_field_for_attr("details" , None) descr.set_field_for_attr("icon_filename", None) self.edit(About())
def run(self): form = cgi.FieldStorage() if form.has_key("_get_image"): image_filename = form["_get_image"].value if image_filename.endswith(".png") or image_filename.endswith(".jpeg") or image_filename.endswith(".gif"): self.send_image(image_filename) return if not form.has_key("EDITOBJ2_obj"): self.on_new_edition() else: self.edition = loads(form["EDITOBJ2_obj"].value) new_current = None do_action = None for key in form.keys(): if key == "EDITOBJ2_obj": pass elif key.startswith("_edit_"): new_parent, new_current = key[6:].split(".") new_parent = self.edition.id2obj(int(new_parent )) new_current = self.edition.id2obj(int(new_current)) elif key.startswith("_do_action_"): do_action = key[11:] else: id, attr = key.split(".", 1) o = self.edition.id2obj(int(id)) descr = introsp.description(o.__class__) Field = descr.field_for_attr(o, attr) field = Field("Html", self, o, attr, undoredo.stack) if isinstance(field, editobj2.field.BoolField): if isinstance(form.getvalue(key), list): field.set_value(1) else: field.set_value(0) else: try: field.set_html_value(form[key].value) except: print >> sys.stderr, sys.exc_info() if new_current: self.on_change_edition(new_parent, new_current) elif do_action: self.on_do_action(do_action) else: self.on_validate()
def edit(self, o): if o is self.o: return self.descr = introsp.description(o.__class__) if not o is None: attrs = [(self.descr.priority_for_attr(o, attr), editobj2.TRANSLATOR(attr), attr, self.descr.field_for_attr(o, attr)) for attr in self.descr.attrs_of(o)] attrs = [priority_name_attr_Field for priority_name_attr_Field in attrs if priority_name_attr_Field[-1]] attrs.sort() else: attrs = [] if o and self.o and (self.attrs == attrs): if not self.o is None: observe.unobserve(self.o, self._listener) self.o = o self.property_attrs = self.descr.property_attrs_of(o) self.attrs = attrs if not o is None: for field in self.fields.itervalues(): field.edit(o) observe.observe(self.o, self._listener) else: if not self.o is None: observe.unobserve(self.o, self._listener) self._delete_all_fields() self.o = o self.property_attrs = self.descr.property_attrs_of(o) self.attrs = attrs if not o is None: self._set_nb_fields(len(attrs)) i = 0 self.fields = {} for priority, name, attr, Field in attrs: if (Field is editobj2.field.ObjectAttributeField) and isinstance(self.master, editobj2.field.ObjectAttributeField): Field = editobj2.field.EntryField field = self.fields[attr] = Field(self.gui, self, o, attr, self.undo_stack) self._new_field(name, field, self.descr.unit_for_attr(o, attr), i) i += 1 observe.observe(self.o, self._listener)
def _listener(self, o, type, new, old): self.update() if (type is list) or (type is set) or (type is dict): if self.o_children is not None: observe.unobserve(self.o_children, self._listener) self.o_children = self.descr.children_of(self.o) observe.observe(self.o_children, self._listener) self.update_children() elif type is object: if self.o_children is not None: if (self.o_children_getter in new.keys()) or (self.o_children_getter in old.keys()): # XXX Optimizable : verify if self.o_children_getter is a string AND new[self.o_children_getter] == old[self.o_children_getter] observe.unobserve(self.o_children, self._listener) self.o_children = self.descr.children_of(self.o) observe.observe(self.o_children, self._listener) self.update_children() elif type == "__class__": self.descr = introsp.description(self.o.__class__) self.update_children()
for download in dm.downloads[:]: if download.progress < 1.0: download.progress = download.progress + 0.001 * download.speed if download.progress >= 1.0: download.progress = 1.0 download.completed = 1 return 1 def run_downloader(): import time time.sleep(1.0) # Wait for the app to start up while 1: downloader() time.sleep(0.2) descr = introsp.description(Download) descr.set_field_for_attr("progress", field.ProgressBarField) descr.set_field_for_attr("speed", field.RangeField(0, 100), "Ko/s") descr.set_field_for_attr("completed", field.BoolField) def f(obj, field, undo_stack): print obj, field, undo_stack descr.set_field_for_attr("local_filename", field.WithButtonField(field.LabelField, u"Test", f)) descr.set_icon_filename(os.path.join(os.path.dirname(sys.argv[0]), "./file.png")) descr = introsp.description(DownloadManager) descr.set_details("To add a new download, click on the '+' button on the right.\nUse the '-' button to remove and cancel a download.") descr.set_children_getter( "downloads", # Children None, # Has children method lambda download_manager: Download("http://", ""), # New children method "insert", # Add method
elif self.path.endswith(".py"): return os.path.join(editobj2._ICON_DIR, "python.png") elif self.path.endswith(".png") or self.path.endswith(".jpeg") or self.path.endswith(".jpg"): return self.path else: return os.path.join(os.path.dirname(sys.argv[0]), "./file.png") def has_children(self): return os.path.isdir(self.path) def get_children(self): if self.children is None: if os.path.isdir(self.path): self.children = [File(os.path.join(self.path, filename)) for filename in os.listdir(self.path) if not filename.startswith(".")] #self.children = range(10) else: self.children = [] return self.children descr = introsp.description(File) descr.set_icon_filename(lambda o: o.get_icon_filename()) descr.set_field_for_attr("icon_filename", None) descr.set_field_for_attr("size", field.IntField, unit = "Ko") descr.set_children_getter("children", None, "has_children") file = File(os.path.join("..")) if "--gtk" in sys.argv: editobj2.GUI = "Gtk" elif "--tk" in sys.argv: editobj2.GUI = "Tk" elif "--qt" in sys.argv: editobj2.GUI = "Qt" elif "--qtopia" in sys.argv: editobj2.GUI = "Qtopia" editobj2.edit(file).main()
def __unicode__(self): return self.module.__name__ mod = Module(editobj2, os.path.join(os.path.dirname(sys.argv[0]), "./dialog.png")) mod.url = "http://home.gna.org/oomadness/en/editobj" mod.version = editobj2.VERSION mod.submodules.append(Module(introsp)) mod.submodules.append(Module(observe)) mod.submodules.append(Module(undoredo)) mod.submodules.append(Module(editor)) mod.submodules.append(Module(field)) # mod.submodules.append(Module(treewidget)) descr = introsp.description(Module) descr.set_field_for_attr("details", None) descr.set_field_for_attr("icon_filename", None) descr.set_children_getter("submodules") # The following are not needed because EditObj2 is smart enough to guess them; # they are kept only for documentation purpose. # descr.set_field_for_attr("submodules", None) # descr.set_details(lambda o: o.details) # descr.set_label (lambda o: unicode(o)) if "--gtk" in sys.argv: editobj2.GUI = "Gtk" elif "--tk" in sys.argv: editobj2.GUI = "Tk"
class Config(object): def __init__(self): self.connection = Connection() self.security = Security() self.identification = Identification() self.plugins = [ Plugin("GPG", command = "gpg"), Plugin("SVG"), Plugin("OggVorbis", driver_filename = "/dev/dsp") ] config = Config() # os.path.join(os.path.dirname(sys.argv[0]), "./jiba.png") descr = introsp.description(Identification) descr.set_label("Identification") descr.set_field_for_attr("age", field.IntField, unit = "years") descr.set_icon_filename(os.path.join(os.path.dirname(sys.argv[0]), "./jiba.png")) descr = introsp.description(Connection) descr.set_label("Connection") descr.set_field_for_attr("type" , field.EnumField(["modem", "DSL", "ADSL"])) descr.set_field_for_attr("speed", field.RangeField(0, 512), unit = "Ko/s") descr = introsp.description(Security) descr.set_label("Security") descr.set_field_for_attr("level" , field.EnumField({"low":0, "medium":1, "high":2, "paranoid":3})) descr.set_field_for_attr("allow_javascript", field.BoolField) descr.set_field_for_attr("block_popup" , field.BoolField)
def do_it (): for o in objects: introsp.description(o.__class__).set(o, self.attr, value)
def undo_it(): for i, o in enumerate(objects): introsp.description(o.__class__).set(o, self.attr, old_values[i])
def _get_actions(self, o, parent): actions = list(introsp.description(o.__class__).actions_for(o, parent)) for action in actions: action.label = editobj2.TRANSLATOR(action.name) actions.sort(lambda a, b: cmp(a.label, b.label)) return actions
def on_move_down(self, *args): introsp.description(self.p.__class__).do_action(introsp.ACTION_MOVE_DOWN, self.undo_stack, self.p, self.o)
def test_getset_2(self): d = introsp.description(IntrospTest) i = IntrospTest() assert d.attrs_of(i) == set(['a', 'c', 'b', 'y', 'x', 'z'])
def __init__(self): self.a = 0.0 self.b = 1.0 def get_result(self): return self.a / self.b class Calculator(object): def __init__(self): self.operations = [ Sum(), Substract(), Multiply(), Divide(), ] descr = introsp.description(Sum) descr.set_label("Sum") descr = introsp.description(Substract) descr.set_label("Substract") descr = introsp.description(Multiply) descr.set_label("Multiply") descr = introsp.description(Divide) descr.set_label("Divide") # Hint: descr = introsp.description(Calculator)
choices = {} for octavo in range(8): for note in range(12): x = note + 12 * octavo choices[model.note_label(x)] = x NoteValueField = editobj2.field.EnumField(choices, long_list = 1, translate = 0) DurationField = NoteValueField introsp.set_field_for_attr("details" , None) introsp.set_field_for_attr("icon_filename", None) descr = introsp.description(globdef.Config) descr.set_label (_("Preferences")) descr.set_details(_("__config_preamble__")) descr.set_field_for_attr("MIDI_USE_TEMP_FILE", None) descr.set_field_for_attr("PREVIOUS_FILES", None) descr.set_field_for_attr("ASK_FILENAME_ON_EXPORT", field.BoolField) descr.set_field_for_attr("PAGE_FORMAT", field.EnumField({ _("A3") : "a3paper", _("A4") : "a4paper", _("A5") : "a5paper", _("letter") : "letterpaper" })) #descr.set_field_for_attr("SIDE_BAR_POSITION", field.EnumField(["left", "right", "top", "bottom", "floating_horizontal", "floating_vertical", "none"])) descr.set_field_for_attr("SIDE_BAR_POSITION", None) descr.set_field_for_attr("PLAY_AS_TYPING", field.BoolField) descr.set_field_for_attr("DISPLAY_PLAY_BAR", field.BoolField) descr.set_field_for_attr("NUMBER_OF_PREVIOUS_FILES", field.IntField) descr.set_field_for_attr("AUTOMATIC_DURATION", field.BoolField) descr.set_field_for_attr("FONTSIZE", field.IntField) descr.set_field_for_attr("ENGLISH_CHORD_NAME", field.BoolField)
def on_remove(self, *args): introsp.description(self.p.__class__).do_action(introsp.ACTION_REMOVE, self.undo_stack, self.p, self.o)