def importbib(db, bibfile, tags=[], overwrite=False): errors = [] sources = Sources() for entry in sorted(Bibtex(bibfile), key=lambda entry: entry.key): print >>sys.stderr, entry.key try: docs = [] # check for doc with this bibkey bdoc = db.doc_for_bib(entry.key) if bdoc: docs.append(bdoc) # check for known sids for source in sources.scan_bibentry(entry): sdoc = db.doc_for_source(source.sid) # FIXME: why can't we match docs in list? if sdoc and sdoc.docid not in [doc.docid for doc in docs]: docs.append(sdoc) if len(docs) == 0: doc = Document(db) elif len(docs) > 0: if len(docs) > 1: print >>sys.stderr, " Multiple distinct docs found for entry. Using first found." doc = docs[0] print >>sys.stderr, " Updating id:%d..." % (doc.docid) doc.add_bibentry(entry) filepath = entry.get_file() if filepath: print >>sys.stderr, " Adding file: %s" % filepath doc.add_file(filepath) doc.add_tags(tags) doc.sync() except BibtexError as e: print >>sys.stderr, " Error processing entry %s: %s" % (entry.key, e) print >>sys.stderr errors.append(entry.key) if errors: print >>sys.stderr print >>sys.stderr, "Failed to import %d" % (len(errors)), if len(errors) == 1: print >>sys.stderr, "entry", else: print >>sys.stderr, "entries", print >>sys.stderr, "from bibtex:" for error in errors: print >>sys.stderr, " %s" % (error) sys.exit(1) else: sys.exit(0)
def restore(self, log=False): """Restore a database from an existing root.""" docdirs = os.listdir(self.root) docdirs.sort() for ddir in docdirs: if ddir == '.xapers': continue docdir = os.path.join(self.root, ddir) if not os.path.isdir(docdir): # skip things that aren't directories continue if log: print >>sys.stderr, docdir # if we can't convert the directory name into an integer, # assume it's not relevant to us and continue try: docid = int(ddir) except ValueError: continue docfiles = os.listdir(docdir) if not docfiles: # skip empty directories continue if log: print >>sys.stderr, ' docid:', docid try: doc = self[docid] except xapian.DocNotFoundError: doc = Document(self, docid=docid) for dfile in docfiles: dpath = os.path.join(docdir, dfile) if dfile == 'bibtex': if log: print >>sys.stderr, ' adding bibtex' doc.add_bibtex(dpath) elif os.path.splitext(dpath)[1] == '.pdf': if log: print >>sys.stderr, ' adding file:', dfile doc.add_file(dpath) elif dfile == 'tags': if log: print >>sys.stderr, ' adding tags' with open(dpath, 'r') as f: tags = f.read().strip().split('\n') doc.add_tags(tags) doc.sync()
def restore(self, log=False): """Restore a database from an existing root.""" docdirs = os.listdir(self.root) docdirs.sort() for ddir in docdirs: if ddir == '.xapers': continue docdir = os.path.join(self.root, ddir) if not os.path.isdir(docdir): # skip things that aren't directories continue if log: print >> sys.stderr, docdir # if we can't convert the directory name into an integer, # assume it's not relevant to us and continue try: docid = int(ddir) except ValueError: continue docfiles = os.listdir(docdir) if not docfiles: # skip empty directories continue if log: print >> sys.stderr, ' docid:', docid try: doc = self[docid] except xapian.DocNotFoundError: doc = Document(self, docid=docid) for dfile in docfiles: dpath = os.path.join(docdir, dfile) if dfile == 'bibtex': if log: print >> sys.stderr, ' adding bibtex' doc.add_bibtex(dpath) elif os.path.splitext(dpath)[1] == '.pdf': if log: print >> sys.stderr, ' adding file:', dfile doc.add_file(dpath) elif dfile == 'tags': if log: print >> sys.stderr, ' adding tags' with open(dpath, 'r') as f: tags = f.read().strip().split('\n') doc.add_tags(tags) doc.sync()
def importbib(db, bibfile, tags=[], overwrite=False): errors = [] sources = Sources() for entry in sorted(Bibtex(bibfile), key=lambda entry: entry.key): print >> sys.stderr, entry.key try: docs = [] # check for doc with this bibkey bdoc = db.doc_for_bib(entry.key) if bdoc: docs.append(bdoc) # check for known sids for source in sources.scan_bibentry(entry): sdoc = db.doc_for_source(source.sid) # FIXME: why can't we match docs in list? if sdoc and sdoc.docid not in [doc.docid for doc in docs]: docs.append(sdoc) if len(docs) == 0: doc = Document(db) elif len(docs) > 0: if len(docs) > 1: print >> sys.stderr, " Multiple distinct docs found for entry. Using first found." doc = docs[0] print >> sys.stderr, " Updating id:%d..." % (doc.docid) doc.add_bibentry(entry) filepath = entry.get_file() if filepath: print >> sys.stderr, " Adding file: %s" % filepath doc.add_file(filepath) doc.add_tags(tags) doc.sync() except BibtexError as e: print >> sys.stderr, " Error processing entry %s: %s" % ( entry.key, e) print >> sys.stderr errors.append(entry.key) if errors: print >> sys.stderr print >> sys.stderr, "Failed to import %d" % (len(errors)), if len(errors) == 1: print >> sys.stderr, "entry", else: print >> sys.stderr, "entries", print >> sys.stderr, "from bibtex:" for error in errors: print >> sys.stderr, " %s" % (error) sys.exit(1) else: sys.exit(0)