def main(): '''Command line version of tool''' import sys import bibgrammar from optparse import OptionParser usage = "%prog [options] filename(s)" parser = OptionParser(usage=usage, version ="%prog " + __version__) parser.add_option("-y", "--yearsep", action="store", type="string", \ dest="yearsep", default = ':', help="char to separate names and year") parser.add_option("-s", "--sep", action="store", type="string", \ dest="sep", default = '+', help="char to separate names") parser.add_option("-m", "--maxnames", action="store", type="int", \ dest="maxnames", default = 2, help="Max names to add to key") parser.add_option("-e", "--etal", action="store", type="string", \ dest="etal", default = 'etal',help="What to add after max names") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Print INFO messages to stdout, default=%default") # get options (options, args) = parser.parse_args() if options.verbose: biblabel_logger.setLevel(logging.INFO) # get database as text from .bib file(s) or stdin if len(args) > 0 : try : src = ''.join(open(f).read() for f in args) except: print 'Error in filelist' else : src = sys.stdin.read() bfile = bibfile.BibFile() bibgrammar.Parse(src, bfile) used_citekeys = [] # stores created keys for entry in bfile.entries: label = make_entry_citekey( entry, used_citekeys, max_names = options.maxnames, sep = options.sep, ysep = options.yearsep, etal = options.etal) #:note: citekey != key, be careful! entry.citekey = label used_citekeys.insert(0,label) #prepend to take advantage (in make_entry_citekey) of possibly sorted bfile for entry in bfile.entries: print entry
def main(): '''Command-line tool. See jabbrev.py -h for help''' input = sys.stdin output = sys.stdout try: from optparse import OptionParser except (ImportError, AttributeError): try: from optik import OptionParser except (ImportError, AttributeError): print "jabbrev needs python 2.3 or Greg Ward's optik module." usage = "usage: %prog [options] DATABASE_FILE [ABBREVIATION_FILE] " parser = OptionParser(usage=usage, version="%prog " + __version__) (options, args) = parser.parse_args() if len(args) > 2: print "Too many arguments" sys.exit(1) try: # bibtex file is last argument bib = open(args[0]).read() except: print "No bibtex file found." sys.exit(1) if len(args) == 2: input = open(args[1]) else: input = sys.stdin.read() bfile = bibfile.BibFile() bibgrammar.Parse(bib, bfile) journals = MakeMap(input.readlines()) Translate(bfile, journals)
reo = re.compile(string_or_compiled, re.MULTILINE) else: #->must have a compiled regular expression reo = string_or_compiled """ Find regex in bib_entry. If field is omitted, search is through all fields. :note: used by bibsearch.py """ ls = [ entry for entry in self.entries if entry.search_fields( string_or_compiled=reo, field=field, ignore_case=ignore_case) ] return ls # self test # ------------------------- # usage: bibfile.py DATABASE_FILE if __name__ == "__main__": import sys if len(sys.argv) > 1: src = open(sys.argv[1]).read() bfile = BibFile() bibgrammar.Parse(src, bfile) for entry in bfile.entries: print entry else: print "self test usage: bibfile.py DATABASE_FILE"
def main(): """Command-line tool. See bibsearch.py -h for help. """ from optparse import OptionParser usage = "usage: %prog [options] FILE [search strings]" parser = OptionParser(usage=usage, version="%prog " + __version__) parser.add_option("-k", "--key", action="store_true", dest="citekey_output", default=False, help="Output citekey rather than reference") parser.add_option("-l", "--long", action="store_true", dest="long_output", default=False, help="Output entire bibtex entry") parser.add_option("-r", "--regex", action="store_true", dest="search_input", default=False, help="Search for regular expression rather than key") parser.add_option("-s", "--stylefile", action="store", dest="stylefile", default="default.py", help="Specify user-chosen style file", metavar="FILE") parser.add_option("-f", "--field", action="store", type="string", dest="field", default=None, help="Search only FIELD; default=%default.", metavar="FIELD") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Print INFO messages to stdout, default=%default") parser.add_option("-V", "--very_verbose", action="store_true", dest="very_verbose", default=False, help="Print DEBUG messages to stdout, default=%default") (options, args) = parser.parse_args() if options.verbose: bibsearch_logger.setLevel(logging.INFO) if options.very_verbose: bibsearch_logger.setLevel(logging.DEBUG) bibsearch_logger.debug("Script running.\nargs=%s\nstyle file=%s" % (args, options.stylefile)) try: src = open(args[0]).read() except: print("Error: No bibtex file found.") sys.exit(1) # If no search string was sepcified was specified, read search strings from stdin if len(args) < 2: searches = string.split(sys.stdin.read()) else: searches = args[1:] # create object to store parsed .bib file parsed_bibfile = bibfile.BibFile() # store a parsed .bib file in parsed_bibfile bibgrammar.Parse(src, parsed_bibfile) # list of entries entrylist = [] if options.field: for s in searches: entrylist.extend( parsed_bibfile.search_entries(s, field=options.field)) elif options.search_input: for s in searches: entrylist.extend(parsed_bibfile.search_entries(s)) else: entrylist = parsed_bibfile.get_entrylist(searches, discard=True) if entrylist: #found some matches -> output the list in desired format result = "" if options.citekey_output: result = "\n".join(e.citekey for e in entrylist) elif options.long_output: result = "\n".join(str(e) for e in entrylist) else: # style based formated references style_stmt = "import bibstyles.%s as style" % os.path.splitext( options.stylefile)[0] exec style_stmt in globals() citation_manager = style.CitationManager( [parsed_bibfile], citekeys=[e.citekey for e in entrylist], citation_template=style.CITATION_TEMPLATE) cite_processor = bibstyles.shared.CiteRefProcessor( citation_manager) result = citation_manager.make_citations() print(result) else: #did not find any matches bibsearch_logger.info("No matches.")
def main(): """Command-line tool. See bib4txt.py -h for help. """ #set default input and output input = sys.stdin output = sys.stdout from optparse import OptionParser usage = """ usage: %prog [options] BIB_DATABASE standard usage: %prog -i reST_FILE -n -o refs_FILE BIB_DATABASE """ parser = OptionParser(usage=usage, version ="%prog " + __version__) parser.add_option("-i", "--infile", action="store", type="string", dest="infile", help="Parse FILE for citation references.", metavar="FILE") parser.add_option("-o", "--outfile", action="store", type="string", dest="outfile", help="Write formatted references to FILE", metavar="FILE") parser.add_option("-n", "--nuke", action="store_true", dest="overwrite", default=False, help="silently overwrite outfile, default=%default") parser.add_option("-s", "--stylefile", action="store", dest="stylefile", default="default.py", help="Specify user-chosen style file",metavar="FILE") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Print INFO messages to stdout, default=%default") parser.add_option("-V", "--very-verbose", action="store_true", dest="very_verbose", default=False, help="Print DEBUG messages to stdout, default=%default") parser.add_option("-a", "--all", action="store_true", dest="entire_doc", default=False, help="Output entire document, making citation reference substitutions, default=%default") parser.add_option("-x", "--xp", action="store_true", dest="xp_parse", default=False, help="Use experimental document parser, default=%default") parser.add_option("-L", "--logger-level", action="store", type="int", dest="logger_level", help="Set logging level to integer value.") (options, args) = parser.parse_args() if options.logger_level: bib4txt_logger.setLevel(options.logger_level) elif options.very_verbose: bib4txt_logger.setLevel(logging.DEBUG) elif options.verbose: bib4txt_logger.setLevel(logging.INFO) bib4txt_logger.info( "Script running.\nargs=%s\ninfile=%s\noutfile=%s\nstyle file=%s" %(args, options.infile, options.outfile,options.stylefile) ) exec("import bibstyles.%s as style"%os.path.splitext(options.stylefile)[0]) # open output file for writing (default: stdout) if options.outfile: if os.path.exists(options.outfile) and not options.overwrite: print "File %s exists: use -n option to nuke (overwrite) this file."%(options.outfile) print "PLEASE CHECK FILE NAME CAREFULLY!" sys.exit(1) output = open(options.outfile,'w') # read database (.bib) files bibfile_names = args bibfile_as_string = bibfiles2string(bibfile_names) if not bibfile_as_string: bib4txt_logger.warning("No BibTeX databases found.") sys.exit(1) # read input file (default: stdin) if options.infile: try: input = open(options.infile,'r') except: print "Cannot open: "+options.infile sys.exit(1) if options.entire_doc: ebnf_dec = ebnf_sp.cites_rest else: ebnf_dec = ebnf_sp.cites_only_rest if options.xp_parse: ebnf_dec = ebnf_sp.cites_xp # Create a simpleparse.parser Parser based on the chosen grammar cite_parser = simpleparse.parser.Parser(ebnf_dec, root='src') # create object to store parsed .bib file bibfile_processor = bibfile.BibFile() bib4txt_logger.debug('Ready to parse bib file.') #store parsed .bib files in the bibfile_processor bibgrammar.Parse(bibfile_as_string, bibfile_processor) bib4txt_logger.info('bib file parsed.') result = make_text_output( input.read(), cite_parser, bibfile_processor, style, citations_only = not options.entire_doc) output.write(result) output.close() input.close()