Download CSL files from github

This commit is contained in:
Jake Howard 2017-03-29 22:18:01 +01:00
parent 82b5b93128
commit 74c9983114
7 changed files with 69 additions and 2 deletions

2
.gitignore vendored
View file

@ -89,3 +89,5 @@ ENV/
out/ out/
md_pdf/assets/cover.html md_pdf/assets/cover.html
md_pdf/assets/csl/
md_pdf/assets/styles-master/

View file

@ -4,4 +4,5 @@ import argparse
def parse_args(): def parse_args():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="Display verbose output", action="store_true") 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() return parser.parse_args()

View file

@ -4,7 +4,7 @@ from md_pdf.exceptions import PrematureExit, BaseException
from md_pdf.build import build from md_pdf.build import build
from md_pdf.config.read import load_config from md_pdf.config.read import load_config
from md_pdf.config.validate import validate_config from md_pdf.config.validate import validate_config
from md_pdf.csl import check_csl, download_csl
FORMAT = "[%(levelname)s]: %(message)s" FORMAT = "[%(levelname)s]: %(message)s"
logging.basicConfig(format=FORMAT, level=logging.DEBUG) logging.basicConfig(format=FORMAT, level=logging.DEBUG)
@ -13,6 +13,10 @@ logging.basicConfig(format=FORMAT, level=logging.DEBUG)
def cli(): def cli():
args = parse_args() args = parse_args()
try: try:
if args.update_csl:
download_csl()
return 0
check_csl()
config = load_config() config = load_config()
validate_config(config) validate_config(config)
build(config) build(config)

View file

@ -1,7 +1,9 @@
import os import os
PROJECT_DIR = os.path.dirname(__file__) PROJECT_DIR = os.path.dirname(__file__)
WORKING_DIR = os.getcwd() WORKING_DIR = os.getcwd()
ASSET_DIR = os.path.join(PROJECT_DIR, 'assets')
CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml') CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml')
CSL_DOWNLOAD_LINK = "https://github.com/citation-style-language/styles/archive/master.zip"

47
md_pdf/csl.py Normal file
View file

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

View file

@ -0,0 +1,10 @@
import shutil
import os
def remove_dir(dir):
try:
shutil.rmtree(dir)
os.rmdir(dir)
except FileNotFoundError:
pass

View file

@ -9,6 +9,7 @@ setup(
"dotmap==1.2.17", "dotmap==1.2.17",
"jinja2==2.9.5", "jinja2==2.9.5",
"pdfkit==0.6.1", "pdfkit==0.6.1",
"progressbar2==3.16.0",
"pypandoc==1.3.3", "pypandoc==1.3.3",
"PyYAML==3.12" "PyYAML==3.12"
], ],