pfif module

PFIF 1.1 - 1.4 parsing and serialization (see http://zesty.ca/pfif/).

This module converts between PFIF XML documents (PFIF 1.1, 1.2, 1.3, or 1.4) and plain Python dictionaries that have PFIF 1.4 field names as keys (always 1.4) and Unicode strings as values. Some useful constants are also defined here according to the PFIF specification. Use parse() to parse PFIF 1.1, 1.2, 1.3, or 1.4; use PFIF_1_1, PFIF_1_2, PFIF_1_3, or PFIF_1_4 to serialize to the desired version.

class pfif.Handler(rename_fields=True)[source]

Bases: xml.sax.handler.ContentHandler

SAX event handler for parsing PFIF documents.

append_to_field(record, tag, parent, content)[source]
characters(content)[source]
endElementNS(tag, qname)[source]
startElementNS(tag, qname, attrs)[source]
class pfif.PfifVersion(version, ns, fields, mandatory_fields, serializers)[source]
check_tag((ns, local), parent=None)[source]

Given a namespace-qualified tag and its parent, returns the PFIF type or field name if the tag is valid, or None if the tag is not recognized.

entity_to_dict(entity, fields)[source]

Converts a person or note record from a Python object (with PFIF 1.4 field names as attributes) to a Python dictionary (with the given field names as keys, and Unicode strings as values).

note_to_dict(entity)[source]
person_to_dict(entity, expired=False)[source]
write_fields(file, type, record, indent='')[source]

Writes PFIF tags for a record’s fields.

write_file(file, persons, get_notes_for_person=<function <lambda>>)[source]

Takes a list of person records and a function that gets the list of note records for each person, and writes PFIF to the given file object. Each record is a plain dictionary of strings.

write_note(file, note, indent='')[source]

Writes PFIF for a note record.

write_person(file, person, notes=[], indent='')[source]

Writes PFIF for a person record and a list of its note records.

pfif.check_pfif_tag(name, parent=None)[source]

Recognizes a PFIF XML tag from any version of PFIF.

pfif.convert_description_to_other(desc)[source]

Converts ‘description’ in PFIF 1.4 to ‘other’ in older versions.

pfif.format_boolean(value)[source]
pfif.format_utc_datetime(dt)[source]
pfif.maybe_convert_other_to_description(other)[source]

Converts ‘other’ in PFIF 1.3 and earlier to ‘description’ in PFIF 1.4 if ‘other’ has only ‘description’ field. Otherwise it returns ‘other’ without modifying it, so we don’t lose any information.

pfif.nop(value)[source]
pfif.parse(pfif_text, rename_fields=True)[source]

Takes the text of a PFIF document, as a Unicode string or UTF-8 string, and returns a list of person records and a list of note records. Each record is a plain dictionary of strings, with PFIF 1.4 field names as keys if rename_fields is True; otherwise, the field names are kept as is in the input XML file.

pfif.parse_file(pfif_utf8_file, rename_fields=True)[source]

Reads a UTF-8-encoded PFIF file to give a list of person records and a list of note records. Each record is a plain dictionary of strings, with PFIF 1.4 field names as keys if rename_fields is True; otherwise, the field names are kept as is in the input XML file.

pfif.rename_fields_to_latest(record)[source]

Renames fields in PFIF 1.3 and earlier to PFIF 1.4, and also does a special conversion for other -> description.

pfif.xml_escape(s)[source]