Skip to content

Add data updater #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 29, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added logging to the update functions
  • Loading branch information
Ayuto committed Jul 4, 2017
commit fae1c9c84f9f158bc2412d9f9ebb20856023f729
19 changes: 14 additions & 5 deletions addons/source-python/packages/source-python/core/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from urllib.request import urlopen

# Source.Python Imports
# Core
from core import core_logger
# Paths
from paths import DATA_PATH
from paths import SP_DATA_PATH

Expand All @@ -20,20 +23,23 @@
# >> ALL DECLARATION
# =============================================================================
__all__ = (
'CHECKSUM_URL',
'DATA_URL',
'DATA_ZIP_FILE',
'CHECKSUM_URL',
'DATA_URL',
'DATA_ZIP_FILE',
'download_latest_data',
'get_latest_data_checksum',
'is_new_data_available',
'unpack_data',
'is_new_data_available',
'unpack_data',
'update_data'
)


# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Don't use __getattr__ here. 'update' is a method of the _LogInstance class.
update_logger = core_logger['update']

DATA_ZIP_FILE = DATA_PATH / 'source-python-data.zip'
CHECKSUM_URL = 'http://data.sourcepython.com/checksum.txt'
DATA_URL = 'http://data.sourcepython.com/source-python-data.zip'
Expand All @@ -58,6 +64,7 @@ def download_latest_data(timeout=3):
:param float timeout:
Number of seconds that need to pass until a timeout occurs.
"""
update_logger.log_debug('Downloading data to {} ...'.format(DATA_ZIP_FILE))
with urlopen(DATA_URL, timeout=timeout) as url:
data = url.read()

Expand All @@ -66,6 +73,7 @@ def download_latest_data(timeout=3):

def unpack_data():
"""Unpack ``source-python-data.zip``."""
update_logger.log_debug('Extracting data in {} ...'.format(DATA_PATH))
with ZipFile(DATA_ZIP_FILE) as zip:
zip.extractall(DATA_PATH)

Expand All @@ -77,6 +85,7 @@ def update_data(timeout=3):
"""
download_latest_data(timeout)
if SP_DATA_PATH.isdir():
update_logger.log_debug('Removing {} ...'.format(SP_DATA_PATH))
SP_DATA_PATH.rmtree()

unpack_data()
Expand Down