def test03(self): ag = Fields.AuthorGroup() ag.append(Fields.Author("Jäger, Herbert")) item = {'author': ag, 'title': 'Bonjour tristesse'} r = userformat.author_title_format(item) self.assertEqual(r, 'Jäger, H.: Bonjour tristesse')
def test02(self): ag = Fields.AuthorGroup() ag.append(Fields.Author("Françoise Sagan")) item = {'author': ag, 'title': 'Bonjour tristesse'} r = userformat.author_title_format(item) self.assertEqual(r, 'Sagan, F.: Bonjour tristesse')
def _delete(self, *args): self.current = Fields.Reference([]) self.string = _("[ Drop an Entry here ]") self.edit.set_text(self.string) return
def StartViewer(entry, key, stringuri, parent=None, document=None): if not is_interactive(): return from Pyblio.GnomeUI import Utils uri = Fields.URL(stringuri) scheme, location, path, parameters, query, fragment = uri.url fileuri = uri.get_url() if uri.invalid or uri.inexact: message = Utils.Callback( _("Warning: This URL is marked as Invalid or Approximate: %s\nContinue?" ) % fileuri) if not message.answer(): return if document: document.statusbar.push(document.context_id, _(u"Determining Mime Type… ")) try: mimetype = gio.content_type_guess(fileuri) except RuntimeError, mesg: Utils.error_dialog( _("Cannot determine mime type for item %s ") % entry.key.key, _("URL in question is: %s\n" "You should check the url or path given for errors.\n" "Details: %s") % (fileuri, mesg)) if document: document.statusbar.pop(document.context_id) return
def format(database, style, output, file=sys.stdout, id='Bibliography'): output = Autoload.get_by_name("output", output).data url = None style = os.path.splitext(style)[0] if os.path.exists(style + '.xml'): url = Fields.URL(style + '.xml') else: from Pyblio import version full = os.path.join(version.pybdir, 'Styles', style) full = full + '.xml' if os.path.exists(full): url = Fields.URL(full) Utils.generate(url, output, database, database.keys(), file) return
def __init__(self, URL=None, action='enter/edit', parent=None): gtk.Button.__init__(self) self.parent_widget = parent self.url = URL or Fields.URL() self.newuri = None assert isinstance(URL, Fields.URL) self.action = action assert action in Resource.CHOOSER_ACTIONS, "Invalid file chooser action" self.connect("clicked", self.cb_clicked) self.box = gtk.HBox(False, 4) self.add(self.box) self.image = gtk.Image() pixbuf = self.render_icon(gtk.STOCK_NEW, gtk.ICON_SIZE_MENU) self.image.set_from_pixbuf(pixbuf) self.box.pack_start(self.image, False, False, 0) self.label = gtk.Label(self.url.get_url() or _('Enter/Select an URL')) self.label.set_ellipsize(pango.ELLIPSIZE_START) self.label.set_alignment(0.0, 0.5) self.box.pack_start(self.label) self.sep = gtk.VSeparator() self.box.pack_start(self.sep, False, False, 0) self.icon = gtk.image_new_from_stock(gtk.STOCK_SAVE, gtk.ICON_SIZE_MENU) self.box.pack_start(self.icon, False, False, 0) self.title = "Enter/Edit URL" self.dialog = None self.current_name = None self.show_all()
def update_content(self, entry, text): group = Fields.AuthorGroup() for author in string.split(text, '\n'): author = string.strip(author) if author == '': continue fields = string.split(author, ',') if len(fields) == 1: (first, last, lineage) = (None, fields[0], None) elif len(fields) == 2: (first, last, lineage) = (fields[1], fields[0], None) else: (first, last, lineage) = (fields[2], fields[0], fields[1]) group.append(Fields.Author((None, first, last, lineage))) entry[self.field] = group return
def test01(self): ag = Fields.AuthorGroup() ag.append(Fields.Author("Bruce Rind")) item = {'author': ag} r = userformat.author_editor_format(item) self.assertEqual(r, 'Rind, B.') ag.append(Fields.Author("Otto Forster")) r = userformat.author_editor_format(item) self.assertEqual(r, 'Rind/Forster') ag.append(Fields.Author("Nicolas Bourbaki")) r = userformat.author_editor_format(item) self.assertEqual(r, 'Rind/Forster/Bourbaki') ag.append(Fields.Author("Léonid Kameneff")) r = userformat.author_editor_format(item) self.assertEqual(r, 'Rind, B. et al.')
def update(self, entry): if self.current == self.initial: return 0 if self.current == Fields.Reference([]): del entry[self.field] return 1 entry[self.field] = self.current return 1
def update(self): if self.node and self.buff.get_modified(): start, end = self.buff.get_bounds() key = self.node['key'] text = self.buff.get_text(start, end).encode('latin-1') if text.strip(): self.entry[key] = Fields.LongText(text) else: del self.entry[key] self.node.setdefault('modified', True)
def parse_author(self, text): ag = Fields.AuthorGroup() rx = re.compile('\.(?:$|\s+)') ry = re.compile('(.)') for name in rx.split(text): if not name: continue la = name.split() if len(la) == 1: last, first = la[0], None else: last = ' '.join(la[:-1]) first = la[-1] first = ry.sub(r'\1. ', first) auth = Fields.Author(copy=(None, first, last, None)) ag.append(auth) return ag
def lt_new(self): d = LT_Dialog_1(parent=self.w.get_toplevel()) newname = d.run() if not newname: return new = {'key': newname.lower(), 'name': newname, 'mandatory': False} pos = len(self.lt_nodes) self.lt_nodes.append(new) assert self.lt_nodes[pos] == new self.set_buttons(len(self.lt_nodes) - 1) self.entry[new['key']] = Fields.LongText('') self.lt_listvw.display_list(self.entry, self.lt_nodes) self.lt_display(pos)
def run(self): response = gtk.FileChooserDialog.run(self) uri = self.get_uri() invalid = self.invalid_w.get_active() inexact = self.inexact_w.get_active() date = self.date_w.get_text() note = self.note_w.get_text() #print 'DIALOG RUN:', uri, invalid, inexact, date, note return response, Fields.URL(uri, invalid=invalid, inexact=inexact, date=date, note=note)
def setup(self, entry): if entry.has_key(self.field): (self.current, self.loss) = entry.field_and_loss(self.field) if self.current.list: self.string = string.join( map(lambda x: x.key, self.current.list), ', ') else: self.string = _("[ Drop an Entry here ]") else: (self.current, self.loss) = (Fields.Reference([]), 0) self.string = _("[ Drop an Entry here ]") self.initial = self.current return
def display(self, node, item): self.node = node self.entry = item self.label.set_text(node.get('name', _('Text'))) key = node['key'] if not item.has_key(key): item[key] = Fields.LongText(_('Enter text here')) self.buff.set_text(str(item[key]).decode('latin-1'), -1) self.buff.set_modified(False) self.page.show_all() if self.hidden: self.hidden = False self.notebook.insert_page(self.page, self.label, self.position) self.notebook.page_num(self.page) self.notebook.set_current_page(self.position)
def bibopen (entity, how = None): ''' Generic function to open a bibliographic database ''' def simple_try (url, how): # url is Fields.URL instance, only to be passed to opener base = None if how == None: listedmethods = Autoload.available ("format") for method in listedmethods: opener = get_by_name (method, 'open') if opener: base = opener (url, 1) if base is not None: return base return None opener = get_by_name (how, 'open') if opener: base = opener (url, 0) else: raise Exceptions.FormatError (_(u"method “%s” provides no opener") % how) return base # Consider the reference as an URL: url is an Fields.URL instance url = Fields.URL (entity) if url.url [0] == 'file' and not os.path.exists (url.url [2]): raise Exceptions.FileError (_(u"File “%s” does not exist") % url.get_url ()) # eventually load a new module if how is None: handler = Autoload.get_by_regexp ("format", url.get_url ()) if handler: how = handler.name base = simple_try (url, how) if base is None: raise Exceptions.FormatError (_(u"don’t know how to open “%s”") % entity) return base
def drag_received(self, *arg): selection = arg[4] info = arg[5] if not info == Mime.KEY: return keys = string.split(selection.data, '\n') reflist = [] for k in keys: (base, key) = string.split(k, '\0') if not base: base = None reflist.append(Key.Key(base, key)) self.current = Fields.Reference(reflist) text = string.join(map(lambda x: x.key, self.current.list), ', ') self.edit.set_text(text) return
def update(self, entry): (day, month, year) = (None, None, None) text = string.strip(self.day.get_chars(0, -1)).encode('latin-1') if text != '': try: day = int(text) except ValueError: Utils.error_dialog_s(self.day.get_toplevel(), ("Invalid day field in date")) return -1 text = string.strip(self.month.get_chars(0, -1)).encode('latin-1') if text != '': try: month = int(text) except ValueError: Utils.error_dialog_s(self.day.get_toplevel(), ("Invalid day field in date")) return -1 text = string.strip(self.year.get_chars(0, -1)).encode('latin-1') if text != '': try: year = int(text) except ValueError: Utils.error_dialog_s(self.day.get_toplevel(), ("Invalid day field in date")) return -1 if self.initial == (day, month, year): return 0 if (day, month, year) == (None, None, None): del entry[self.field] return 1 try: entry[self.field] = Fields.Date((year, month, day)) except Exceptions.DateError, error: Utils.error_dialog_s(self.day.get_toplevel(), str(error)) return -1
def bibnew (name, type = None): opener = get_by_name (type, 'new') if opener is None: if os.path.exists (name): raise IOError, "file `%s' exists" % name file = open (name, 'w') file.close () return bibopen (name, type) # Consider the reference as an URL url = list (urlparse.urlparse (name)) if url [0] == '': # Consider we handle a local file url [0] = 'file' url [2] = os.path.expanduser (url [2]) return opener (Fields.URL(url))
def open_document (self, url, how = None, no_name = False): Utils.set_cursor (self.w, 'clock') orig_url = Fields.URL (url) url = orig_url.get_url () restore = False if orig_url.url [0] == 'file': name = orig_url.url [2] auto_save = os.path.join (os.path.dirname (name), 'x-pyblio-save-' + os.path.basename (name)) if os.path.exists (auto_save): mod_date = os.stat (name) [stat.ST_MTIME] mod_date_auto = os.stat (auto_save) [stat.ST_MTIME] if mod_date < mod_date_auto: restore = Utils.Callback (_("An autosave file was found which is newer than the original file.\nDo you want to restore it?"), self.w).answer () if restore: url = auto_save try: data = Open.bibopen (url, how = how) except (Exceptions.ParserError, Exceptions.FormatError, Exceptions.FileError, Exceptions.DateError), error: Utils.set_cursor (self.w, 'normal') Utils.error_dialog (_("Open error"), error, parent = self.w) return
class TextBase(BaseField): ''' Virtual class common to Text and Entry ''' def setup(self, entry): BaseField.setup(self, entry) if self.string and self.string[0] == '@': self.string = ' ' + self.string return def update_content(self, entry, text): if text[0] == '@' and hasattr(entry, 'set_native'): try: entry.set_native(self.field, string.lstrip(text[1:])) except Exceptions.ParserError, msg: Utils.error_dialog(_("Error in native string parsing"), str(msg)) return -1 return 1 text = string.lstrip(text) entry[self.field] = Fields.Text(text) return 1
import sys from Pyblio import Fields, Autoload from Pyblio.Open import bibopen from Pyblio.Style import Utils db = bibopen(sys.argv[2]) keys = db.keys() keys.sort() url = Fields.URL(sys.argv[3]) Utils.generate(url, Autoload.get_by_name('output', sys.argv[4]).data, db, keys, sys.stdout)
def next(self): dict = {} # read entry till next blank line text = [] field = '' while 1: line = self.file.readline() if line == '': break line = string.rstrip(line) # starting with a blank ? if line == '' or line[0] == ' ': # ...then we continue the current text text.append(string.lstrip(line)) continue # new entry ? if separator_re.match(line): break # else, this is a new field if field: # save the previous one if needed dict[field] = '\n'.join(text) text = [] # store the name of this new field field = string.lower(line) # don't waste the last field content if field: dict[field] = '\n'.join(text) # did we parse a field ? if len(dict) == 0: return None # create the entry content entry = Base.Entry(type=self.deftype) for key in dict.keys(): if not self.mapping.has_key(key): #print "warning: unused key `%s'" % key continue (name, type) = self.mapping[key] text_type = Types.get_field(name).type # parse a simple text field if type == SimpleField: entry[name] = text_type(string.strip(dict[key])) elif type == KeywordField: text = string.strip(dict[key]) if entry.has_key(name): text = str(entry[name]) + '\n ' + text entry[name] = text_type(text) # parse an author field elif type == AuthorField: entry[name] = self.parse_author(dict[key]) continue # parse a source field elif type == SourceField: dict_key = ' '.join(dict[key].split('\n')) m = self.source_re.match(dict_key.strip()) if m: year, month, day = None, None, None j, v, s, n, p, o, y, d = m.group('journal', 'volume', 'inseries', 'number', 'pages', 'other', 'year', 'month') if s: ### article in a monograph series entry['booktitle'] = Fields.Text(j) if d: entry['beigabevermerk'] = Fields.LongText(d) entry.type = Types.get_entry('incollection') elif j: entry['journal'] = Fields.Text(j) if d and not d.isspace(): dates = d.split() try: month = long_month[dates[0]] except KeyError: pass ## import warnings ## warnings.filterwarnings ('once', ## message='date', ## module='OvidLike') ## warnings.warn ( ## 'OVID: %s is not representable in date ' ## 'field %s' %(dates[0], d), stacklevel=2) if len(dates) > 1: day = int(dates[1]) if v: entry['volume'] = Fields.Text(v) if n: entry['number'] = Fields.Text(n) if p: entry['pages'] = Fields.Text(p) if o: entry['other-note'] = Fields.Text(o) if y: year = int(y) entry['date'] = Fields.Date((year, month, day)) else: print '>>> Error: Source field does not parse correctly:' print dict_key print entry continue return entry
def after(field, year, month=None, day=None): return -Search.DateTester(field, Fields.Date((year, month, day)))
def before(field, year, month=None, day=None): return Search.DateTester(field, Fields.Date((year, month, day)))
def next(self): current = None data = '' table = {} # Skip whitespace while 1: line = self.file.readline() if line == '': return table line = string.rstrip(line) if line != '': break while 1: head = header.match(line) if head: if current: if table.has_key(current): table[current].append(data) else: table[current] = [data] current = string.strip(head.group(1)) data = head.group(2) else: cont = contin.match(line) if cont: data = data + ' ' + cont.group(1) line = self.file.readline() if line == '': break line = string.rstrip(line) if line == '': break # don't forget the last item if current: if table.has_key(current): table[current].append(data) else: table[current] = [data] # create the entry with the actual fields norm = {} type = Types.get_entry('article') if table.has_key('PMID'): norm['url'] = Fields.URL(medurl + table['PMID'][0]) norm['medline-pmid'] = Fields.Text(table['PMID'][0]) del table['PMID'] if table.has_key('UI'): norm[one_to_one['UI']] = Fields.Text(table['UI'][0]) del table['UI'] if table.has_key('AU'): group = Fields.AuthorGroup() for au in table['AU']: # analyze the author by ourself. first, last, lineage = [], [], [] for part in string.split(au, ' '): if part.isupper(): # in upper-case, this is a first name if len(last) > 0: first.append(part) else: # if there is no last name, there can't be a first name last.append(part) else: if len(first) > 0: # there was a first name, this must be a lineage lineage.append(part) else: last.append(part) if len(first) > 1: print "medline: long first name found. skipping." first = first[0:1] if len(first) > 0: first = string.join(first[0], '. ') + '.' else: first = None if len(last) > 0: last = string.join(last, ' ') else: last = None if len(lineage) > 0: lineage = string.join(lineage, ' ') else: lineage = None group.append(Fields.Author((None, first, last, lineage))) norm[one_to_one['AU']] = group del table['AU'] if table.has_key('DP'): fields = string.split(table['DP'][0], ' ') norm[one_to_one['DP']] = Fields.Date(fields[0]) del table['DP'] # The simple fields... for f in table.keys(): f_mapped = one_to_one.get(f, 'medline-%s' % (f.lower())) text_type = Types.get_field(f_mapped).type norm[f_mapped] = text_type(string.join(table[f], " ; ")) return Base.Entry(None, type, norm)
def next (self): lines = {} in_table = {} file_notes, file_time, file_version, file_format = ('','','','') while 1: line = self.file.readline() if line == '': return lines # what does that mean ?? head = xheader.match(line) if not head : pass elif head.group(1) == 'Date:': file_time = time.strftime( "%Y-%m-%d %H:%M", rfc822.parsedate(head.group(2))) elif head.group(1) == 'Notes:': file_notes = string.strip(head.group(2)) elif head.group(1) == 'FN': file_format = head.group(2) elif head.group(1) == 'VR': file_version = head.group(2) elif len(head.group(1)) == 2 : break else : pass self.extraneous.append(line) self.isifileformat = self.isifileformat or "Isifile format %s(%s)" % ( file_format, file_version) self.isifileinfo = self.isifileinfo or "ISI %s (%s) %s" %( file_time, file_notes, login_name) while 1: if line == 'ER':break if head : key = head.group(1) if key == 'ER': break val = head.group(3) if lines.has_key(key): lines[key].append(val) else: lines[key] = [val] else: cont = contin.match(line) if cont : val = cont.group(1) lines[key].append(val) else: break line = self.file.readline() if line == '': break # error situation head = header.match (line) key = 'PT' if lines.has_key(key): if string.strip(lines[key][0])[0] == 'J': del lines [key] else: print 'Warning: Unknown type of entry (%s) -- may need editing.' %( lines[key]) type = Types.get_entry ('article') for key in ( 'AU', 'ED'): if lines.has_key(key): group = Fields.AuthorGroup() for item in lines[key]: if string.strip(item) =='[Anon]' : auth = [item] else: name, firstn = string.split (item, ',') auth = ["%s, " % name] for i in string.strip (firstn): auth.append ("%s. " % i) group.append (Fields.Author("".join(auth))) if key == 'AU': in_table['author'] = group elif key == 'ED': in_table['editor'] = group del lines[key] key, key1, key2 = 'PG', 'BP', 'EP' if lines.has_key(key1) and lines.has_key(key2): if len(lines[key1]) == len(lines[key2]): pages = [] for i in range(len(lines[key1])): firstpg = lines[key1] [i] lastpg = lines[key2] [i] pages.append(('%s -- %s' % (firstpg, lastpg))) in_table['pages'] = Fields.Text (string.join(pages, '; ')) del lines[key1]; del lines[key2] else: print 'inconsistent BP, EP fields found' if lines.has_key(key): in_table['size'] = Fields.Text ('%s p.' %(lines[key][0])) del lines[key] key = 'PY' if lines.has_key(key): val = lines[key][0] in_table['date'] = Fields.Date(val) del lines[key] key = 'ID' if lines. has_key(key): val = "[ISI:] %s ;;" %(string.lower(string.join(lines[key], ' '))) if lines.has_key('DE'): lines['DE'].append ('; ' + val) else : lines['DE'] = [val] del lines[key] # journal titles come in various forms if lines.has_key ('SO'): uc_title = ' '.join(lines['SO']) in_table ['journal'] = Fields.Text (uc_title) if lines.has_key('JI'): uc_title = re.split(r"([- .,/]+)", uc_title) ca_title = re.split(r"[- .,/]+", ' '.join(lines['JI'])) i , Title = 0, [] for word in uc_title: Word = string.capitalize(word) if word == ca_title [i]: Title.append (word) i += 1 elif Word.startswith(ca_title[i]): Title.append(Word) i += 1 else: Title.append(string.lower(word)) del lines['JI'] in_table['journal'] = Fields.Text ("".join(Title)) del lines['SO'] for key in lines.keys(): mapped_key, joiner = key_map.get( key, ('isifile-%s' %(key.lower()), ' ; ')) text_type = Types.get_field (mapped_key).type in_table [mapped_key] = text_type (joiner.join(lines[key])) return Base.Entry ( None, type, in_table)
# get option values for opt, value in optlist: if opt == '-o' or opt == '--output': outfile = value continue if opt == '-s' or opt == '--style': style = value continue # get the specified style url = None style = os.path.splitext(style)[0] if os.path.exists(style + '.xml'): url = Fields.URL(style + '.xml') else: from Pyblio import version full = os.path.join(version.pybdir, 'Styles', style) full = full + '.xml' if os.path.exists(full): url = Fields.URL(full) if not url: error(_(u"can’t find style “%s”") % style) spstyle = os.path.split(style)[1] # adjust parameters to the chosen style if spstyle == 'abbrvau': sep = '; ' format = 'textau'
def set_fields (self, * arg): from Pyblio.GnomeUI import Fields Fields.run (self.w) return
def next(self): data = '' fields = {} type = None label = None while 1: line = self.file.readline() if line == '' and not fields: return None line = string.strip(line) if line == '' and fields: # store the current field if type: if type == "label": label = string.join(string.split(data), ' ') elif fields.has_key(type): fields[type].append( string.join(string.split(data), ' ')) else: fields[type] = [string.join(string.split(data), ' ')] # determine the real type while 1: if fields.has_key('journal'): type = 'article' break if fields.has_key('booktitle'): type = 'inbook' break if fields.has_key('volume') or fields.has_key('number'): type = 'inproceedings' break if fields.has_key('publisher'): type = 'book' break if fields.has_key('author') and fields.has_key('title'): type = 'unpublished' break type = 'misc' break entry = Types.get_entry(type) for f in fields.keys(): type = Types.get_field(f).type if type == Fields.AuthorGroup: group = Fields.AuthorGroup() for auth in fields[f]: group.append(Fields.Author(auth)) fields[f] = group else: if len(fields[f]) > 1: sys.stderr.write( "warning: field `%s' is defined more than once" % f) continue fields[f] = type(fields[f][0]) if label: key = Key.Key(None, label) return Base.Entry(key, entry, fields) else: return Base.Entry(None, entry, fields) t = tag_re.match(line) # we matched a new field start if t: if type: if type == "label": label = string.join(string.split(data), ' ') elif fields.has_key(type): fields[type].append( string.join(string.split(data), ' ')) else: fields[type] = [string.join(string.split(data), ' ')] type = t.group(1) if not self.mapping.has_key(type): print "warning: key `%s' has been skipped" % (type) type = None data = '' else: # store the current field type = self.mapping[type][0] data = t.group(2) continue # in the general case, append the new text data = data + ' ' + line
def update_content(self, entry, text): entry[self.field] = Fields.URL(text) return