bibstuff package

Submodules

bibstuff.bibadd module

bibstuff.bibadd: Add entries to .bib file

WARNING:works but currently very crude and rough! (no special characters; macros alpha-lower only)
copyright:2006 by Alan Isaac, see AUTHORS
license:MIT, see LICENSE
TODO:add checking for unique key
TODO:allow multiple entries
TODO:allow correcting entries
TODO:use style file for HTML formatting
TODO:check for macros against @string defs in .bib file
TODO:macro handling (journals)
TODO:add crossreferencing as option for inbook and incollection
TODO:change default format to legal HTML names
bibstuff.bibadd.get_journal(entry, jrnl_lst=None)[source]

Return string representation of journal, allowing opportunity to provide name to replace macro. TODO: automate macro substitution.

bibstuff.bibadd.get_pages(entry, dash='--', pagespref=('p. ', 'pp. '))[source]
bibstuff.bibadd.get_volnum(entry)[source]
bibstuff.bibadd.html_format(entry)[source]
bibstuff.bibadd.is_macro(s)[source]

Return bool, a crude guess if this is a macro. TODO: rethink this.

bibstuff.bibadd.make_entry(choosetype='', options=False, extras=False, raw_input=<built-in function raw_input>)[source]

Create a bibtex entry by prompting the user for input

Todo:Should this be moved to a script?
bibstuff.bibadd.text_format(entry)[source]
bibstuff.bibadd.valid_fields = {}

author or editor (Æ), author(A), address(a), booktitle(b), chapter(c), crossref(x), edition(e), editor(E), howpublished(h), institution(I), isbn(i), journal(j), key(k), month(m), note(z), number(n), organization(O), pages(p), publisher(P), school(S), series(s), title(T), type(t), url(u), volume(v), year(y), unused(fgl)

Currently will not provide the crossref field since crossrefs must be prepended. (Just warn?)

bibstuff.bibfile module

bibstuff.bibfile: High level BibTeX file interface

Provides two classes, BibFile and BibEntry for accessing the parts of a bibtex database. BibFile inherits from simpleparse.dispatchprocessor. To fill a BibFile instance, bfi, call bibgrammar.Parse(src, bfi).

copyright:Dylan Schwilk (esp. BibFile) and Alan G Isaac (esp. BibEntry), see AUTHORS
license:MIT (see LICENSE)
requires:Python 2.4+
TODO:make this framework more general, perhaps along the lines of the btparse library in btOOL
class bibstuff.bibfile.BibEntry(*args, **kwargs)[source]

Bases: dict

Stores a single bibliographic entry. Provides a dictionary interface to the fields: field keys are case-insensitive and fields are stored in the order added.

Note:2006-08-10 use ‘citekey’ instead of ‘key’ since BibTeX allows a ‘key’ field
Note:2008-03-29 ‘entry_type’ instead of ‘type’ since BibTeX allows a ‘type’ field
citekey

property: ‘citekey’

citekey_label_style1 = {'misc': '%(names)s-%(year)s', 'default_type': '%(names)s-%(year)s', 'max_names': 2, 'book': '%(names)s-%(year)s', 'lower_name': False, 'anonymous': 'anon', 'name_template': 'v{_}_|l{}', 'etal': 'etal', 'name_name_sep': '+', 'article': '%(names)s-%(year)s'}
entry_type

property: ‘entry_type’

fields

property: ‘fields’

format_names(names_formatter)[source]

return formatted BibName-object if possible else raw name

Note:called by CitationManager in format_citation
Note:2006-08-08 no longer sets a _names attribute
TODO:add default name_template useful for .bib files?
format_with(entry_formatter)[source]
get_citekey()[source]
get_entry_type()[source]
get_fields()[source]
get_names(entry_formatter=None, try_fields=None)[source]

return (BibName-object if possible else string)

Note:2006-08-09 matching change to make_names, no longer sets self._names
make_citekey(used_citekeys=, []style={'misc': '%(names)s-%(year)s', 'default_type': '%(names)s-%(year)s', 'max_names': 2, 'book': '%(names)s-%(year)s', 'lower_name': False, 'anonymous': 'anon', 'name_template': 'v{_}_|l{}', 'etal': 'etal', 'name_name_sep': '+', 'article': '%(names)s-%(year)s'})[source]

Create and return a new citekey based on the entry’s data. This is for creating predictable and useful citekey (labels) for BibEntry objects. This is not integrated with the citation styles in bibstuff.bibstyles; but it serves a very different purpose. This is to create consistent citation keys that are easy to type and guess and that are valid BibTeX citation keys.

