def writer (iter, output, **argh): entry = iter.first () while entry: ekeys = {} for k in entry.keys (): ekeys [k] = 1 med = one_to_one ['UI'] if entry.has_key (med): del ekeys [med] ui = str (entry [med]) else: print "warning: entry has no medline reference" ui = 0 output.write ('%-4.4s- %s\n' % ('UI', ui)) med = one_to_one ['AU'] if entry.has_key (med): del ekeys [med] for auth in entry [med]: first = auth.first or '' compact = [] for seq in string.split (first, ' '): if len (seq) > 0: compact.append (seq [0]) first = string.join (compact, '') text = string.join ((auth.last or '', first, auth.lineage or ''), ' ') output.write ('%-4.4s- %s\n' % ('AU', text)) med = one_to_one ['DP'] if entry.has_key (med): del ekeys [med] output.write ('%-4.4s- %s\n' % ('DP', str (entry [med]))) # write the remaining know fields for key in one_to_one.keys (): field = one_to_one [key] if not ekeys.has_key (field): continue del ekeys [field] output.write ('%-4.4s- %s\n' % (key, Utils.format (str (entry [field]), 75, 0, 6))) # write the unknown fields remaining = ekeys.keys () remaining.sort () for field in remaining: if field [0:8] == 'medline-': key = string.upper (field [8:]) output.write ('%-4.4s- %s\n' % (key, Utils.format (str (entry [field]), 75, 0, 6))) entry = iter.next () if entry: output.write ('\n')
def __str__(self): ''' Nice standard entry ''' tp = self.type.name fields = self.type.fields try: text = '%s [%s]\n' % (tp, self.key.key) except AttributeError: text = '%s [no key]\n' %(tp) text = text + ('-' * 70) + '\n' dico = self.keys () for f in fields: name = f.name lcname = string.lower(name) if not self.has_key(lcname): continue text = text + ' %-14s ' % name text = text + Utils.format(str(self[lcname]), 75, 17, 17) [17:] text = text + '\n' try: dico.remove(lcname) except ValueError: raise ValueError, \ 'multiple definitions of field `%s\' in `%s\'' \ % (name, tp) for f in dico: text = text + ' %-14s ' % f text = text + Utils.format(str(self[f]), 75, 17, 17) [17:] text = text + '\n' return text
def output_write(key, text): # A text is either a string or a list: if type(text) == types.ListType: output.write ('%2s %s\n' %(key, text[0])) for t in text[1:]: output.write (' %s\n' %(t)) elif str(text): output.write ('%2s %s\n' % (key, Utils.format( str (text), 70, 0, 3)))
def writer (iter, output, **argh): entry = iter.first () mapping = Config.get ("refer/mapping").data while entry: for key in mapping.keys (): # some fields are not to be used in output, as we # lost their content if not mapping [key] [1]: continue field = mapping [key] [0] if field == "label" and entry.key: output.write ('%' + key + ' ') output.write (Utils.format (str (entry.key.key), 75, 0, 0)) output.write ('\n') continue elif entry.has_key (field): type = Types.get_field (field).type if type == Fields.AuthorGroup: # one field per author for auth in entry [field]: output.write ('%' + key + ' ') output.write (Utils.format (str (auth), 75, 0, 0)) output.write ('\n') continue # general case output.write ('%' + key + ' ') output.write (Utils.format (str (entry [field]), 75, 0, 0)) output.write ('\n') entry = iter.next () if entry: output.write ('\n') return
def writer(iter, output, **argh): entry = iter.first() mapping = Config.get("refer/mapping").data while entry: for key in mapping.keys(): # some fields are not to be used in output, as we # lost their content if not mapping[key][1]: continue field = mapping[key][0] if field == "label" and entry.key: output.write('%' + key + ' ') output.write(Utils.format(str(entry.key.key), 75, 0, 0)) output.write('\n') continue elif entry.has_key(field): type = Types.get_field(field).type if type == Fields.AuthorGroup: # one field per author for auth in entry[field]: output.write('%' + key + ' ') output.write(Utils.format(str(auth), 75, 0, 0)) output.write('\n') continue # general case output.write('%' + key + ' ') output.write(Utils.format(str(entry[field]), 75, 0, 0)) output.write('\n') entry = iter.next() if entry: output.write('\n') return
def end (self): self.data = string.strip (self.data) text = Utils.format (self.data, 79, self.length, self.length) self.out.write (self.key + text [self.length:] + '\n\n') return
def test01 (self): for i in self.data: p, r = i[0], i[1:] self.assertEqual (Utils.compress_page_range(p,False), r)
def writer(iter, output, **argh): global one_to_one if one_to_one is None: one_to_one = Config.get('medline/mapping').data entry = iter.first() while entry: ekeys = {} for k in entry.keys(): ekeys[k] = 1 med = one_to_one['UI'] if entry.has_key(med): del ekeys[med] ui = str(entry[med]) else: print "warning: entry has no medline reference" ui = 0 output.write('%-4.4s- %s\n' % ('UI', ui)) med = one_to_one['AU'] if entry.has_key(med): del ekeys[med] for auth in entry[med]: first = auth.first or '' compact = [] for seq in string.split(first, ' '): if len(seq) > 0: compact.append(seq[0]) first = string.join(compact, '') text = string.join( (auth.last or '', first, auth.lineage or ''), ' ') output.write('%-4.4s- %s\n' % ('AU', text)) med = one_to_one['DP'] if entry.has_key(med): del ekeys[med] output.write('%-4.4s- %s\n' % ('DP', str(entry[med]))) # write the remaining know fields for key in one_to_one.keys(): field = one_to_one[key] if not ekeys.has_key(field): continue del ekeys[field] output.write('%-4.4s- %s\n' % (key, Utils.format(str(entry[field]), 75, 0, 6))) # write the unknown fields remaining = ekeys.keys() remaining.sort() for field in remaining: if field[0:8] == 'medline-': key = string.upper(field[8:]) output.write('%-4.4s- %s\n' % (key, Utils.format(str(entry[field]), 75, 0, 6))) entry = iter.next() if entry: output.write('\n')
def entry_write(entry, output): '''Print a single entry as BiBTeX code.''' native = isinstance(entry, Entry) tp = entry.type # write the type and key output.write('@%s{%s,\n' % (tp.name, entry.key.key)) # create a hash containing all the keys, to keep track # of those who have been already written dico = {} datefields = Config.get('bibtex/datefield').data convert = Config.get('bibtex/months').data # we have to handle the special case of the dates # create the list of months monthlist = range(0, 12) for key in convert.keys(): monthlist[convert[key]-1] = key dateformat = Config.get('bibtex+/dateformat').data if native: # loop over all the fields for field in entry.keys(): if datefields.has_key(field): # we are processing a date... date = entry[field] dico[datefields[field][0]] = str(date.year) if date.month: month = monthlist[date.month - 1] if date.day: month = dateformat % {'day': date.day, 'month' : month} dico[datefields[field][1]] = month else: # we are processing a normal entry dico[field] = _bibtex.get_native(entry.dict[field]) else: for field in entry.keys(): # convert the field in a bibtex form if datefields.has_key(field): # we are processing a date... date = entry[field] dico[datefields[field][0]] = str(date.year) if date.month: month = monthlist[date.month - 1] if date.day: month = dateformat % {'day': date.day, 'month' : month} dico[datefields[field][1]] = month else: # we are processing a normal entry value = entry[field] # eventually convert the crossref if isinstance(value, Reference): value = string.join(map(lambda item: item.key, value.list), ', ') fieldtype = _fieldtype(Types.get_field(field)) dico[field] = _nativify(value, fieldtype) first = True # write according to the type order for f in tp.mandatory + tp.optional: # dico contains all the available fields field = string.lower(f.name) if not dico.has_key(field): continue if not first: output.write (',\n') else: first = False output.write(' %-14s = ' % f.name) output.write(Utils.format (dico[field], 75, 19, 19)[19:]) del dico[field] keys = dico.keys() keys.sort() for f in keys: if not first: output.write (',\n') else: first = False output.write(' %-14s = ' % f) output.write(Utils.format(dico[f], 75, 19, 19)[19:]) output.write ('\n}\n\n') return
def entry_write(entry, output): '''Print a single entry as BiBTeX code.''' native = isinstance(entry, Entry) tp = entry.type # write the type and key output.write('@%s{%s,\n' % (tp.name, entry.key.key)) # create a hash containing all the keys, to keep track # of those who have been already written dico = {} datefields = Config.get('bibtex/datefield').data convert = Config.get('bibtex/months').data # we have to handle the special case of the dates # create the list of months monthlist = range(0, 12) for key in convert.keys(): monthlist[convert[key] - 1] = key dateformat = Config.get('bibtex+/dateformat').data if native: # loop over all the fields for field in entry.keys(): if datefields.has_key(field): # we are processing a date... date = entry[field] dico[datefields[field][0]] = str(date.year) if date.month: month = monthlist[date.month - 1] if date.day: month = dateformat % {'day': date.day, 'month': month} dico[datefields[field][1]] = month else: # we are processing a normal entry dico[field] = _bibtex.get_native(entry.dict[field]) else: for field in entry.keys(): # convert the field in a bibtex form if datefields.has_key(field): # we are processing a date... date = entry[field] dico[datefields[field][0]] = str(date.year) if date.month: month = monthlist[date.month - 1] if date.day: month = dateformat % {'day': date.day, 'month': month} dico[datefields[field][1]] = month else: # we are processing a normal entry value = entry[field] # eventually convert the crossref if isinstance(value, Reference): value = string.join(map(lambda item: item.key, value.list), ', ') fieldtype = _fieldtype(Types.get_field(field)) dico[field] = _nativify(value, fieldtype) first = True # write according to the type order for f in tp.mandatory + tp.optional: # dico contains all the available fields field = string.lower(f.name) if not dico.has_key(field): continue if not first: output.write(',\n') else: first = False output.write(' %-14s = ' % f.name) output.write(Utils.format(dico[field], 75, 19, 19)[19:]) del dico[field] keys = dico.keys() keys.sort() for f in keys: if not first: output.write(',\n') else: first = False output.write(' %-14s = ' % f) output.write(Utils.format(dico[f], 75, 19, 19)[19:]) output.write('\n}\n\n') return
def get_native(self, entry): ''' Return the object in its native format ''' stream = Utils.StringStream() entry_write(entry, stream) return stream.text