From 74c99831143be2bb5374e96b0745a23c40ec06d1 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 29 Mar 2017 22:18:01 +0100 Subject: [PATCH] Download CSL files from github --- .gitignore | 2 ++ md_pdf/args.py | 1 + md_pdf/cli.py | 6 +++++- md_pdf/consts.py | 4 +++- md_pdf/csl.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ md_pdf/utils.py | 10 ++++++++++ setup.py | 1 + 7 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 md_pdf/csl.py diff --git a/.gitignore b/.gitignore index 45c5282..ba76f92 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,5 @@ ENV/ out/ md_pdf/assets/cover.html +md_pdf/assets/csl/ +md_pdf/assets/styles-master/ diff --git a/md_pdf/args.py b/md_pdf/args.py index cd5d440..9c219c4 100644 --- a/md_pdf/args.py +++ b/md_pdf/args.py @@ -4,4 +4,5 @@ import argparse def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", help="Display verbose output", action="store_true") + parser.add_argument("--update-csl", help="Update CSL files", action="store_true") return parser.parse_args() diff --git a/md_pdf/cli.py b/md_pdf/cli.py index 607b04f..60b8d3d 100644 --- a/md_pdf/cli.py +++ b/md_pdf/cli.py @@ -4,7 +4,7 @@ from md_pdf.exceptions import PrematureExit, BaseException from md_pdf.build import build from md_pdf.config.read import load_config from md_pdf.config.validate import validate_config - +from md_pdf.csl import check_csl, download_csl FORMAT = "[%(levelname)s]: %(message)s" logging.basicConfig(format=FORMAT, level=logging.DEBUG) @@ -13,6 +13,10 @@ logging.basicConfig(format=FORMAT, level=logging.DEBUG) def cli(): args = parse_args() try: + if args.update_csl: + download_csl() + return 0 + check_csl() config = load_config() validate_config(config) build(config) diff --git a/md_pdf/consts.py b/md_pdf/consts.py index f9f29db..eec4c62 100644 --- a/md_pdf/consts.py +++ b/md_pdf/consts.py @@ -1,7 +1,9 @@ import os PROJECT_DIR = os.path.dirname(__file__) - WORKING_DIR = os.getcwd() +ASSET_DIR = os.path.join(PROJECT_DIR, 'assets') CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml') + +CSL_DOWNLOAD_LINK = "https://github.com/citation-style-language/styles/archive/master.zip" diff --git a/md_pdf/csl.py b/md_pdf/csl.py new file mode 100644 index 0000000..51a76c5 --- /dev/null +++ b/md_pdf/csl.py @@ -0,0 +1,47 @@ +from md_pdf.consts import PROJECT_DIR, CSL_DOWNLOAD_LINK, ASSET_DIR +from md_pdf.exceptions import PrematureExit +import os +import urllib +import zipfile +import tempfile +import shutil +from md_pdf.utils import remove_dir +from progressbar import ProgressBar + +CSL_TEMP_DIR = os.path.join(ASSET_DIR, 'styles-master') +CSL_DIR = os.path.join(ASSET_DIR, 'csl') + + +def check_csl(): + if not os.path.isdir(CSL_DIR) or os.listdir(CSL_DIR) == []: + raise PrematureExit("No CSL files found!") + + +def download_csl(): + bar = ProgressBar() + + remove_dir(CSL_DIR) + remove_dir(os.path.join(ASSET_DIR, 'styles-master')) + + def download_handle(count, block_size, total_size): + bar.update(int(count * block_size * 100 / total_size)) + + _, download_location = tempfile.mkstemp() + bar.start() + urllib.request.urlretrieve(CSL_DOWNLOAD_LINK, download_location, reporthook=download_handle) + bar.finish() + + with open(download_location, 'rb') as downloaded_file: + with zipfile.ZipFile(downloaded_file) as csl_zip: + member_list = csl_zip.namelist() + bar.start(max_value=len(member_list)) + + for i, member in enumerate(member_list): + csl_zip.extract(member, path=ASSET_DIR) + bar.update(i) + + bar.finish() + + 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 e69de29..1ebd6bb 100644 --- a/md_pdf/utils.py +++ b/md_pdf/utils.py @@ -0,0 +1,10 @@ +import shutil +import os + + +def remove_dir(dir): + try: + shutil.rmtree(dir) + os.rmdir(dir) + except FileNotFoundError: + pass diff --git a/setup.py b/setup.py index f587171..515d2c0 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ setup( "dotmap==1.2.17", "jinja2==2.9.5", "pdfkit==0.6.1", + "progressbar2==3.16.0", "pypandoc==1.3.3", "PyYAML==3.12" ],