48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
|
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)
|