Add logging

This commit is contained in:
Jake Howard 2017-04-01 16:12:03 +01:00
parent aa6713eaa6
commit 6e60e72603
8 changed files with 33 additions and 4 deletions

View file

@ -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']:

View file

@ -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:

View file

@ -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',

View file

@ -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'),

View file

@ -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()

View file

@ -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,

View file

@ -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)

View file

@ -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)