#! /usr/bin/python3.5 # Last edited on 2017-06-13 23:08:24 by stolfilocal # Copyright (c) 2007, Peter Corke - See note at end of file # Defines pcbibtex_entry, subclass of pcbib_entry, and provides all BibTeX specific methods such as # writing an entry to file import pcbib_db; import pcbib_entry; import string; import re; import sys; import urllib; class pcbibtex_entry(pcbib_entry.pcbib_entry): # write a BibTeX format entry def write(self, file=sys.stdout, stringdict=None): file.write( "@%s{%s,\n" % (self.getEntryType(), self.getCiteKey()) ); count = 0 for rk in self.fieldDict: count += 1; # skip internally used fields if (rk == 'year_numeric') or (rk == 'month_numeric') or (rk == 'entry_type'): continue; # generate the entry value = self.fieldDict[rk]; file.write(" %s = " % rk ); if rk in ['author', 'editor']: file.write("{%s}" % " and ".join(value) ); elif rk == 'month': if value: file.write("{%s}" % value ); else: value = self.getMonthName(); file.write("%s" % value[0:3].lower() ); else: # is it an abbrev? if value in self.bibliography.abbrevDict: file.write("%s" % value ); else: file.write("{%s}" % value ); # add comma to all but last fields if count < len(self.fieldDict): file.write(",\n"); else: file.write("\n"); file.write("}\n\n"); def setField(self, field, value): "Sets the field of {self} called {field} to the" \ " given {value}. A single level of braces or" \ " doublequotes will be stripped from value. Leading" \ " and trailing blanks will be stripped too.\n" \ "The {field} is mapped to lower case (with warning) if not already.\n" \ "If the {field} is 'author' or 'editor', splits the {value} at" \ " the 'and' keyword and strips blanks (but not braces or quotes).\n" def strip_blanks(s): s = string.strip(s, ' '); return s; # ----------------------------------------------- def strip_braces_and_quotes(s): "Strips a single level of braces or" \ " doublequotes will be stripped from value. Leading" \ " and trailing blanks will be stripped too." s = string.strip(s, ' '); if ((s[0] == '"') and (s[-1] == '"')) or ((s[0] == '{') and (s[-1] == '}')): s = string.strip(s[1:-1], ' '); return s; # ----------------------------------------------- # deal specially with author/editor list, convert from bibtex X and Y to # a list for bibentry class if field.lower() in ["author", "editor"]: value = string.split(value, " and "); value = map(strip_blanks, value); # Insert in entry, print error if failed: try: # invoke the superclass pcbib_entry.pcbib_entry.setField(self, field, value); except AttributeError, err: print >> sys.stderr, "%s" % err; # Copyright (c) 2007, Peter Corke # # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * The name of the copyright holder may not be used to endorse or # promote products derived from this software without specific prior # written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF # THE POSSIBILITY OF SUCH DAMAGE.