Skip to content

Instantly share code, notes, and snippets.

@SavinaRoja
Last active November 22, 2015 22:37
Show Gist options
  • Save SavinaRoja/9620518 to your computer and use it in GitHub Desktop.
Save SavinaRoja/9620518 to your computer and use it in GitHub Desktop.
oaepub script from OpenAccess_EPUB version 0.5.4. Put here for posterity before applying some changes in 0.5.5.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
OpenAccess_EPUB
Usage:
oaepub [--version | --help] [options] COMMAND [ARGS ...]
Options:
-h --help show this help message and exit
-v --version show program version and exit
-V --verbose print additional information about command execution
The available commands are:
batch Convert all the contents of a directory to individual EPUB
clearcache Delete some, or all, of the contents of OpenAccess_EPUB's cache
collection Convert multiple articles into a single omnibus EPUB
configure Configure some settings for your OpenAccess_EPUB install
convert Convert explicit input(s) individually to EPUB
publishers Show which publishers are currently supported by OpenAccess_EPUB
validate Validate article XML files according to their specification
See 'oaepub COMMAND --help' for more information on a specific command.
For more help, visit the documentation:
http://openaccess-epub.readthedocs.org
"""
#Standard Library modules
import sys
import logging
#Non-Standard Library modules
from docopt import docopt
#OpenAccess_EPUB modules
from openaccess_epub._version import __version__
if __name__ == '__main__':
args = docopt(__doc__,
version='OpenAccess_EPUB v.' + __version__,
options_first=True)
log = logging.getLogger()
def printverbose(info):
if args['--verbose']:
print(info)
#Let's shape what gets passed along to the subcommands
argv = []
#If we want to pass an ubercommand option to a subcommand, do it now because
#options are first, and like so:
#if args['--verbose']:
# argv.append('--verbose') # this would pass --verbose onward
#argv += ['--verbose'] if args['--verbose'] else argv # Cute, one-liner
#Add on all of the ARGS ... (mixture of options and args for sub-parsing)
argv += args['ARGS']
#Decide which subcommand to run based on args['COMMAND']
if args['COMMAND'] == 'batch':
import openaccess_epub.commands.batch as batch
main = batch.main
printverbose(
'''Running Batch command with the following options/arguments:
{0}
Batch command script located at:
{1}'''.format(str(argv), str(batch.__file__)))
elif args['COMMAND'] == 'clearcache':
import openaccess_epub.commands.clearcache as clearcache
main = clearcache.main
printverbose(
'''Running Clearcache command with the following options/arguments:
{0}
Clearcache command script located at:
{1}'''.format(str(argv), str(clearcache.__file__)))
elif args['COMMAND'] == 'collection':
import openaccess_epub.commands.collection as collection
main = collection.main
printverbose(
'''Running Collection command with the following options/arguments:
{0}
Collection command script located at:
{1}'''.format(str(argv), str(collection.__file__)))
elif args['COMMAND'] == 'configure':
import openaccess_epub.commands.configure as configure
main = configure.main
printverbose(
'''Running Configure command with the following options/arguments:
{0}
Configure command script located at:
{1}'''.format(str(argv), str(configure.__file__)))
elif args['COMMAND'] == 'convert':
import openaccess_epub.commands.convert as convert
main = convert.main
printverbose(
'''Running Convert command with the following options/arguments:
{0}
Convert command script located at:
{1}'''.format(str(argv), str(convert.__file__)))
elif args['COMMAND'] == 'validate':
import openaccess_epub.commands.validate as validate
main = validate.main
printverbose(
'''Running Validate command with the following options/arguments:
{0}
Validate command script located at:
{1}'''.format(str(argv), str(validate.__file__)))
elif args['COMMAND'] == 'publishers':
sys.exit('''\
PLoS - http://www.plos.org/
Frontiers - http://www.frontiersin.org/''')
elif args['COMMAND'] == 'haiku':
sys.exit('''
A field of cotton--
as if the moon
had flowered.
- Matsuo Bashō''')
else:
print('Your command \'{0}\' was not recognized'.format(args['COMMAND']))
sys.exit()
main(argv=argv)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#This is the updated and more concise version
"""
OpenAccess_EPUB
Usage:
oaepub [--version | --help] [options] COMMAND [ARGS ...]
Options:
-h --help show this help message and exit
-v --version show program version and exit
-V --verbose print additional information about command execution
The available commands are:
batch Convert all the contents of a directory to individual EPUB
clearcache Delete some, or all, of the contents of OpenAccess_EPUB's cache
collection Convert multiple articles into a single omnibus EPUB
configure Configure some settings for your OpenAccess_EPUB install
convert Convert explicit input(s) individually to EPUB
publishers Show which publishers are currently supported by OpenAccess_EPUB
validate Validate article XML files according to their specification
See 'oaepub COMMAND --help' for more information on a specific command.
For more help, visit the documentation:
http://openaccess-epub.readthedocs.org
"""
#Standard Library modules
import sys
import logging
from importlib import import_module
#Non-Standard Library modules
from docopt import docopt
#OpenAccess_EPUB modules
from openaccess_epub._version import __version__
VALID_COMMANDS = ['batch', 'clearcache', 'collection', 'configure', 'convert',
'validate']
if __name__ == '__main__':
args = docopt(__doc__,
version='OpenAccess_EPUB v.' + __version__,
options_first=True)
log = logging.getLogger()
def printverbose(info):
if args['--verbose']:
print(info)
#Let's shape what gets passed along to the subcommands
argv = []
#If we want to pass an ubercommand option to a subcommand, do it now because
#options are first, and like so:
#if args['--verbose']:
# argv.append('--verbose') # this would pass --verbose onward
#argv += ['--verbose'] if args['--verbose'] else argv # Concise, one-liner
#Add on all of the ARGS ... (mixture of options and args for sub-parsing)
argv += args['ARGS']
#This is not just to be cute, it's also a test of terminal UTF-8
if args['COMMAND'] == 'haiku':
sys.exit('''
A field of cotton--
as if the moon
had flowered.
- Matsuo Bashō (松尾 芭蕉)''')
elif args['COMMAND'] == 'publishers':
sys.exit('''\
PLoS - http://www.plos.org/
Frontiers - http://www.frontiersin.org/''')
#Decide which subcommand to run based on args['COMMAND']
if args['COMMAND'] not in VALID_COMMANDS:
sys.exit('Your command \'{0}\' was not recognized'.format(args['COMMAND']))
else:
mod = import_module('openaccess_epub.commands.' + args['COMMAND'])
main = mod.main
printverbose('''\
Running {cmd} command with the following options/arguments:
{args}
{cmd} command script located at:
{filename}'''.format(cmd=args['COMMAND'], args=str(argv), filename=mod.__file__))
main(argv=argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment