From 6e60e72603b524d1452b9316562ee939e1ff74af Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 1 Apr 2017 16:12:03 +0100 Subject: [PATCH] Add logging --- md_pdf/build/__init__.py | 4 ++++ md_pdf/build/cover.py | 4 ++++ md_pdf/build/pandoc.py | 7 +++++-- md_pdf/build/pdf.py | 5 ++++- md_pdf/cli.py | 2 +- md_pdf/config/validate.py | 5 +++++ md_pdf/csl.py | 6 ++++++ md_pdf/utils.py | 4 ++++ 8 files changed, 33 insertions(+), 4 deletions(-) diff --git a/md_pdf/build/__init__.py b/md_pdf/build/__init__.py index abd99ab..ce2b772 100644 --- a/md_pdf/build/__init__.py +++ b/md_pdf/build/__init__.py @@ -3,9 +3,13 @@ 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 +import logging + +logger = logging.getLogger(__file__) def build(config): + logger.info("Starting Build...") data = read_files(os.path.abspath(config['input'])) doc = build_document(data, config.get('bibliography'), config.get('context')) if 'html' in config['output_formats']: diff --git a/md_pdf/build/cover.py b/md_pdf/build/cover.py index c73cb10..7856f2e 100644 --- a/md_pdf/build/cover.py +++ b/md_pdf/build/cover.py @@ -1,6 +1,9 @@ from jinja2 import Template from md_pdf.consts import ASSET_DIR import os +import logging + +logger = logging.getLogger(__file__) COVER_TEMPLATE = os.path.join(ASSET_DIR, 'cover-template.html') @@ -8,6 +11,7 @@ OUTPUT_COVER_FILE = os.path.join(ASSET_DIR, 'cover.html') def render_cover(config): + logger.info("Rendering Cover...") context = config['context'].copy() context['title'] = config['title'] with open(COVER_TEMPLATE) as f: diff --git a/md_pdf/build/pandoc.py b/md_pdf/build/pandoc.py index fa92185..5921578 100644 --- a/md_pdf/build/pandoc.py +++ b/md_pdf/build/pandoc.py @@ -1,8 +1,11 @@ import pypandoc from bs4 import BeautifulSoup import os -from md_pdf.consts import PROJECT_DIR, CSL_DIR +from md_pdf.consts import CSL_DIR from jinja2 import Template +import logging + +logger = logging.getLogger(__file__) def fix_references_title(content): @@ -36,7 +39,7 @@ def build_document(files_content, bibliography, context): '--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography['csl']))) ] filters.append('pandoc-citeproc') - + logger.info("Rendering HTML...") html = fix_references_title(pypandoc.convert_text( files_content, 'html', diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index a35fb90..a11ce51 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -2,6 +2,9 @@ import pdfkit from md_pdf.consts import ASSET_DIR from md_pdf.build.cover import OUTPUT_COVER_FILE import os +import logging + +logger = logging.getLogger(__file__) STYLE_FILE = os.path.join(ASSET_DIR, 'style.css') @@ -26,7 +29,7 @@ PDF_OPTIONS = { def export_pdf(content, config): PDF_OPTIONS['title'] = config.get('title', 'Output') PDF_OPTIONS['replace'] = [(key, str(value)) for key, value in config['context'].items()] - + logger.info("Rendering PDF...") return pdfkit.from_string( content, os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'), diff --git a/md_pdf/cli.py b/md_pdf/cli.py index 8ff13d8..38ab70f 100644 --- a/md_pdf/cli.py +++ b/md_pdf/cli.py @@ -11,7 +11,7 @@ FORMAT = "[%(levelname)s]: %(message)s" def cli(): args = parse_args() - logging.basicConfig(format=FORMAT, level=logging.INFO if args.verbose else logging.NOTSET) + logging.basicConfig(format=FORMAT, level=logging.WARN if args.verbose else logging.INFO) try: if args.update_csl: download_csl() diff --git a/md_pdf/config/validate.py b/md_pdf/config/validate.py index 3a31a17..3b7a52d 100644 --- a/md_pdf/config/validate.py +++ b/md_pdf/config/validate.py @@ -2,6 +2,10 @@ from md_pdf.exceptions import ConfigValidationException from md_pdf.consts import CSL_DIR import glob import os +import logging + + +logger = logging.getLogger(__file__) REQUIRED_KEYS = [ @@ -63,6 +67,7 @@ def validate_context(config): def validate_config(config): + logger.info("Validating Config...") for validator in [ check_required_keys, test_input, diff --git a/md_pdf/csl.py b/md_pdf/csl.py index e6c0aaf..6186d2c 100644 --- a/md_pdf/csl.py +++ b/md_pdf/csl.py @@ -7,6 +7,9 @@ import tempfile import shutil from md_pdf.utils import remove_dir from progressbar import ProgressBar +import logging + +logger = logging.getLogger(__file__) CSL_TEMP_DIR = os.path.join(ASSET_DIR, 'styles-master') @@ -27,6 +30,7 @@ def download_csl(): bar.update(int(count * block_size * 100 / total_size)) _, download_location = tempfile.mkstemp() + logger.info("Downloading CSL...") bar.start() urllib.request.urlretrieve(CSL_DOWNLOAD_LINK, download_location, reporthook=download_handle) # nosec bar.finish() @@ -34,6 +38,7 @@ def download_csl(): with open(download_location, 'rb') as downloaded_file: with zipfile.ZipFile(downloaded_file) as csl_zip: member_list = csl_zip.namelist() + logger.info("Extracting CSL...") bar.start(max_value=len(member_list)) for i, member in enumerate(member_list): @@ -42,6 +47,7 @@ def download_csl(): bar.finish() + logger.info("Cleaning Up...") shutil.copytree(CSL_TEMP_DIR, CSL_DIR) os.remove(download_location) remove_dir(CSL_TEMP_DIR) diff --git a/md_pdf/utils.py b/md_pdf/utils.py index 1ebd6bb..1009166 100644 --- a/md_pdf/utils.py +++ b/md_pdf/utils.py @@ -1,8 +1,12 @@ import shutil import os +import logging + +logger = logging.getLogger(__file__) def remove_dir(dir): + logger.debug("Removing directory {}.".format(dir)) try: shutil.rmtree(dir) os.rmdir(dir)