This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf/md_pdf/csl.py

57 lines
1.6 KiB
Python
Raw Normal View History

2017-03-29 22:21:31 +01:00
from md_pdf.consts import CSL_DOWNLOAD_LINK, ASSET_DIR, CSL_DIR
2017-03-29 22:18:01 +01:00
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
2017-04-01 16:12:03 +01:00
import logging
logger = logging.getLogger(__file__)
2017-03-29 22:18:01 +01:00
2017-03-29 22:21:31 +01:00
2017-03-29 22:18:01 +01:00
CSL_TEMP_DIR = os.path.join(ASSET_DIR, 'styles-master')
def check_csl():
if not os.path.isdir(CSL_DIR) or os.listdir(CSL_DIR) == []:
2017-04-09 14:14:27 +01:00
raise PrematureExit("No CSL files found! Run again with --update-csl")
2017-03-29 22:18:01 +01:00
def download_csl():
bar = ProgressBar()
remove_dir(CSL_DIR)
def download_handle(count, block_size, total_size):
2017-05-04 08:39:38 +01:00
if total_size < 1: # only update the bar if we have a size
return
2017-03-29 22:18:01 +01:00
bar.update(int(count * block_size * 100 / total_size))
_, download_location = tempfile.mkstemp()
2017-04-01 16:12:03 +01:00
logger.info("Downloading CSL...")
2017-03-29 22:18:01 +01:00
bar.start()
2017-03-30 18:34:32 +01:00
urllib.request.urlretrieve(CSL_DOWNLOAD_LINK, download_location, reporthook=download_handle) # nosec
2017-03-29 22:18:01 +01:00
bar.finish()
with open(download_location, 'rb') as downloaded_file:
with zipfile.ZipFile(downloaded_file) as csl_zip:
member_list = csl_zip.namelist()
2017-04-01 16:12:03 +01:00
logger.info("Extracting CSL...")
2017-03-29 22:18:01 +01:00
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()
2017-04-01 16:12:03 +01:00
logger.info("Cleaning Up...")
2017-04-27 13:39:59 +01:00
os.close(_)
2017-03-29 22:18:01 +01:00
shutil.copytree(CSL_TEMP_DIR, CSL_DIR)
os.remove(download_location)
remove_dir(CSL_TEMP_DIR)
2017-03-30 18:34:32 +01:00
urllib.request.urlcleanup()