Spires.py: Difference between revisions
Jump to navigation
Jump to search
(take account of curly brackets in SPIRES BiBTeX titles) |
mNo edit summary |
||
Line 193: | Line 193: | ||
data = f.read() | data = f.read() | ||
if | if r'\begin{thebibliography}' in data: | ||
data = data[:data.find(r'\begin{thebibliography}')] | data = data[:data.find(r'\begin{thebibliography}')] | ||
citations = [] | citations = [] | ||
while | while r'\cite{' in data: | ||
data = data[data.find(r'\cite{') + 6:] | data = data[data.find(r'\cite{') + 6:] | ||
if | if '}' in data: | ||
citation = data[:data.find('}')] | citation = data[:data.find('}')] | ||
data = data[data.find('}')+1:] | data = data[data.find('}')+1:] | ||
citation = | citation += ',' | ||
while | while ',' in citation: | ||
if citation[:citation.find(',')] not in citations: | if citation[:citation.find(',')] not in citations: | ||
citations.append(citation[:citation.find(',')]) | citations.append(citation[:citation.find(',')]) | ||
Line 214: | Line 214: | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
authorOpt = | authorOpt = False | ||
titleOpt = | titleOpt = False | ||
bibtexOpt = | bibtexOpt = False | ||
bibitemOpt = | bibitemOpt = False | ||
citeOpt = | citeOpt = False | ||
verboseOpt = | verboseOpt = False | ||
libraryOpt = | libraryOpt = False | ||
downloadOpt = | downloadOpt = False | ||
try: | try: | ||
Line 244: | Line 244: | ||
elif o == '--download': | elif o == '--download': | ||
downloadOpt = | downloadOpt = True | ||
if a == '': | if a == '': | ||
a = './' | a = './' | ||
elif a[-1] != '/': | elif a[-1] != '/': | ||
a = | a += '/' | ||
downloadPath = os.path.expanduser(a) | downloadPath = os.path.expanduser(a) | ||
print 'download path is ' + downloadPath | print 'download path is ' + downloadPath | ||
elif o == '-b': | elif o == '-b': | ||
bibtexOpt = | bibtexOpt = True | ||
elif o == '-i': | elif o == '-i': | ||
bibitemOpt = | bibitemOpt = True | ||
elif o == '-a': | elif o == '-a': | ||
authorOpt = | authorOpt = True | ||
elif o == '-t': | elif o == '-t': | ||
titleOpt = | titleOpt = True | ||
elif o == '-c': | elif o == '-c': | ||
citeOpt = | citeOpt = True | ||
elif o == '-e': | elif o == '-e': | ||
bibtexOpt = | bibtexOpt = True | ||
authorOpt = | authorOpt = True | ||
titleOpt = | titleOpt = True | ||
citeOpt = | citeOpt = True | ||
elif o == '-v': | elif o == '-v': | ||
verboseOpt = | verboseOpt = True | ||
if len(arguments) != 1: | if len(arguments) != 1: | ||
print | print "you didn't specify a SPIRES reference; try 'spires.py -h' for more information" | ||
sys.exit(0) | sys.exit(0) | ||
else: | else: | ||
Line 283: | Line 283: | ||
if len(options) == 0: | if len(options) == 0: | ||
bibtexOpt = | bibtexOpt = True | ||
authorOpt = | authorOpt = True | ||
titleOpt = | titleOpt = True | ||
citeOpt = | citeOpt = True | ||
Line 294: | Line 294: | ||
print 'the reference ' + ref + ' is a(n) ' + type | print 'the reference ' + ref + ' is a(n) ' + type | ||
if | if bibtexOpt or authorOpt or titleOpt or citeOpt or libraryOpt: | ||
BiBTeX = getBiBTeX(ref,type) | BiBTeX = getBiBTeX(ref,type) | ||
if 'no records' in BiBTeX: | if 'no records' in BiBTeX: | ||
Line 317: | Line 317: | ||
print title | print title | ||
if | if citeOpt or libraryOpt: | ||
cite = extractcite(BiBTeX) | cite = extractcite(BiBTeX) | ||
Revision as of 11:42, 25 December 2009
Copy the following text into a file called spires.py with your favourite text editor
#! /usr/bin/python ## SPIRES script version 0.3 ## Copyright 2007 Tom Brown ## This program is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License as ## published by the Free Software Foundation; either version 3 of the ## License, or (at your option) any later version. ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## You should have received a copy of the GNU General Public License ## along with this program. If not, see <http://www.gnu.org/licenses/>. ## See http://www.stringwiki.org/wiki/SPIRES_script for more usage ## instructions '''SPIRES script Usage: python spires.py reference [ -hbiatcev ] [ --help ] [ --library library.bib ] [ --download download_path/ ] "reference" must be a standard arXiv reference, e.g. hep-th/9711200, 0705.0303, Maldacena:1997re or a SPIRES journal reference, e.g. CMPHA,43,199 Options: -h, --help displays this help message -b displays the BiBTeX entry -i displays the bibitem entry -a displays the author(s) -t displays the title -c displays the TeX citation key -e displays everything -v verbose mode --download download_path/ for arXiv references downloads a pdf of the paper from the arXiv to the directory download_path/ --library library.bib if it is not already in library.bib, appends the BiBTeX entry to library.bib; use at your own risk ''' __version__ = "0.2" __author__ = "Tom Brown" __copyright__ = "Copyright 2007 Tom Brown, GNU GPL 3" import sys, os, getopt, re, urllib def findRefType(ref): ref = ref.replace('arxiv:','') if re.search(r'^[a-zA-Z\-]+/\d{7}$',ref): type = 'old-style eprint' elif re.search(r'^\d{7}$',ref): type = 'old-style eprint' ref = 'hep-th/' + ref elif re.search('^\d{4}\.\d{4}$',ref): type = 'new-style eprint' elif re.search(r'^\D+:\d{4}[a-zA-Z]{2}$',ref): type = 'texkey' else: type = 'journal' return type, ref def getBiBTeX(ref,type): if type == 'old-style eprint': query = 'eprint=' + ref elif type == 'new-style eprint': query = 'eprint=arXiv:' + ref elif type == 'texkey': query = 'texkey=' + ref elif type == 'journal': query = 'j=' + ref else: return "no records were found in SPIRES to match your search, please try again" BiBTeX = urllib.urlopen('http://www.slac.stanford.edu/spires/find/hep/wwwbriefbibtex?' + query + '&server=sunspi5').read() if 'No records' in BiBTeX: return "no records were found in SPIRES to match your search, please try again" BiBTeX = BiBTeX[BiBTeX.find('<!-- START RESULTS -->'):] BiBTeX = BiBTeX[BiBTeX.find('@'):] BiBTeX = BiBTeX[:BiBTeX.rfind('<!-- END RESULTS -->')+1] BiBTeX = BiBTeX[:BiBTeX.rfind('}')+1] return BiBTeX def getBibitem(ref,type): if type == 'old-style eprint': query = 'eprint=' + ref elif type == 'new-style eprint': query = 'eprint=arXiv:' + ref elif type == 'texkey': query = 'texkey=' + ref elif type == 'journal': query = 'j=' + ref else: return "no records were found in SPIRES to match your search, please try again" bibitem = urllib.urlopen('http://www.slac.stanford.edu/spires/find/hep/wwwbrieflatex2?' + query + '&server=sunspi5').read() if 'No records' in bibitem: return "no records were found in SPIRES to match your search, please try again" bibitem = bibitem[bibitem.find('<!-- START RESULTS -->'):] bibitem = bibitem[bibitem.find('%'):] bibitem = bibitem[:bibitem.rfind('<!-- END RESULTS -->')+1] bibitem = bibitem[:bibitem.rfind('%')+1] return bibitem def extractauthor(BiBTeX): # remove excess white space and replace with a single space data = re.sub(r'\s+',r' ',BiBTeX) author = data[data.find('author'):] author = author[author.find('\"')+1:] author = author[:author.find('\"')] return author def extracttitle(BiBTeX): # remove excess white space and replace with a single space data = re.sub(r'\s+',r' ',BiBTeX) title = data[data.find('title'):] title = title[title.find('\"')+2:] title = title[:title.find('\"')-1] return title def extractcite(BiBTeX): cite = BiBTeX[BiBTeX.find('{')+1:BiBTeX.find(',')] return cite def downloadeprint(ref,type,downloadPath): downloadPath = os.path.expanduser(downloadPath) if type == 'old-style eprint': urllib.urlretrieve('http://arxiv.org/pdf/' + ref, downloadPath + ref.replace('/','-') + '.pdf') elif type == 'new-style eprint': urllib.urlretrieve('http://arxiv.org/pdf/' + ref, downloadPath + ref + '.pdf') def updatelibrary(cite,BiBTeX,BiBTeXlibraryFileName): BiBTeXlibraryFileName = os.path.expanduser(BiBTeXlibraryFileName) libraryfile = open(BiBTeXlibraryFileName, 'r') library = libraryfile.read() libraryfile.close() if library.count(cite) == 0: print 'adding BiBTeX entry to ' + BiBTeXlibraryFileName #find the end of the file (the second argument means count from #the end of the file libraryfile = open(BiBTeXlibraryFileName, 'a') libraryfile.write('\n' + BiBTeX + '\n') libraryfile.close() else: print 'BiBTeX entry already in library' def listCitations(fileName): fileName = os.path.expanduser(fileName) f=open(fileName, 'r') data = f.read() if r'\begin{thebibliography}' in data: data = data[:data.find(r'\begin{thebibliography}')] citations = [] while r'\cite{' in data: data = data[data.find(r'\cite{') + 6:] if '}' in data: citation = data[:data.find('}')] data = data[data.find('}')+1:] citation += ',' while ',' in citation: if citation[:citation.find(',')] not in citations: citations.append(citation[:citation.find(',')]) citation = citation[citation.find(',')+1:] return citations if __name__ == "__main__": authorOpt = False titleOpt = False bibtexOpt = False bibitemOpt = False citeOpt = False verboseOpt = False libraryOpt = False downloadOpt = False try: options, arguments = getopt.gnu_getopt(sys.argv[1:], 'hbiatcev', ['help','library=','download=']) except getopt.error: print 'error: you tried to use an unknown option or the argument for an option that requires it was missing; try \'spires.py -h\' for more information' sys.exit(0) for o,a in options: if o in ('-h','--help'): print __doc__ sys.exit(0) elif o == '--library': if a == '': print '--library expects an argument' sys.exit(0) libraryOpt = 1 BiBTeXlibraryFileName = os.path.expanduser(a) print 'library file name is ' + BiBTeXlibraryFileName elif o == '--download': downloadOpt = True if a == '': a = './' elif a[-1] != '/': a += '/' downloadPath = os.path.expanduser(a) print 'download path is ' + downloadPath elif o == '-b': bibtexOpt = True elif o == '-i': bibitemOpt = True elif o == '-a': authorOpt = True elif o == '-t': titleOpt = True elif o == '-c': citeOpt = True elif o == '-e': bibtexOpt = True authorOpt = True titleOpt = True citeOpt = True elif o == '-v': verboseOpt = True if len(arguments) != 1: print "you didn't specify a SPIRES reference; try 'spires.py -h' for more information" sys.exit(0) else: ref=arguments[0] if len(options) == 0: bibtexOpt = True authorOpt = True titleOpt = True citeOpt = True type, ref = findRefType(ref) if verboseOpt: print 'the reference ' + ref + ' is a(n) ' + type if bibtexOpt or authorOpt or titleOpt or citeOpt or libraryOpt: BiBTeX = getBiBTeX(ref,type) if 'no records' in BiBTeX: print BiBTeX sys.exit(0) if bibtexOpt: print BiBTeX if bibitemOpt: bibitem = getBibitem(ref,type) print bibitem if 'no records' in bibitem: sys.exit(0) if authorOpt: author = extractauthor(BiBTeX) print author if titleOpt: title = extracttitle(BiBTeX) print title if citeOpt or libraryOpt: cite = extractcite(BiBTeX) if citeOpt: print '\cite{' + cite + '}' if libraryOpt: updatelibrary(cite,BiBTeX,BiBTeXlibraryFileName) if downloadOpt: downloadeprint(ref,type,downloadPath)