chg: [generator] add an option to handle gzip file
- It doesn't change the default behavior assuming it's a non-gzip file - If the `gzip_enable` is set to `True` then the destination file is uncompressedpull/243/head
parent
ab2938e008
commit
811869148f
|
|
@ -5,6 +5,7 @@ import logging
|
||||||
from inspect import currentframe, getframeinfo, getmodulename, stack
|
from inspect import currentframe, getframeinfo, getmodulename, stack
|
||||||
from os import mkdir, path
|
from os import mkdir, path
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
|
import gzip
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import dns.exception
|
import dns.exception
|
||||||
|
|
@ -35,7 +36,7 @@ def init_logging():
|
||||||
init_logging()
|
init_logging()
|
||||||
|
|
||||||
|
|
||||||
def download_to_file(url, file):
|
def download_to_file(url, file, gzip_enable=False):
|
||||||
frame_records = stack()[1]
|
frame_records = stack()[1]
|
||||||
caller = getmodulename(frame_records[1]).upper()
|
caller = getmodulename(frame_records[1]).upper()
|
||||||
|
|
||||||
|
|
@ -51,30 +52,34 @@ def download_to_file(url, file):
|
||||||
if(url_datetime > file_datetime):
|
if(url_datetime > file_datetime):
|
||||||
logging.info('{} File on server is newer, so downloading update to {}'.format(
|
logging.info('{} File on server is newer, so downloading update to {}'.format(
|
||||||
caller, get_abspath_source_file(file)))
|
caller, get_abspath_source_file(file)))
|
||||||
actual_download_to_file(url, file, user_agent)
|
actual_download_to_file(url, file, user_agent, gzip_enable=gzip_enable)
|
||||||
else:
|
else:
|
||||||
logging.info(
|
logging.info(
|
||||||
'{} File on server is older, nothing to do'.format(caller))
|
'{} File on server is older, nothing to do'.format(caller))
|
||||||
except KeyError as exc:
|
except KeyError as exc:
|
||||||
logging.warning('{} KeyError in the headers. the {} header was not sent by server {}. Downloading file'.format(
|
logging.warning('{} KeyError in the headers. the {} header was not sent by server {}. Downloading file'.format(
|
||||||
caller, str(exc), url))
|
caller, str(exc), url))
|
||||||
actual_download_to_file(url, file, user_agent)
|
actual_download_to_file(url, file, user_agent, gzip_enable=gzip_enable)
|
||||||
except FileNotFoundError as exc:
|
except FileNotFoundError as exc:
|
||||||
logging.info(
|
logging.info(
|
||||||
"{} File didn't exist, so downloading {} from {}".format(caller, file, url))
|
"{} File didn't exist, so downloading {} from {}".format(caller, file, url))
|
||||||
actual_download_to_file(url, file, user_agent)
|
actual_download_to_file(url, file, user_agent, gzip_enable=gzip_enable)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
'{} General exception occured: {}.'.format(caller, str(exc)))
|
'{} General exception occured: {}.'.format(caller, str(exc)))
|
||||||
actual_download_to_file(url, file, user_agent)
|
actual_download_to_file(url, file, user_agent, gzip_enable=gzip_enable)
|
||||||
|
|
||||||
|
|
||||||
def actual_download_to_file(url, file, user_agent):
|
def actual_download_to_file(url, file, user_agent, gzip_enable=False):
|
||||||
r = requests.get(url, headers=user_agent)
|
r = requests.get(url, headers=user_agent)
|
||||||
with open(get_abspath_source_file(file), 'wb') as fd:
|
with open(get_abspath_source_file(file), 'wb') as fd:
|
||||||
for chunk in r.iter_content(4096):
|
for chunk in r.iter_content(4096):
|
||||||
fd.write(chunk)
|
fd.write(chunk)
|
||||||
|
if gzip_enable:
|
||||||
|
with gzip.open(get_abspath_source_file(file), 'rb') as fgzip:
|
||||||
|
file_content = fgzip.read()
|
||||||
|
with open(get_abspath_source_file(file), 'wb') as fd:
|
||||||
|
fd.write(file_content)
|
||||||
|
|
||||||
def process_stream(url):
|
def process_stream(url):
|
||||||
r = requests.get(url, stream=True)
|
r = requests.get(url, stream=True)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue