コード例 #1
0
def main(argv=None):
    args = docopt(__doc__,
                  argv=argv,
                  version='OpenAccess_EPUB v.' + __version__,
                  options_first=True)

    c_file = args['COLLECTION_FILE']
    c_file_root = utils.file_root_name(c_file)
    abs_input_path = utils.get_absolute_path(c_file)

    if not args['--log-to']:
        log_to = os.path.join(os.path.dirname(abs_input_path),
                              c_file_root + '.log')
    else:
        log_to = args['--log-to']

    #Basic logging configuration
    oae_logging.config_logging(args['--no-log-file'], log_to,
                               args['--log-level'], args['--silent'],
                               args['--verbosity'])

    command_log = logging.getLogger('openaccess_epub.commands.collection')

    #Load the config module, we do this after logging configuration
    config = openaccess_epub.utils.load_config_module()

    #Quit if the collection file is not there
    if not os.path.isfile(c_file):
        command_log.critical('File does not exist {0}'.format(c_file))
        sys.exit('Unable to continue')

    command_log.info('Parsing collection file: {0}'.format(c_file))
    with open(c_file, 'r') as f:
        inputs = [line.strip() for line in f.readlines()]

    #Get the output directory
    if args['--output'] is not None:
        output_directory = utils.get_absolute_path(args['--output'])
    else:
        if os.path.isabs(config.default_output):  # Absolute remains so
            output_directory = config.default_output
        else:  # Else rendered relative to input
            abs_dirname = os.path.dirname(abs_input_path)
            output_directory = os.path.normpath(
                os.path.join(abs_dirname, config.default_output))

    output_directory = os.path.join(output_directory, c_file_root)
    command_log.info(
        'Processing collection output in {0}'.format(output_directory))

    if os.path.isdir(output_directory):
        utils.dir_exists(output_directory)
    try:
        os.makedirs(output_directory)
    except OSError as err:
        if err.errno != 17:
            command_log.exception(
                'Unable to recursively create output directories')

    #Instantiate collection NCX and OPF
    navigation = Navigation(collection=True)
    package = Package(collection=True, title=c_file_root)

    #Copy over the basic epub directory
    make_epub_base(output_directory)

    epub_version = None

    #Iterate over the inputs
    for xml_file in inputs:
        xml_path = utils.evaluate_relative_path(
            os.path.dirname(abs_input_path), xml_file)
        parsed_article = Article(xml_path,
                                 validation=not args['--no-validate'])
        if epub_version is None:  # Only set this once, no mixing!
            if args['--epub2']:
                epub_version = 2
            elif args['--epub3']:
                epub_version = 3
            else:
                epub_version = parsed_article.publisher.epub_default

        navigation.process(parsed_article)
        package.process(parsed_article)

        #Get the Digital Object Identifier
        doi = parsed_article.get_DOI()
        journal_doi, article_doi = doi.split('/')

        #Get the images
        openaccess_epub.utils.images.get_images(output_directory,
                                                args['--images'], xml_path,
                                                config, parsed_article)

        parsed_article.publisher.render_content(output_directory, epub_version)

    if epub_version == 2:
        navigation.render_EPUB2(output_directory)
        package.render_EPUB2(output_directory)
    elif epub_version == 3:
        navigation.render_EPUB3(output_directory)
        package.render_EPUB3(output_directory)
    epub_zip(output_directory)

    #Cleanup removes the produced output directory, keeps the EPUB
    if not args['--no-cleanup']:
        command_log.info('Removing {0}'.format(output_directory))
        shutil.rmtree(output_directory)

    #Running epubcheck on the output verifies the validity of the ePub,
    #requires a local installation of java and epubcheck.
    if not args['--no-epubcheck']:
        epub_name = '{0}.epub'.format(output_directory)
        openaccess_epub.utils.epubcheck(epub_name, config)
コード例 #2
0
ファイル: epub.py プロジェクト: Python3pkg/OpenAccess_EPUB
def make_EPUB(parsed_article,
              output_directory,
              input_path,
              image_directory,
              config_module=None,
              epub_version=None,
              batch=False):
    """
    Standard workflow for creating an EPUB document.

    make_EPUB is used to produce an EPUB file from a parsed article. In addition
    to the article it also requires a path to the appropriate image directory
    which it will insert into the EPUB file, as well the output directory
    location for the EPUB file.

    Parameters
    ----------
    article : openaccess_epub.article.Article instance
        `article` is an Article instance for the XML document to be converted to
        EPUB.
    output_directory : str
        `output_directory` is a string path to the directory in which the EPUB
        will be produced. The name of the directory will be used as the EPUB's
        filename.
    input_path : str
        `input_path` is a string absolute path to the input XML file, used to
        locate input-relative images.
    image_directory : str
        `image_directory` is a string path indicating an explicit image
        directory. If supplied, other image input methods will not be used.
    config_module : config module, optional
        `config_module` is a pre-loaded config module for OpenAccess_EPUB; if
        not used then this function will load the global config file. Might be
        useful in certain cases to dynamically alter configuration.
    epub_version : {None, 2, 3}
        `epub_version` dictates which version of EPUB to be created. An error
        will be raised if the specified version is not supported for the
        publisher. If left to the default, the created version will defer to the
        publisher default version.
    batch : bool, optional
        `batch` indicates that batch creation is being used (such as with the
        `oaepub batch` command). In this case, directory conflicts will be
        automatically resolved (in favor of keeping previous data, skipping
        creation of EPUB).

    Returns False in the case of a fatal error, True if successful.
    """
    #command_log.info('Creating {0}.epub'.format(output_directory))
    if config_module is None:
        config_module = openaccess_epub.utils.load_config_module()

    if epub_version not in (None, 2, 3):
        log.error('Invalid EPUB version: {0}'.format(epub_version))
        raise ValueError('Invalid EPUB version. Should be 2 or 3')

    if epub_version is None:
        epub_version = parsed_article.publisher.epub_default

    #Handle directory output conflicts
    if os.path.isdir(output_directory):
        if batch:  # No user prompt, default to protect previous data
            log.error('Directory conflict during batch conversion, skipping.')
            return False
        else:  # User prompting
            openaccess_epub.utils.dir_exists(output_directory)
    else:
        try:
            os.makedirs(output_directory)
        except OSError as err:
            if err.errno != 17:
                log.exception('Unable to recursively create output directories')

    #Copy over the basic epub directory
    make_epub_base(output_directory)

    #Get the images, if possible, fail gracefully if not
    success = openaccess_epub.utils.images.get_images(output_directory,
                                                      image_directory,
                                                      input_path,
                                                      config_module,
                                                      parsed_article)
    if not success:
        log.critical('Images for the article were not located! Aborting!')
        return False

    #Instantiate Navigation and Package
    epub_nav = Navigation()
    epub_package = Package()

    #Process the article for navigation and package info
    epub_nav.process(parsed_article)
    epub_package.process(parsed_article)

    #Render the content using publisher-specific methods
    parsed_article.publisher.render_content(output_directory, epub_version)
    if epub_version == 2:
        epub_nav.render_EPUB2(output_directory)
        epub_package.render_EPUB2(output_directory)
    elif epub_version == 3:
        epub_nav.render_EPUB3(output_directory)
        epub_package.render_EPUB3(output_directory)

    #Zip the directory into EPUB
    epub_zip(output_directory)

    return True
コード例 #3
0
def main(argv=None):
    args = docopt(__doc__,
                  argv=argv,
                  version='OpenAccess_EPUB v.' + __version__,
                  options_first=True)

    c_file = args['COLLECTION_FILE']
    c_file_root = utils.file_root_name(c_file)
    abs_input_path = utils.get_absolute_path(c_file)

    if not args['--log-to']:
        log_to = os.path.join(os.path.dirname(abs_input_path),
                              c_file_root + '.log')
    else:
        log_to = args['--log-to']

    #Basic logging configuration
    oae_logging.config_logging(args['--no-log-file'],
                               log_to,
                               args['--log-level'],
                               args['--silent'],
                               args['--verbosity'])

    command_log = logging.getLogger('openaccess_epub.commands.collection')

    #Load the config module, we do this after logging configuration
    config = openaccess_epub.utils.load_config_module()

    #Quit if the collection file is not there
    if not os.path.isfile(c_file):
        command_log.critical('File does not exist {0}'.format(c_file))
        sys.exit('Unable to continue')

    command_log.info('Parsing collection file: {0}'.format(c_file))
    with open(c_file, 'r') as f:
        inputs = [line.strip() for line in f.readlines()]

    #Get the output directory
    if args['--output'] is not None:
        output_directory = utils.get_absolute_path(args['--output'])
    else:
        if os.path.isabs(config.default_output):  # Absolute remains so
            output_directory = config.default_output
        else:  # Else rendered relative to input
            abs_dirname = os.path.dirname(abs_input_path)
            output_directory = os.path.normpath(os.path.join(abs_dirname, config.default_output))

    output_directory = os.path.join(output_directory, c_file_root)
    command_log.info('Processing collection output in {0}'.format(output_directory))

    if os.path.isdir(output_directory):
        utils.dir_exists(output_directory)
    try:
        os.makedirs(output_directory)
    except OSError as err:
        if err.errno != 17:
            command_log.exception('Unable to recursively create output directories')

    #Instantiate collection NCX and OPF
    navigation = Navigation(collection=True)
    package = Package(collection=True, title=c_file_root)

    #Copy over the basic epub directory
    make_epub_base(output_directory)

    epub_version = None

    #Iterate over the inputs
    for xml_file in inputs:
        xml_path = utils.evaluate_relative_path(os.path.dirname(abs_input_path),
                                                xml_file)
        parsed_article = Article(xml_path, validation=not args['--no-validate'])
        if epub_version is None:  # Only set this once, no mixing!
            if args['--epub2']:
                epub_version = 2
            elif args['--epub3']:
                epub_version = 3
            else:
                epub_version = parsed_article.publisher.epub_default

        navigation.process(parsed_article)
        package.process(parsed_article)

        #Get the Digital Object Identifier
        doi = parsed_article.get_DOI()
        journal_doi, article_doi = doi.split('/')

        #Get the images
        openaccess_epub.utils.images.get_images(output_directory,
                                                args['--images'],
                                                xml_path,
                                                config,
                                                parsed_article)

        parsed_article.publisher.render_content(output_directory, epub_version)

    if epub_version == 2:
        navigation.render_EPUB2(output_directory)
        package.render_EPUB2(output_directory)
    elif epub_version == 3:
        navigation.render_EPUB3(output_directory)
        package.render_EPUB3(output_directory)
    epub_zip(output_directory)

    #Cleanup removes the produced output directory, keeps the EPUB
    if not args['--no-cleanup']:
        command_log.info('Removing {0}'.format(output_directory))
        shutil.rmtree(output_directory)

    #Running epubcheck on the output verifies the validity of the ePub,
    #requires a local installation of java and epubcheck.
    if not args['--no-epubcheck']:
        epub_name = '{0}.epub'.format(output_directory)
        openaccess_epub.utils.epubcheck(epub_name, config)