Add logging
This commit is contained in:
parent
aa6713eaa6
commit
6e60e72603
8 changed files with 33 additions and 4 deletions
|
@ -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.cover import render_cover
|
||||||
from md_pdf.build.pdf import export_pdf
|
from md_pdf.build.pdf import export_pdf
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
def build(config):
|
def build(config):
|
||||||
|
logger.info("Starting Build...")
|
||||||
data = read_files(os.path.abspath(config['input']))
|
data = read_files(os.path.abspath(config['input']))
|
||||||
doc = build_document(data, config.get('bibliography'), config.get('context'))
|
doc = build_document(data, config.get('bibliography'), config.get('context'))
|
||||||
if 'html' in config['output_formats']:
|
if 'html' in config['output_formats']:
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
from md_pdf.consts import ASSET_DIR
|
from md_pdf.consts import ASSET_DIR
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
COVER_TEMPLATE = os.path.join(ASSET_DIR, 'cover-template.html')
|
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):
|
def render_cover(config):
|
||||||
|
logger.info("Rendering Cover...")
|
||||||
context = config['context'].copy()
|
context = config['context'].copy()
|
||||||
context['title'] = config['title']
|
context['title'] = config['title']
|
||||||
with open(COVER_TEMPLATE) as f:
|
with open(COVER_TEMPLATE) as f:
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import pypandoc
|
import pypandoc
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import os
|
import os
|
||||||
from md_pdf.consts import PROJECT_DIR, CSL_DIR
|
from md_pdf.consts import CSL_DIR
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
def fix_references_title(content):
|
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'])))
|
'--csl={}'.format(os.path.join(CSL_DIR, "{}.csl".format(bibliography['csl'])))
|
||||||
]
|
]
|
||||||
filters.append('pandoc-citeproc')
|
filters.append('pandoc-citeproc')
|
||||||
|
logger.info("Rendering HTML...")
|
||||||
html = fix_references_title(pypandoc.convert_text(
|
html = fix_references_title(pypandoc.convert_text(
|
||||||
files_content,
|
files_content,
|
||||||
'html',
|
'html',
|
||||||
|
|
|
@ -2,6 +2,9 @@ import pdfkit
|
||||||
from md_pdf.consts import ASSET_DIR
|
from md_pdf.consts import ASSET_DIR
|
||||||
from md_pdf.build.cover import OUTPUT_COVER_FILE
|
from md_pdf.build.cover import OUTPUT_COVER_FILE
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
STYLE_FILE = os.path.join(ASSET_DIR, 'style.css')
|
STYLE_FILE = os.path.join(ASSET_DIR, 'style.css')
|
||||||
|
@ -26,7 +29,7 @@ PDF_OPTIONS = {
|
||||||
def export_pdf(content, config):
|
def export_pdf(content, config):
|
||||||
PDF_OPTIONS['title'] = config.get('title', 'Output')
|
PDF_OPTIONS['title'] = config.get('title', 'Output')
|
||||||
PDF_OPTIONS['replace'] = [(key, str(value)) for key, value in config['context'].items()]
|
PDF_OPTIONS['replace'] = [(key, str(value)) for key, value in config['context'].items()]
|
||||||
|
logger.info("Rendering PDF...")
|
||||||
return pdfkit.from_string(
|
return pdfkit.from_string(
|
||||||
content,
|
content,
|
||||||
os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'),
|
os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'),
|
||||||
|
|
|
@ -11,7 +11,7 @@ FORMAT = "[%(levelname)s]: %(message)s"
|
||||||
|
|
||||||
def cli():
|
def cli():
|
||||||
args = parse_args()
|
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:
|
try:
|
||||||
if args.update_csl:
|
if args.update_csl:
|
||||||
download_csl()
|
download_csl()
|
||||||
|
|
|
@ -2,6 +2,10 @@ from md_pdf.exceptions import ConfigValidationException
|
||||||
from md_pdf.consts import CSL_DIR
|
from md_pdf.consts import CSL_DIR
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
REQUIRED_KEYS = [
|
REQUIRED_KEYS = [
|
||||||
|
@ -63,6 +67,7 @@ def validate_context(config):
|
||||||
|
|
||||||
|
|
||||||
def validate_config(config):
|
def validate_config(config):
|
||||||
|
logger.info("Validating Config...")
|
||||||
for validator in [
|
for validator in [
|
||||||
check_required_keys,
|
check_required_keys,
|
||||||
test_input,
|
test_input,
|
||||||
|
|
|
@ -7,6 +7,9 @@ import tempfile
|
||||||
import shutil
|
import shutil
|
||||||
from md_pdf.utils import remove_dir
|
from md_pdf.utils import remove_dir
|
||||||
from progressbar import ProgressBar
|
from progressbar import ProgressBar
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
CSL_TEMP_DIR = os.path.join(ASSET_DIR, 'styles-master')
|
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))
|
bar.update(int(count * block_size * 100 / total_size))
|
||||||
|
|
||||||
_, download_location = tempfile.mkstemp()
|
_, download_location = tempfile.mkstemp()
|
||||||
|
logger.info("Downloading CSL...")
|
||||||
bar.start()
|
bar.start()
|
||||||
urllib.request.urlretrieve(CSL_DOWNLOAD_LINK, download_location, reporthook=download_handle) # nosec
|
urllib.request.urlretrieve(CSL_DOWNLOAD_LINK, download_location, reporthook=download_handle) # nosec
|
||||||
bar.finish()
|
bar.finish()
|
||||||
|
@ -34,6 +38,7 @@ def download_csl():
|
||||||
with open(download_location, 'rb') as downloaded_file:
|
with open(download_location, 'rb') as downloaded_file:
|
||||||
with zipfile.ZipFile(downloaded_file) as csl_zip:
|
with zipfile.ZipFile(downloaded_file) as csl_zip:
|
||||||
member_list = csl_zip.namelist()
|
member_list = csl_zip.namelist()
|
||||||
|
logger.info("Extracting CSL...")
|
||||||
bar.start(max_value=len(member_list))
|
bar.start(max_value=len(member_list))
|
||||||
|
|
||||||
for i, member in enumerate(member_list):
|
for i, member in enumerate(member_list):
|
||||||
|
@ -42,6 +47,7 @@ def download_csl():
|
||||||
|
|
||||||
bar.finish()
|
bar.finish()
|
||||||
|
|
||||||
|
logger.info("Cleaning Up...")
|
||||||
shutil.copytree(CSL_TEMP_DIR, CSL_DIR)
|
shutil.copytree(CSL_TEMP_DIR, CSL_DIR)
|
||||||
os.remove(download_location)
|
os.remove(download_location)
|
||||||
remove_dir(CSL_TEMP_DIR)
|
remove_dir(CSL_TEMP_DIR)
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
import shutil
|
import shutil
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__file__)
|
||||||
|
|
||||||
|
|
||||||
def remove_dir(dir):
|
def remove_dir(dir):
|
||||||
|
logger.debug("Removing directory {}.".format(dir))
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(dir)
|
shutil.rmtree(dir)
|
||||||
os.rmdir(dir)
|
os.rmdir(dir)
|
||||||
|
|
Reference in a new issue