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

49 lines
1.3 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-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) == []:
raise PrematureExit("No CSL files found!")
def download_csl():
bar = ProgressBar()
remove_dir(CSL_DIR)
2017-03-31 18:34:42 +01:00
remove_dir(CSL_DOWNLOAD_LINK)
2017-03-29 22:18:01 +01:00
def download_handle(count, block_size, total_size):
bar.update(int(count * block_size * 100 / total_size))
_, download_location = tempfile.mkstemp()
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()
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)
2017-03-30 18:34:32 +01:00
urllib.request.urlcleanup()