Created
August 12, 2020 18:18
-
-
Save mraspaud/9ffe0b66ecad9a1445b88eeaa0e2bdfa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
from appdirs import AppDirs | |
class LutDirs: | |
"""Lut class.""" | |
def __init__(self, appname, appauthor, version=None): | |
self._appdirs = AppDirs(appname, appauthor, version=version) | |
@property | |
def user_data_dir(self): | |
return self._appdirs.user_data_dir | |
def data_component(self, component_name, version=None): | |
path = os.path.join(self._appdirs.user_data_dir, component_name) | |
if version is not None: | |
path = os.path.join(path, version) | |
return path | |
def test_user_data_dir(): | |
lut_dirs = LutDirs('lutdirs', 'Troll') | |
assert lut_dirs.user_data_dir is not None | |
def test_data_component_different_versions(): | |
lut_dirs = LutDirs('lutdirs', 'Troll') | |
assert lut_dirs.data_component('luts-for-simon', version='1.0') != \ | |
lut_dirs.data_component('luts-for-simon', version='2.0') | |
def test_data_component_contains_version(): | |
lut_dirs = LutDirs('lutdirs', 'Troll') | |
assert '1.0' in lut_dirs.data_component('luts-for-simon', version='1.0') | |
def test_data_component_contains_component_name(): | |
lut_dirs = LutDirs('lutdirs', 'Troll') | |
assert 'luts-for-simon' in lut_dirs.data_component('luts-for-simon', version='1.0') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import shutil | |
import stat | |
import tempfile | |
import urllib.request | |
from unittest.mock import patch | |
class LutDownloads: | |
"""Lut downloading.""" | |
def __init__(self, source, target): | |
self._source = source | |
self._target = target | |
def download_lut(self): | |
urllib.request.urlretrieve(self._source, self._target) | |
def update_lut(self): | |
if not os.path.isfile(self._target): | |
self.download_lut() | |
@patch('urllib.request') | |
def test_download_to_target(request_mock): | |
def put_char_in_file(url, path): | |
with open(path, 'a+') as fd: | |
fd.write('c') | |
del url | |
temp_dir = tempfile.mkdtemp() | |
target = os.path.join(temp_dir, 'bla') | |
try: | |
lut_downloads = LutDownloads('some_url', target) | |
request_mock.urlretrieve = put_char_in_file | |
lut_downloads.download_lut() | |
assert os.path.isfile(target) | |
size = os.stat(target)[stat.ST_SIZE] | |
lut_downloads.download_lut() | |
assert os.stat(target)[stat.ST_SIZE] == size + 1 | |
finally: | |
shutil.rmtree(temp_dir) | |
@patch('urllib.request') | |
def test_update_to_target(request_mock): | |
def put_char_in_file(url, path): | |
with open(path, 'a+') as fd: | |
fd.write('c') | |
del url | |
temp_dir = tempfile.mkdtemp() | |
target = os.path.join(temp_dir, 'bla') | |
try: | |
lut_downloads = LutDownloads('some_url', target) | |
request_mock.urlretrieve = put_char_in_file | |
lut_downloads.update_lut() | |
assert os.path.isfile(target) | |
size = os.stat(target)[stat.ST_SIZE] | |
lut_downloads.update_lut() | |
assert os.stat(target)[stat.ST_SIZE] == size | |
finally: | |
shutil.rmtree(temp_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment