"""Statistiques sur les publications (BibTeX) du LIPN E. Viennet, Sept. 2006 Utilise pybliographer """ from Pyblio import Open from optparse import OptionParser import sys # lance par lipnbibstat: fix argv sys.argv = sys.argv[1:] # VERSION='0.1' SEP = ',' # CSV field separator usage = "usage: %prog [--csv --notitle --biblist biborderfilename --minyear minyear --Maxyear maxyear --nofuture] file.bib" parser = OptionParser( usage, version="%prog "+VERSION ) parser.add_option("-c", "--csv", action="store_const", const='csv', dest="format", default = '', help="output in CSV format (spreadsheet)") parser.add_option("-t", "--notitle", action="store_const", const='notitle', dest="notitle", default = '', help="set if only print without titles in first column") parser.add_option("-b", "--biblist", action="store", type='string', dest="biborderfilename", default = '', help="file including the list in order of bib keys to be printed") parser.add_option("-m", "--minyear", action="store", type='int', dest="minyear", default = 0, help="minimum year of bib entries to be summed") parser.add_option("-M", "--Maxyear", action="store", type='int', dest="Maxyear", default = 10000, help="maximum year of bib entries to be summed") parser.add_option("-f", "--nofuture", action="store_const", const='nofuture', dest="nofuture", default = '', help="set if only bib entries yet published to be summed") (options, args) = parser.parse_args() if len(args) != 1: parser.error("incorrect number of arguments") filename = args[0] # lecture du fichier bibtex sys.stderr.write('Processing %s\n' % filename) B = Open.bibopen(filename) sys.stderr.write( '%s: %d entries' % (filename, len(B))) def search_year( entry, B ): """Donne l'annee de publication, en suivant si necessaire la crossref.""" if entry.has_key('date'): return str(entry['date'].year) elif entry.has_key('crossref') and len(entry['crossref'].list) > 0: return search_year(B[entry['crossref'].list[0]], B) else: return '?' # Traite toutes les dates (en suivant les crossrefs) for key in B.keys(): entry = B[key] entry.lipn_year = search_year( entry, B ) if not entry.lipn_year: sys.stderr.write('Warning: no "year" in %s\n' % entry.key.key) def checkyear(year): # renvoie true si les contraintes minyear, Maxyear et nofuture sont satisfaites try: if int(year) < options.minyear: return 'false' if int(year) > options.Maxyear: return 'false' except: 0 if options.nofuture == 'nofuture': if year == '?': return 'false' return 'true' # Construit dictionnaire # lipn-category : liste d'entrees BibByCat = {} for key in B.keys(): entry = B[key] try: lipncat = str(entry['lipn-category']) except: sys.stderr.write('Warning: no lipn-category in %s\n' % entry.key.key) continue if BibByCat.has_key(lipncat): BibByCat[lipncat].append(entry) else: BibByCat[lipncat] = [entry] # Nombre par annee categorie NbByCat = {} # lipn-cat : { year : nb } for lipncat in BibByCat.keys(): NbByCat[lipncat] = {} for entry in BibByCat[lipncat]: year = entry.lipn_year if checkyear(year) == 'true': if NbByCat[lipncat].has_key(year): NbByCat[lipncat][year] += 1 else: NbByCat[lipncat][year] = 1 # Total par an years = {} # year : nb_publis for lipncat in BibByCat.keys(): for entry in BibByCat[lipncat]: year = entry.lipn_year if checkyear(year) == 'true': if not years.has_key(entry.lipn_year): years[entry.lipn_year] = 1 else: years[entry.lipn_year] += 1 # cosmetic: ajoute toutes les annees (pour aligner les colonnes) for lipncat in NbByCat.keys(): for year in years.keys(): if not NbByCat[lipncat].has_key(year): NbByCat[lipncat][year] = 0 # Affichage: ligne de titre if options.format == 'csv': # titres colonnes y = years.keys() y.sort() if options.notitle == 'notitle': print SEP.join(y) else: print SEP.join(['categorie'] + y) # lecture du fichier biborderfile biborder = [] # type et ordre d'impression des resultats if options.biborderfilename: sys.stderr.write('Processing %s Bib order file\n' % options.biborderfilename) biborderfile = open(options.biborderfilename) try: for key in biborderfile: biborder.append(key.rstrip(' \n')) finally: biborderfile.close() else: biborder = BibByCat.keys() sys.stderr.write('keys used: %s\n' % ', '.join([ '%s' % key for key in biborder]) ) # Affichage: data for lipncat in biborder: if lipncat in BibByCat.keys(): l = NbByCat[lipncat].items() l.sort() if options.format != 'csv': if options.notitle != 'notitle': print lipncat, '\t', ', '.join([ '%s : %d' % x for x in l ]) else: print ', '.join([ '%s : %d' % x for x in l ]) else: if options.notitle != 'notitle': print SEP.join( [lipncat] + [str(x[1]) for x in l]) else: print SEP.join( [str(x[1]) for x in l]) # Affichage: Total if options.notitle != 'notitle': l = years.items() l.sort() if options.format != 'csv': print 'TOTAL', '\t', ', '.join([ '%s : %d' % x for x in l ]) else: print SEP.join( ['TOTAL'] + [str(x[1]) for x in l])