Start rewrite to config-file based cli
This commit is contained in:
parent
ededd98e77
commit
7bbe0cbcd6
15 changed files with 59 additions and 63 deletions
7
md_pdf/args.py
Normal file
7
md_pdf/args.py
Normal file
|
@ -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()
|
|
@ -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'))
|
|
@ -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
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
from md_pdf.utils import PROJECT_DIR
|
from md_pdf.consts import PROJECT_DIR
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,5 +6,6 @@ def get_files_content(filenames):
|
||||||
yield f.read()
|
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)))
|
return '\n'.join(list(get_files_content(filenames)))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import pypandoc
|
import pypandoc
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import os.path
|
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')
|
CSL_FILE = os.path.join(PROJECT_DIR, 'assets', 'harverd.csl')
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import pdfkit
|
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
|
from md_pdf.build.cover import OUTPUT_COVER_FILE
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
|
@ -1,40 +1,22 @@
|
||||||
import os.path
|
|
||||||
import click
|
|
||||||
import logging
|
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"
|
FORMAT = "[%(levelname)s]: %(message)s"
|
||||||
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
|
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():
|
def cli():
|
||||||
pass
|
args = parse_args()
|
||||||
|
try:
|
||||||
|
config = load_config()
|
||||||
if __name__ == '__main__':
|
build(args, config)
|
||||||
cli()
|
except PrematureExit:
|
||||||
|
return 0
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(str(e))
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
8
md_pdf/config/read.py
Normal file
8
md_pdf/config/read.py
Normal file
|
@ -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)
|
0
md_pdf/config/validate.py
Normal file
0
md_pdf/config/validate.py
Normal file
7
md_pdf/consts.py
Normal file
7
md_pdf/consts.py
Normal file
|
@ -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')
|
2
md_pdf/exceptions.py
Normal file
2
md_pdf/exceptions.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
class PrematureExit(Exception):
|
||||||
|
pass
|
|
@ -1,5 +0,0 @@
|
||||||
import os
|
|
||||||
|
|
||||||
PROJECT_DIR = os.path.dirname(__file__)
|
|
||||||
|
|
||||||
WORKING_DIR = os.getcwd()
|
|
4
setup.py
4
setup.py
|
@ -6,10 +6,10 @@ setup(
|
||||||
version="1.0",
|
version="1.0",
|
||||||
install_requires=[
|
install_requires=[
|
||||||
"beautifulsoup4==4.5.3",
|
"beautifulsoup4==4.5.3",
|
||||||
"click==6.7.0",
|
|
||||||
"jinja2==2.9.5",
|
"jinja2==2.9.5",
|
||||||
"pdfkit==0.6.1",
|
"pdfkit==0.6.1",
|
||||||
"pypandoc==1.3.3"
|
"pypandoc==1.3.3",
|
||||||
|
"PyYAML==3.12"
|
||||||
],
|
],
|
||||||
entry_points="""
|
entry_points="""
|
||||||
[console_scripts]
|
[console_scripts]
|
||||||
|
|
0
test-files/mdp.yml
Normal file
0
test-files/mdp.yml
Normal file
Reference in a new issue