#! /usr/bin/python3.5 # Last edited on 2017-06-13 23:06:49 by stolfilocal # Copyright (c) 2007, Peter Corke - See note at end of file # pcbib_db class # - essentially a container for many pcbib_entry objects # - provides methods for reading/writing the bibliography # - provides iterators, sorting etc import string; import pcbib_entry; import urllib; import urlparse; import os; import os.path; import sys; NoSuchFile = "No such file"; class pcbib_db: # An instance of this class is a dictionary that maps citation keys # to entries (class {pcbib_entry}). def __init__(self): self.citeKeyList = []; # List of citation keys of all entries in desired order. self.abbrevDict = {}; # Dictionary of abbreviations. self.filename = ""; # To be defined by {open}. def open(self, filename): "Opens {filename} for reading, returns the file" \ " pointer. If {filename} is '-', returns" \ " {sys.stdin}. Also sets {self.filename} to the absolute file name." if filename == '-': self.filename = "stdin"; return sys.stdin; if not os.path.isfile(filename): raise NoSuchFile; rd = open(filename, "r"); home = os.path.expanduser('~'); absname = os.path.abspath(filename); common = os.path.commonprefix([home, absname]); if common: self.filename = "~" + absname[len(common):] else: self.filename = filename; return rd; def close(self, rd): rd.close(); # resolve all abbreviations found in the values of all fields of all entries def resolveAbbrev(self): #print >> sys.stderr, len(self.abbrevDict); for entry in self: for field in entry: value = entry.getField(field); if isinstance(value,str): if value in self.abbrevDict: if self.abbrevDict[value]: entry.setField(field, self.abbrevDict[value]); def insertEntry(self, entry, ignore=False): #print >> sys.stderr, "inserting key ", entry.getCiteKey() # should check to see if entry is of pcbib_entry type citeKey = entry.getCiteKey(); if citeKey in [x.citeKey for x in self.citeKeyList]: if not ignore: print >> sys.stderr, "key %s already in dictionary" % (citeKey) return False; self.citeKeyList.append(entry); return True; def insertAbbrev(self, abbrev, value): #print >> sys.stderr, "inserting abbrev ", abbrev if abbrev in self.abbrevDict: #print >> sys.stderr, "abbrev %s already in list" % (abbrev) return False; self.abbrevDict[abbrev] = value; #entry.brief(); return True; def getFilename(self): return self.filename; def getAbbrevs(self): return self.abbrevDict; def display(self): for entry in self: entry.display(); print >> sys.stderr def __contains__(self, citeKey): return citeKey in [x.citeKey for x in self.citeKeyList]; def __getitem__(self, i): if type(i) is str: index = [x.citeKey for x in self.citeKeyList].index(i); return self.citeKeyList[index]; elif type(i) is int: return self.citeKeyList[i]; else: raise; def __len__(self): return len(self.citeKeyList); def sort(self, sortfunc): # turn the dictionary of entries into a list so we can sort it self.citeKeyList.sort(sortfunc); # return list of all bibentry's that match the search spec def search(self, field, str, entryType="all", caseSens=0): if str == '*': return self.citeKeyList; result = []; if string.lower(entryType) == "all": for entry in self: if entry.search(field, str, caseSens): result.append(entry); else: for entry in self: if entry.isEntryType(entryType) and entry.search(field, str, caseSens): result.append(entry); return result; def __repr__(self): print >> sys.stderr def brief(self): for entry in self: entry.brief(); # ---------------------------------------------------------------------- # 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.