Exemplo n.º 1
0
class documentFormatter:
    """ base class for formatting bibliographies """
    def __init__(self, db=None, style="numbered", sortRefs=False):
	self.citekeys = []
	self.sortRefs = sortRefs
	self.style = style
	self.references = bibArray(style)
	self.uniqueCitekeys = []
        self.missingRefs = []
        self.inTextRefs = {}

	self.opening = "["
	self.closing = "]"

	if style == "parenthetical":
	    self.opening = "("
	    self.closing = ")"

	if db:
	    self.db = db
	else:
	    self.db = DbConnection()
	    self.db.guiPrompt()

    def getUniqueCitekeys(self):
	""" return just the unique citekeys """
	if len(self.citekeys) > 1:
	    u = {}
	    for key in self.citekeys: u[key] = key
	    #self.uniqueCitekeys = u.keys()
	    for k in u.keys():
		print "Key: %s" % k
		# remove duplicates here
		if (self.citekeys.count(k) > 1): # if ref occurs more than once
		    indices = []
		    print "Before: %i" % len(self.citekeys)
		    for i in range(0, len(self.citekeys)):
			if self.citekeys[i] == k: # find where this ref occurs
			    indices.append(i)
		    counter = 0
		    for i in range(1, len(indices)): # remove all but first
			print "indices: %s" % indices
			self.citekeys.pop(indices[i] - counter)
			counter += 1 # counter needed since size of array is changing
		    print "After: %i" % len(self.citekeys)
		
	#else:
	#    self.uniqueCitekeys = self.citekeys
		
    def getReferences(self):
	""" attempts to retrieve all the references from the DB """
        for key in self.citekeys:
            try:
                b = bibItem(db=self.db, key=key)
            except invalidDBKeyException:
                self.missingRefs.append(key)
            else:
		if key:
		    print "Inserting key %s" % key
		    self.references.append(b)
                    print "CITATION: %s" % self.references.inTextCitation(b["citekey"])

	if self.sortRefs == True:
	    self.references.sort(refSorter)
        self.references.changeInTextCounts()
	print "Citations: %s" % self.references.getInTextCitations()
        self.formatReferences()
	for i in self.references: print i
        print "IN TEXT REFS %s" % self.inTextRefs
        
    def printMissingRefs(self):
        """ prints a list of missing references to the screen """
        print "Missing keys: "
        for r in self.missingRefs:
            print "   %s" % r

    def formatReferences(self):
	""" creates dict of formatted in text references """
        if self.style == "parenthetical":
            self.references.modifyDuplicates()
        for k in self.inTextRefs.keys():
            formatted = ""
            formattedRefs = []
            refs = self.refsInCitation(k)
            for r in refs:
                try:
                    c = self.references.inTextCitation(r)
                except KeyError:
                    formattedRefs.append("MISSING: %s" % r)
                else:
                    print "THIS WAS C: %s" % c
                    formattedRefs.append("%s" % self.references.inTextCitation(r))

            if len(refs) > 1:
                print "FR: %s" % formattedRefs
                s = ", ".join(formattedRefs)
                formatted = "%s%s%s" % (self.opening, s, self.closing)
            else:
                formatted = "%s%s%s" % (self.opening, formattedRefs[0], self.closing)
            self.inTextRefs[k] = formatted
    
    
    def removeColons(self, reference):
	"""quick hack to replace Doe:2001 with Doe2001"""
	p = re.compile(r':')
	return p.sub('', reference)

    def refsInCitation(self, citation):
        """ get the citekeys inside an unformatted inText citation """
        refs = []
        spaceRemover = re.compile(r'^\s+')
	squiqqlyRemover = re.compile(r'[\{\}]')
        citation = squiqqlyRemover.sub('', citation)
        print "List: %s" % citation
        for reference in citation.split(","):
            reference = spaceRemover.sub('', reference)
	    reference = self.removeColons(reference)
            refs.append(reference)
        return(refs)

    def openFile(self, fileName):
        """ an empty method that needs to be subclassed """
        pass

    def getCitekeys(self):
	""" an empty method that needs to be subclassed """
	pass
    
    def replaceCitekeys(self):
	""" an empty method that needs to be subclassed """
	pass

    def formatBibliography(self):
	""" an empty method that needs to be subclassed """
	pass

    def writeFile(self, output):
	""" an empty method that needs to be subclassed """
	pass

    def formatDocument(self, input=None, output=None):
        """ formats a document - replacing references and creating a ref list"""
        if input:
            self.filename = input
        # open the file - this method needs to be subclassed.
        self.openFile(self.filename)

	# extract the citekeys from the document (handled by the subclass)
        self.getCitekeys()
	# get just the unique citekeys
        self.getUniqueCitekeys()
        print "Unique: %s" % self.uniqueCitekeys
	# get the references from the database
        self.getReferences()
	# replace the citekeys inside the document (handled by subclass)
        if self.style != "unformatted":
            self.replaceCitekeys()
	# format the bibliography (handled by subclass)
        self.formatBibliography()
	# write to file (handled by subclass)
        if output:
            self.writeFile(output)
        self.printMissingRefs()
Exemplo n.º 2
0
        notequery.prepare(QtCore.QString("INSERT INTO note (citekey, note) VALUES (?, ?)"))
        notequery.bindValue(0, QtCore.QVariant(currentRef))
        notequery.bindValue(1, QtCore.QVariant(note))
        notequery.exec_()
        if notequery.lastError().type():
            print "Error inserting note: [%s] %s" % (citekey, note)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)


    fileHandle = open("/micehome/jlerch/Documents/MICE/articles/morris-water-maze/lit_notes.txt", 'r')
    fileContents = fileHandle.read()

    db = DbConnection()
    db.guiPrompt()


    starts = []
    ends = []

    refStarter = re.compile(r'^====')
    noteStarter = re.compile(r'^\*')
    spaceStarter = re.compile(r'^\s')

    lines = fileContents.split("\n")
    counter = 0
    note = None
    currentRef = None
    notecounter = 0
    for l in lines: