Exemplo n.º 1
0
def read(path):
    LOG.debug("reading %s", path)

    archive = get_good_zip(path)

    try:
        container = archive.read(CONTAINER)
    except KeyError:
        LOG.error('%s has no container', path)
        return

    container = parse_xml(container)

    if container is None:
        LOG.error('%s has invalid container', path)
        return

    opf_name = container.find('.//{%s}rootfile' % CONTAINER_NS).get('full-path')

    try:
        opf = archive.read(opf_name)
    except KeyError:
        LOG.error('Could not open opf file (%s)', opf_name.encode('utf-8'))
        return

    opf = parse_xml(opf)

    if 0:
        dump(opf)

    if opf is None:
        return

    metadata = None

    for child in opf:
        if child.tag == 'metadata' or child.tag.endswith('}metadata'):
            metadata = child
            break

    if metadata is None:
        LOG.error('Could not find metadata in the opf file')
        return

    if metadata.tag == 'metadata':
        return read_oeb_metadata(metadata)
    else:
        return read_opf_metadata(metadata)
Exemplo n.º 2
0
def read_fb2_file(path):
    """reads fb2/fb2.zip file and returns xml internal representation"""

    if path.endswith('.fb2.zip'):
        archive = get_good_zip(path)

        if archive is None:
            LOG.error('%s is not a valid zip file', path)
            return

        files = archive.namelist()

        if len(files) != 1:     # the .zip file contains several books, fail!
            return

        data = archive.read(files[0])
    else:
        data = open(path, 'r').read()

    return data