def run_test8(): ''' Test #8 tests Bibulous' ability to generate glossaries, symbol lists, and acronym lists. ''' bblfile = './test/test8.bbl' auxfile = './test/test8.aux' target_bblfile = './test/test8_target.bbl' print('\n' + '=' * 75) print('Running Bibulous Test #8') bibobj = Bibdata(auxfile, debug=False) bibobj.write_bblfile() return (bblfile, target_bblfile)
def run_test5(): ''' Test #5 flexes the Python API. ''' auxfile = './test/test5.aux' bblfile = './test/test5.bbl' targetfile = './test/test5_target.bbl' print('\n' + '=' * 75) print('Running Bibulous Test #5') bibobj = Bibdata(auxfile, disable=[9], debug=False) bibobj.write_bblfile(write_preamble=True, write_postamble=True) return (bblfile, targetfile)
def run_test10(): ''' Test #10 checks Bibulous' ability to sort numbers correctly (i.e. "human" sorting, so that "10" goes after, not before, "2"). ''' bblfile = './test/test10_humansort.bbl' auxfile = './test/test10_humansort.aux' target_bblfile = './test/test10_humansort_target.bbl' print('\n' + '=' * 75) print('Running Bibulous Test #10') bibobj = Bibdata(auxfile, debug=False) bibobj.write_bblfile() return (bblfile, target_bblfile)
def run_test3(): ''' Test #3 tests that the "authorextract" method functions correctly. ''' auxfile = './test/test2.aux' ## re-use the huge database authorstr = 'John W. Tukey' outputfile = './test/test3_authorextract.bib' targetfile = './test/test3_authorextract_target.bib' print('\n' + '='*75) print('Running Bibulous Test #3 for author "' + authorstr + '"') bibobj = Bibdata(auxfile, disable=[4,9,21,32,33]) #bibobj.debug = True bibobj.write_authorextract(authorstr, outputfile) return(outputfile, targetfile)
def run_test1(): ''' Test #1 consists of a suite of single tests of options and features that are valid with the default template file. ''' bblfile = './test/test1.bbl' auxfile = './test/test1.aux' target_bblfile = './test/test1_target.bbl' print('\n' + '=' * 75) print('Running Bibulous Test #1') bibobj = Bibdata(auxfile, disable=[9, 17, 28, 29]) bibobj.write_bblfile(write_preamble=True, write_postamble=True, bibsize='ZZ') return (bblfile, target_bblfile)
def run_test6(): ''' Test #6 makes sure to raise an exception when attempting to load a BibTeX-format BST file. ''' auxfile = './test/test6.aux' bstfile = './test/test6.bst' print('\n' + '='*75) print('Running Bibulous Test #6') try: Bibdata([auxfile, bstfile], debug=False, disable=[8,9]) result = False print('TEST #6 FAILED') except ImportError: result = True print('TEST #6 PASSED.') return(result)
def run_test9(): ''' Test #9 is a platform for running conditions in which the entire database needs to be re-read with each test. ''' auxfiles = ['./test/test9_case_sensitive_fieldnames.aux', './test/test9_italian_name_separator.aux'] bblfile = './test/test9.bbl' target_bblfile = './test/test9_target.bbl' print('\n' + '='*75) print('Running Bibulous Test #9') for auxfile in auxfiles: bibobj = Bibdata(auxfile, disable=[9], silent=(auxfile != auxfiles[0])) bibobj.filedict['bbl'] = bblfile write_preamble = (auxfile == auxfiles[0]) write_postamble = (auxfile == auxfiles[-1]) bibobj.write_bblfile(write_preamble=write_preamble, write_postamble=write_postamble) print('') return(bblfile, target_bblfile)
def run_test1(): auxfile = './test/test1.aux' bibobj = Bibdata(auxfile, disable=[9,17]) bibobj.write_bblfile(write_preamble=True, write_postamble=True, bibsize='ZZ') return
def run_test7(): ''' Test #7 checks the operation of generating reference list labels. ''' ## Although three of these files were copied from "test1", it is a bad idea to use the "test1.*" files here because ## any changes to test1 would then require changes to the test7_target.bbl as well. bblfile = './test/test7.bbl' bstfile = './test/test7.bst' auxfile = './test/test7.aux' target_bblfile = './test/test7_target.bbl' ## The default locale will be US english. Ironically, the locale argument needs to use an ASCII string, and since ## the default string encoding here is Unicode, we have to re-encode it manually. Later below, we will try some ## other locale settings. if (os.name == 'posix'): thislocale = locale.setlocale(locale.LC_ALL,'en_US.UTF8'.encode('ascii','replace')) elif (os.name == 'nt'): thislocale = locale.setlocale(locale.LC_ALL,'usa_usa'.encode('ascii','replace')) ## Need to make a list of all the citation label options we want to try. Skip "citenum" since that is the default, ## and so has been tested already. citelabels = ['<citekey>', '[<authorlist.0.last>|<editorlist.0.last>|]-<year>', '<citealpha>', '[<authorlist.0.last>|<editorlist.0.last>|], <year>', '[<authorlist.0.last>|<editorlist.0.last>|] (<year>)', '(<citealnum>)'] print('\n' + '='*75) print('Running Bibulous Test #7') filehandle = open(bstfile, 'r') lines = filehandle.readlines() filehandle.close() for i in range(len(citelabels)): citelabel = citelabels[i] ## First go into the BST file and rewrite the "citelabel" line to be the current sortkey template. filehandle = open(bstfile, 'w') for line in lines: if line.startswith('citelabel = '): filehandle.write('citelabel = ' + citelabel + '\n') else: filehandle.write(line) filehandle.close() bibobj = Bibdata(auxfile, disable=[9], silent=(i>0)) bibobj.locale = thislocale bibobj.bibdata['preamble'] = '\n\n%% SETTING CITELABEL = ' + citelabel #bibobj.debug = True ## turn on debugging for citekey printing print('Setting citation_label = ' + citelabel) write_preamble = (citelabel == citelabels[0]) write_postamble = (citelabel == citelabels[-1]) if not write_preamble: filehandle = open(bblfile, 'a') filehandle.write('\n\n%% SETTING SETTING CITELABEL = ' + citelabel + '\n') filehandle.close() bibobj.write_bblfile(write_preamble=write_preamble, write_postamble=write_postamble) return(bblfile, target_bblfile)
def run_test4(): ''' Test #4 checks the operation of generating citation keys. ''' ## Although three of these files were copied from "test1", it is a bad idea to use the "test1.*" files here because ## any changes to test1 would then require changes to the test4_target.bbl as well. bblfile = './test/test4.bbl' bstfile = './test/test4.bst' auxfile = './test/test4.aux' target_bblfile = './test/test4_target.bbl' ## The default locale will be US english. Ironically, the locale argument needs to use an ASCII string, and since ## the default string encoding here is Unicode, we have to re-encode it manually. Later below, we will try some ## other locale settings. if (os.name == 'posix'): thislocale = locale.setlocale(locale.LC_ALL,'en_US.UTF8'.encode('ascii','replace')) elif (os.name == 'nt'): thislocale = locale.setlocale(locale.LC_ALL,'usa_usa'.encode('ascii','replace')) ## Need to make a list of all the citation sort options we want to try. Skip "citenum" since that is the default, ## and so has been tested already. Note: In the "uniquify" example below, the .upper() operator is needed to force the ## code to see 'b' and 'B' as being the same (and thus need a unique ending) when case-indep. sorting is being used. presortkeys = ['<citekey>', '[<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sortyear.zfill(4)>|<year.zfill(4)>|][<sorttitle>|<title>]', '[<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sorttitle>|<title>][<sortyear.zfill(4)>|<year.zfill(4)>|]', '[<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sortyear.zfill(4)>|<year.zfill(4)>|]<volume>[<sorttitle>|<title>]', '[<alphalabel>][<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sortyear.zfill(4)>|<year.zfill(4)>|][<sorttitle>|<title>]', '[<alphalabel>][<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sortyear.zfill(4)>|<year.zfill(4)>|]<volume>[<sorttitle>|<title>]', '[<sortyear.zfill(4)>|<year.zfill(4)>][<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sorttitle>|<title>]', '[<sortyear.zfill(4)>|<year.zfill(4)>][<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sorttitle>|<title>]', #'<author_or_editor.initial().upper().uniquify(num)>', '[<sortname>|<authorlist.0.last>|<editorlist.0.last>|][<authorlist.0.first>|<editorlist.0.first>][<sortyear.zfill(4)>|<year.zfill(4)>][<sorttitle>|<title>]'] sortkeys = ['<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', '<presortkey.purify().lower().compress()>', #'<presortkey.purify().lower().compress()>', '<presortkey.purify().compress()>'] ## do not use .lower() here, in order to test sorting *with* case sensitivity sort_case_options = ['False', 'False', 'False', 'False', 'False', 'False', 'False', 'False', 'True'] #sort_case_options = ['True', 'True', 'True', 'True', 'True', 'True', 'True', 'True', 'True'] sort_order_options = ['Forward', 'Forward', 'Forward', 'Forward', 'Forward', 'Forward', 'Forward', 'Reverse', 'Forward'] print('\n' + '='*75) print('Running Bibulous Test #4') filehandle = open(bstfile, 'r') lines = filehandle.readlines() filehandle.close() for i in range(len(sortkeys)): presortkey = presortkeys[i] sortkey = sortkeys[i] sort_case_option = sort_case_options[i] sort_order_option = sort_order_options[i] ## First go into the BST file and rewrite the "sortkey" line to be the current sortkey template. filehandle = open(bstfile, 'w') for line in lines: if line.startswith('presortkey = '): filehandle.write('presortkey = ' + presortkey + '\n') elif line.startswith('sortkey = '): filehandle.write('sortkey = ' + sortkey + '\n') elif line.startswith('sort_case = '): filehandle.write('sort_case = ' + sort_case_option + '\n') elif line.startswith('sort_order = '): filehandle.write('sort_order = ' + sort_order_option + '\n') else: filehandle.write(line) filehandle.close() bibobj = Bibdata(auxfile, disable=[9], silent=(i>0)) bibobj.locale = thislocale bibobj.bibdata['preamble'] = '\n\n%% SETTING PRESORTKEY = ' + presortkey bibobj.bibdata['preamble'] += '\n%% SETTING SORTKEY = ' + sortkey #bibobj.debug = True ## turn on debugging for citekey printing print('Setting PRESORTKEY = ' + presortkey) print('Setting SORTKEY = ' + sortkey) write_preamble = (presortkey == presortkeys[0]) write_postamble = (presortkey == presortkeys[-1]) if not write_preamble: filehandle = open(bblfile, 'a') filehandle.write('\n\n%% SETTING PRESORTKEY = ' + presortkey + '\n') filehandle.write('%% SETTING SORTKEY = ' + sortkey + '\n') #if (sort_case_option == 'True'): filehandle.write('%% SETTING SORT_CASE = True\n') if (sort_order_option == 'Reverse'): filehandle.write('%% SETTING SORT_ORDER = Reverse\n') filehandle.close() bibobj.write_bblfile(write_preamble=write_preamble, write_postamble=write_postamble, bibsize='ZZZ') return(bblfile, target_bblfile)
## ================================================================================================== if (__name__ == '__main__'): print(list(sys.argv)) try: auxfile = sys.argv[1] authorstr = sys.argv[2] if (len(sys.argv) > 2): outputfile = sys.argv[3] else: outputfile = auxfile[:-4] + '_authorextract.bib' except getopt.GetoptError as err: print( str(err)) ## will print something like "option -a not recognized" print('usage:') print( '>>> python bibulous-authorextract.py auxfile "author name in quotes" outfile ...' ) print( 'if "file1" has a ".aux" extension, then it is assumed that the file contains the ' 'locations of the ".bib" files.') print( 'if "file1" has a ".bib" extension, then it is assumed to be a ".bib" database ' 'filename (as are all subsequent files).') sys.exit(2) bibdata = Bibdata(auxfile, culldata=False) print('Writing BIB author extract file = ' + outputfile) bibdata.write_authorextract(authorstr, outputfile, debug=False)