def save_document (self, * arg): if self.data.key is None: self.save_document_as () return file = self.data.key.url [2] if not self.modification_check (): return Utils.set_cursor (self.w, 'clock') try: try: self.data.update (Sort.Sort([Sort.KeySort()])) except (OSError, IOError), error: Utils.set_cursor (self.w, 'normal') Utils.error_dialog_s(_(u"Unable to save “%s”:\n%s") % (str (self.data.key), str (error))) return except: etype, value, tb = sys.exc_info () traceback.print_exception (etype, value, tb) Utils.set_cursor (self.w, 'normal') Utils.error_dialog_s(_(u"An internal error occurred during saving\nTry to Save As…")) return Utils.set_cursor (self.w, 'normal') # get the current modification date self.modification_date = os.stat (file) [stat.ST_MTIME] self.update_status (0) return
def sort_by_field (self, field): if field == '-key-': mode = Sort.KeySort () elif field == '-type-': mode = Sort.TypeSort () else: mode = Sort.FieldSort (field) # Check if we are toggling or changing cur = self.selection.sort if cur and len (cur.fields) == 1: cur = cur.fields [0] # We are still filtering according to the same field, # simply toggle the direction if cur == mode: mode.ascend = - cur.ascend self.selection.sort = Sort.Sort ([mode]) self.redisplay_index () return
def autosave (self, url, how): ''' autosave file as x-pyblio-save-filename ''' if self.data.key.url [0] != 'file': return False name = self.data.key.url [2] # create an autosave file save = os.path.join (os.path.dirname (name), 'x-pyblio-save-' + os.path.basename (name)) if self.changed: try: savefile = open (save, 'w') except (IOError, OSError), error: Utils.error_dialog_s(_("Error during autosaving:\n%s") % error [1]) return False iterator = Selection.Selection (sort = Sort.Sort([Sort.KeySort()])) Open.bibwrite (iterator.iterator (self.data.iterator ()), out = savefile, how = how, database=self.data) savefile.close ()
import sys from Pyblio import Selection, Sort how = sys.argv[2] if how == '': how = None a = bibopen(sys.argv[3], how) f = open(sys.argv[4], 'w') # write out in the key order sel = Selection.Selection(sort=Sort.Sort([Sort.KeySort()])) it = sel.iterator(a.iterator()) bibwrite(it, out=f, how=a.id)
def __init__(self, sort, parent=None): """ Create the dialog. - current_sort: the current sorting options - parent: parent window """ Utils.Builder.__init__(self, parent) self._model = gtk.ListStore (gtk.gdk.Pixbuf, str, gobject.TYPE_PYOBJECT, int) self._w_tree.set_model (self._model) # Only the first two rows are visibles, the others are for # internal bookkeeping. col = gtk.TreeViewColumn ('Direction', gtk.CellRendererPixbuf (), pixbuf = 0) self._w_tree.append_column (col) col = gtk.TreeViewColumn ('Field', gtk.CellRendererText (), text = 1) self._w_tree.append_column (col) self._w_sort.show () self._icon = {0: None} for id, stock in ((1, gtk.STOCK_GO_UP), (-1, gtk.STOCK_GO_DOWN)): self._icon [id] = self._w_tree.render_icon (stock_id = stock, size = gtk.ICON_SIZE_MENU, detail = None) # Gather the current sort info for all the available # fields. We create a tuple containing: # (display name, sort criterion instance, sort direction) # # The sort direction can be: # 0: not sorting according to this # 1: ascending sort # -1: descending sort if sort: sort = sort.fields else: sort = [] # These are additional search fields avail = [[_("[Entry Type]"), Sort.TypeSort (), 0], [_("[Key Value]"), Sort.KeySort (), 0]] fields = map (lambda x: x.name, Config.get ('base/fields').data.values ()) fields.sort () def make_one (x): return [x, Sort.FieldSort (x.lower ()), 0] avail = avail + map (make_one, fields) # Update the list with the current user settings for v in avail: if v [1] not in sort: continue i = sort.index (v [1]) if sort [i].ascend: v [2] = +1 else: v [2] = -1 # In the list, display the fields that are used in the current # sort procedure first. for k, v, d in filter (lambda x: x [2], avail): s = self._icon [d] self._model.append ((s, k, v, d)) s = self._icon [0] for k, v, d in filter (lambda x: x [2] == 0, avail): self._model.append ((s, k, v, d)) return
if os.path.exists (url): if not Utils.Callback ( _(u"The file “%s” already exists.\nOverwrite it?") % url, parent = self.w).answer (): return try: file = open (url, 'w') except IOError, error: Utils.error_dialog_s(_("During opening:\n%s") % error [1]) return Utils.set_cursor (self.w, 'clock') iterator = Selection.Selection (sort = Sort.Sort([Sort.KeySort()])) Open.bibwrite (iterator.iterator (self.data.iterator ()), out = file, how = how, database=self.data) file.close () # remove the old autosave object if self.data.key is not None and self.source_id: gobject.source_remove (self.source_id) # remove old autosave file if self.data.key: if self.data.key.url [0] == 'file': old_file = self.data.key.url [2] old_auto_save = os.path.join (os.path.dirname (old_file), 'x-pyblio-save-' + os.path.basename (old_file))