Esempio 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)
Esempio n. 2
0
def _ensure_genres(lang='en'):
    global _genres

    if _genres is None:
        genres_file = os.path.join(os.path.dirname(__file__), 'fb2genres.xml')

        result = FB2Genres()

        genres = parse_xml(open(genres_file, 'r').read())

        # dump(genres)

        for genre in genres:
            fb2_genre = genre.attrib['value']

            name = None

            for child in genre:
                if child.tag == 'root-descr':
                    if child.attrib['lang'] == 'en':
                        name = child.attrib['genre-title']
                elif child.tag == 'subgenres':
                    for subgenre in child:
                        fb2_subgenre = subgenre.attrib['value']
                        sub_name = None

                        for subelem in subgenre:
                            if subelem.tag == 'genre-descr':
                                if subelem.attrib['lang'] == 'en':
                                    sub_name = subelem.attrib['title']
                            elif subelem.tag == 'genre-alt':
                                result.add_alias(fb2_subgenre, subelem.attrib['value'])

                        if sub_name is None:
                            LOG.error('could not find name for %s', fb2_subgenre)
                        else:
                            result.add_genre(fb2_subgenre, fb2_genre, sub_name)

            if name is None:
                LOG.error('could not find name for %s', fb2_genre)
            else:
                result.add_genre(fb2_genre, None, name)

        result.validate()

        _genres = result
Esempio n. 3
0
def get_root(data, path):
    """parses supplied data and returns its root element"""

    root = parse_xml(data, True, path)

    if root is None:
        LOG.error('Invalid data in %s', path)
        return

    if root.tag != ROOT_ELEM:
        LOG.error('%s has wrong root element: %s (expected %s)',
                  path, root.tag, ROOT_ELEM)
        return

    if 0:
        dump(root)

    return root