Parameters:
  • used_citekeys : list

    a list of the already taken citation keys so that the function can avoid duplicates (by adding a,b,c,d... etc)

  • style : str

    The format of the citetekey is determined by a label_style (see below)

Returns:

string the citation key (label)

Example:

The label style is a dict with the following fields:

citekey_label_style1 = dict(
name_template = 'v{_}_|l{}', # see NameFormatter class
max_names = 2,
name_name_sep = "+",
etal = 'etal',
anonymous = 'anon',
lower_name = False,
article = "%(names)s-%(year)s",
book = "%(names)s-%(year)s",
misc = "%(names)s-%(year)s",
default_type = "%(names)s-%(year)s")
TODO:Strip LaTeX accent characters from names when making label
make_names(entry_formatter=None, try_fields=None)[source]

return (BibName-object if possible else string) (from “raw” names).

Change:

2006-08-02 altered to return BibName instance and not set _names

Note:

self returns None if field missing (-> no KeyError)

Note:

this method introduces the only dependence on simpleparse (via bibname)

TODO:

return BibName instance for each available name field??

Parameters:
  • entry_formatter: EntryFormatter instance to provide style information
  • try_fields: list of field names to try sequentially; none empty filed -> name
search_fields(string_or_compiled, field='', ignore_case=True)[source]

Find regular expression in entry.

Return MatchObject if string_or_compiled found in entry else None. If field is omitted, search is through all fields.

Note:

used by BibFile’s find_re method, which is used in turn by bibsearch.py

Parameters:
string_or_compiled : string to compile or compiled regex

pattern for searching

field : string

field to search in self (default: search all fields)

set_citekey(val)[source]
set_entry_type(val)[source]
set_fields(lst)[source]
class bibstuff.bibfile.BibFile[source]

Bases: simpleparse.dispatchprocessor.DispatchProcessor

Stores parsed bibtex file. Access entries by key.

Note:a BibFile object should simply store .bib file parts (a list of entries and a macro map) and provide access to these parts
citekey((tag, start, stop, subtags), buffer)[source]

Return the entry’s citekey

comment_entry((tag, start, stop, subtags), buffer)[source]

Process the given production and it’s children

entry((tag, start, stop, subtags), buffer)[source]

Process the bibentry and its children.

entry_type((tag, start, stop, subtags), buffer)[source]

Return the entry type

field((tag, start, stop, subtags), buffer)[source]

Process a bibentry field and return tuple of name, value.

get_entry_by_citekey(citekey)[source]

Return entry or None.

get_entrylist(citekeys, discard=True)[source]

Return list, the BibEntry instances that were found (and None for entries not found, unless discarded).

macro((tag, start, stop, subtags), buffer)[source]

Process a macro entry and add macros to macro map

name((tag, start, stop, subtags), buffer)[source]

Return lookup on name or name if not in map.

number((tag, start, stop, subtags), buffer)[source]

return a number as a string

preamble((tag, start, stop, subtags), buffer)[source]

Process the given production and it’s children

search_entries(string_or_compiled, field='', ignore_case=True)[source]

Return list of matching entries. Search for regular expression in the fields of each entry. If field is omitted, search is through all fields.

Note:

used by bibsearch.py

Parameters:
  • string_or_compiled : string to compile or compiled regex pattern for searching

  • field : string

    field to search in self (default: search all fields)

string((tag, start, stop, subtags), buffer)[source]

Return a string, stripping leading and trailing markers

bibstuff.bibgrammar module

bibstuff.bibgrammar: BibTeX Parser

Provides an EBNF description of the bibtex bibliography format. The grammar draws largely from the grammar description in Nelson Beebe’s Lex/Yacc parser and also from Greg Ward’s btOOL documentation.

copyright:Dylan Schwilk and Alan G. Isaac, see AUTHORS
license:MIT (see LICENSE)
bibstuff.bibgrammar.Parse(src, processor=None)[source]

Parse the bibtex string src, process with processor.

bibstuff.bibname module

bibstuff.bibname: Name Parser and Formatter

Parses bibtex-formatted author/editor raw names and provides formatting functions (e.g., via bibstyles/shared.NamesFormatter).

copyright:2009-2014 Dylan Schwilk and Alan G Isaac, see AUTHORS
license:MIT (see LICENSE)
note:Major change as of 2008-07-02. Now the ebnf grammar and processor handles parsing of a list of names (a bibtex names field such as editor or author) and parses the single author name into its fvlj parts. This eliminates the need for the original hand-coded parse_raw_names_parts function. Moved to using names_dicts rather than names_parts. The grammar handles latex accents and ligatures as well as braces strings so that a name such as {Barnes and Noble, Inc} is parsed as a single name and not split on the ” and ”.
todo:The dispatch processor does not currently strip the leading and trailing braces from latex/bibtex strings. Not hard to add (see bibfile.py). This should be done eventually.
todo:The grammar does not support quoted strings, only braces strings. Could be added fairly simply
class bibstuff.bibname.BibName(raw_names=None, from_field=None)[source]

Bases: simpleparse.dispatchprocessor.DispatchProcessor

Processes a bibtex names entry (author, editor, etc) and stores the resulting raw_names_parts.

Note:a BibName object should be bibstyle independent.
first((tag, start, stop, subtags), buffer)[source]

Processes first name part in a single name of a bibtex names field

format(names_formatter)[source]

format a BibName object into a string useful for citations

Note:called by the BibEntry class in bibfile.py when entry formatting is requested
get_last_names()[source]

Return list of strings, where each string is a last name.

TODO:graceful handling of missing names parts
get_names_dicts()[source]

Return a list of name dicts, one dict per name, having the fields: first , von, last, jr

jr((tag, start, stop, subtags), buffer)[source]

Processes jr name part in a single name of a bibtex names field

last((tag, start, stop, subtags), buffer)[source]

Processes last name part in a single name of a bibtex names field

name((tag, start, stop, subtags), buffer)[source]

Prduction function to process a single name in a nameslist

parse_raw_names(raw_name)[source]

This function can be used to populate an empty BibName instance or replace all the name values currently contained in an instance. It parses the names field with the bibname grammar

von((tag, start, stop, subtags), buffer)[source]

Processes von name part in a single name of a bibtex names field

bibstuff.bibname.getNames(src)[source]

Returns list of name dicts. Each dict has keys “first”, “last”, “von”, “jr”. src is a string is in bibtex name format.

bibstuff.ebnf_sp module

bibstuff.ebnf_sp: simpleparse EBNF declarations

Contains some simpleparse style ebnf declarations for use in parsing with simpleparse.

copyright:2006 by Alan G Isaac, see AUTHORS
license:MIT (see LICENSE)

bibstuff.isbn2bib module

bibstuff.isbn2bib — Obtain bib entries via ISBN

requires:pyaws v.0.3+ (installation is easy; see below)
requires:free Amazon web services key http://www.amazon.com/gp/browse.html?node=3435361
license:MIT, see LICENSE

Installing pyAWS

You can get a tarball from:http://svn2.assembla.com If you use SVN, I’ll assume you want the latest version. (Otherwise, get the tagged version rather than the trunk.)

  • Decide where you want your pyaws build directory, say in mysvn/pyaws.
  • In a command shell, change to the mysvn directory.
  • Issue the command: svn checkout http://svn2.assembla.com/svn/pyaws/trunk/ pyaws
  • Change to your new pyaws directory.
  • Use your python to execute: setup.py install

Your Amazon Web Services key

  • it is free from Amazon web services http://www.amazon.com/gp/browse.html?node=3435361

  • right now I only look for it in bibstuff.cfg, which must be in the directory from which you call your script, and which must contain the lines:

    [isbn2bib]
    aws_key : your_AWS_key_here 
    
bibstuff.isbn2bib.import_pyaws_ecs()[source]
bibstuff.isbn2bib.main()[source]

Command-line tool. See bibsearch.py -h for help.

bibstuff.isbn2bib.make_bookdict(bkinfo, publisher_addresses=None)[source]
bibstuff.isbn2bib.make_entry(isbn)[source]

Return a bibfile.BibEntry instance. Calls make_bookdict; called by main.

Date:2008-08-31
Todo:this is reusing too much add2bib code
bibstuff.isbn2bib.read_publisher_dict()[source]
bibstuff.isbn2bib.set_license_key()[source]

Module contents

Bibstuff: classes and command-line utilities for interacting with BibTeX style databases

Includes:

  • a bibtex parser (which uses SimpleParse)
  • classes for parsing and manipulating BibTeX file parts
  • utility and classes for managing citations in text files
  • utilities for searching and labeling BibTeX files

For more detail, see README

Bibstuff is free software distributed under the MIT license (see LICENSE)

Bibstuff is pure Python and in that sense will run on any platform.