From 7bbe0cbcd6dfa91e293c90c27b58a902f4b3b879 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 29 Mar 2017 18:26:54 +0100 Subject: [PATCH] Start rewrite to config-file based cli --- md_pdf/args.py | 7 ++++++ md_pdf/build/__init__.py | 14 ++++++++++++ md_pdf/build/cli.py | 20 ----------------- md_pdf/build/cover.py | 2 +- md_pdf/build/md.py | 3 ++- md_pdf/build/pandoc.py | 2 +- md_pdf/build/pdf.py | 2 +- md_pdf/cli.py | 46 ++++++++++++--------------------------- md_pdf/config/read.py | 8 +++++++ md_pdf/config/validate.py | 0 md_pdf/consts.py | 7 ++++++ md_pdf/exceptions.py | 2 ++ md_pdf/utils.py | 5 ----- setup.py | 4 ++-- test-files/mdp.yml | 0 15 files changed, 59 insertions(+), 63 deletions(-) create mode 100644 md_pdf/args.py delete mode 100644 md_pdf/build/cli.py create mode 100644 md_pdf/config/read.py create mode 100644 md_pdf/config/validate.py create mode 100644 md_pdf/consts.py create mode 100644 md_pdf/exceptions.py create mode 100644 test-files/mdp.yml diff --git a/md_pdf/args.py b/md_pdf/args.py new file mode 100644 index 0000000..cd5d440 --- /dev/null +++ b/md_pdf/args.py @@ -0,0 +1,7 @@ +import argparse + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("-v", "--verbose", help="Display verbose output", action="store_true") + return parser.parse_args() diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index e69de29..f160766 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -0,0 +1,14 @@ +from md_pdf.consts import WORKING_DIR +from md_pdf.build.md import read_files +from md_pdf.build.pandoc import build_document, output_html +from md_pdf.build.cover import render_cover +from md_pdf.build.pdf import export_pdf +import os + + +def build(args, config): + data = read_files(os.path.join(WORKING_DIR, '*.md')) + doc = build_document(data, os.path.join(WORKING_DIR, 'bib.yaml')) + output_html(doc, os.path.join(WORKING_DIR, 'out')) + render_cover() + export_pdf(doc, os.path.join(WORKING_DIR, 'out')) diff --git a/md_pdf/build/cli.py b/md_pdf/build/cli.py deleted file mode 100644 index 6830758..0000000 --- a/md_pdf/build/cli.py +++ /dev/null @@ -1,20 +0,0 @@ -import click -from md_pdf.build.md import read_files -from md_pdf.build.pandoc import build_document, output_html -from md_pdf.build.cover import render_cover -from md_pdf.build.pdf import export_pdf - - -@click.command('build', short_help="Build document") -@click.argument('in_files', type=click.Path(dir_okay=False, resolve_path=True, writable=True), nargs=-1) -@click.option('--bibliography', '-b', type=click.Path(dir_okay=False, resolve_path=True, writable=True), default=None) -@click.option('--output', '-o', type=click.Path(file_okay=False, resolve_path=True, writable=True), default='out/') -def cli(in_files, bibliography, output): - data = read_files(in_files) - doc = build_document(data, bibliography) - output_html(doc, output) - render_cover() - export_pdf(doc, output) - return 0 - - diff --git a/md_pdf/build/cover.py b/md_pdf/build/cover.py index c799d01..70fbab9 100644 --- a/md_pdf/build/cover.py +++ b/md_pdf/build/cover.py @@ -1,5 +1,5 @@ from jinja2 import Template -from md_pdf.utils import PROJECT_DIR +from md_pdf.consts import PROJECT_DIR import os diff --git a/md_pdf/build/md.py b/md_pdf/build/md.py index fb92365..ea55c9f 100644 --- a/md_pdf/build/md.py +++ b/md_pdf/build/md.py @@ -6,5 +6,6 @@ def get_files_content(filenames): yield f.read() -def read_files(filenames): +def read_files(files_glob): + filenames = glob.iglob(files_glob) return '\n'.join(list(get_files_content(filenames))) diff --git a/md_pdf/build/pandoc.py b/md_pdf/build/pandoc.py index 8cd878c..dc42a12 100644 --- a/md_pdf/build/pandoc.py +++ b/md_pdf/build/pandoc.py @@ -1,7 +1,7 @@ import pypandoc from bs4 import BeautifulSoup import os.path -from md_pdf.utils import PROJECT_DIR +from md_pdf.consts import PROJECT_DIR CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl') diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index 4454b64..908f4c3 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -1,5 +1,5 @@ import pdfkit -from md_pdf.utils import PROJECT_DIR +from md_pdf.consts import PROJECT_DIR from md_pdf.build.cover import OUTPUT_COVER_FILE import os diff --git a/md_pdf/cli.py b/md_pdf/cli.py index facb294..a34d545 100644 --- a/md_pdf/cli.py +++ b/md_pdf/cli.py @@ -1,40 +1,22 @@ -import os.path -import click import logging +from md_pdf.args import parse_args +from md_pdf.exceptions import PrematureExit +from md_pdf.build import build +from md_pdf.config.read import load_config FORMAT = "[%(levelname)s]: %(message)s" logging.basicConfig(format=FORMAT, level=logging.DEBUG) -logging.getLogger("requests").setLevel(logging.WARNING) -class MDPDFCLI(click.MultiCommand): - - def list_commands(self, ctx): - return ['build'] - - def get_command(self, ctx, name): - ns = {} - if name not in self.list_commands(ctx): - return - try: - fn = os.path.join(os.path.dirname(__file__), name + '/cli.py') - with open(fn) as f: - code = compile(f.read(), fn, 'exec') - eval(code, ns, ns) - return ns['cli'] - except Exception as e: - print("An Error Occured:s") - raise e - - -cli = MDPDFCLI(help='This tool\'s subcommands are loaded from a plugin folder dynamically.') - - -@click.command(cls=MDPDFCLI) def cli(): - pass - - -if __name__ == '__main__': - cli() + args = parse_args() + try: + config = load_config() + build(args, config) + except PrematureExit: + return 0 + except Exception as e: + logging.error(str(e)) + return 1 + return 0 diff --git a/md_pdf/config/read.py b/md_pdf/config/read.py new file mode 100644 index 0000000..5a6f0d4 --- /dev/null +++ b/md_pdf/config/read.py @@ -0,0 +1,8 @@ +import yaml +import os +from md_pdf.consts import CONFIG_FILE + + +def load_config(): + with open(os.path.join(CONFIG_FILE)) as f: + return yaml.load(f) diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py new file mode 100644 index 0000000..e69de29 diff --git a/md_pdf/consts.py b/md_pdf/consts.py new file mode 100644 index 0000000..f9f29db --- /dev/null +++ b/md_pdf/consts.py @@ -0,0 +1,7 @@ +import os + +PROJECT_DIR = os.path.dirname(__file__) + +WORKING_DIR = os.getcwd() + +CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml') diff --git a/md_pdf/exceptions.py b/md_pdf/exceptions.py new file mode 100644 index 0000000..abc8e8e --- /dev/null +++ b/md_pdf/exceptions.py @@ -0,0 +1,2 @@ +class PrematureExit(Exception): + pass diff --git a/md_pdf/utils.py b/md_pdf/utils.py index 5b65c76..e69de29 100644 --- a/md_pdf/utils.py +++ b/md_pdf/utils.py @@ -1,5 +0,0 @@ -import os - -PROJECT_DIR = os.path.dirname(__file__) - -WORKING_DIR = os.getcwd() diff --git a/setup.py b/setup.py index 66db80b..75cf615 100644 --- a/setup.py +++ b/setup.py @@ -6,10 +6,10 @@ setup( version="1.0", install_requires=[ "beautifulsoup4==4.5.3", - "click==6.7.0", "jinja2==2.9.5", "pdfkit==0.6.1", - "pypandoc==1.3.3" + "pypandoc==1.3.3", + "PyYAML==3.12" ], entry_points=""" [console_scripts] diff --git a/test-files/mdp.yml b/test-files/mdp.yml new file mode 100644 index 0000000..e69de29