def test_context_manager(self): settings = Settings() settings.configure(debug=False) with settings(debug=True): self.assertEqual(True, settings.debug) self.assertEqual(False, settings.debug)
def test_as_dict(self): settings = Settings() settings.configure(debug=False) with settings(debug=True): self.assertEqual(dict(debug=True), settings.as_dict()) self.assertEqual(dict(debug=False), settings.as_dict())
def test_warning_when_adding_duplicate(self): settings = Settings() settings.configure() settings2 = Settings() settings2.configure(settings) with pytest.warns(DuplicateConfigureWarning): settings.configure(settings2)
def test_decorator(self): settings = Settings() settings.configure(debug=False) @settings(debug=True) def go(): self.assertEqual(True, settings.debug) go() self.assertEqual(False, settings.debug)
def test_decorator_in_class(self): _self = self settings = Settings() settings.configure(debug=False) class Dummy(object): @settings(debug=True) def go(self): _self.assertEqual(True, settings.debug) Dummy().go() self.assertEqual(False, settings.debug)
def test_with_object(self): class MySettings(object): def __init__(self): self.debug = False settings = Settings() settings.configure(MySettings()) self.assertEqual(False, settings.debug) with settings(debug=True): self.assertEqual(True, settings.debug)
def test_with_prefix(self): ss = Settings() ss.configure(MY_a=1, OTHER_a=2, a=3, c=5, MY_b='2') settings = PrefixedSettings(ss, 'MY_') self.assertEqual(1, settings.a) self.assertEqual('2', settings.b) with self.assertRaises(AttributeError): a = settings.c self.fail(a) with self.assertRaises(AttributeError): a = settings.MY_a self.fail(a)
def test_with_prefix(self): ss = Settings() ss.configure( MY_a=1, OTHER_a=2, a=3, c=5, MY_b='2') settings = PrefixedSettings(ss, 'MY_') self.assertEqual(1, settings.a) self.assertEqual('2', settings.b) with self.assertRaises(AttributeError): a = settings.c self.fail(a) with self.assertRaises(AttributeError): a = settings.MY_a self.fail(a)
def test_no_prefix(self): ss = Settings() ss.configure(a=1, b='2') settings = PrefixedSettings(ss) self.assertEqual(1, settings.a) self.assertEqual('2', settings.b)
def test_default_settings(self): settings = Settings() settings.configure(debug=False) self.assertEqual(False, settings.debug)
""" Time Execution decorator """ import socket import time import six from fqn_decorators import Decorator from pkgsettings import Settings from .constants import PY_35_GT SHORT_HOSTNAME = socket.gethostname() settings = Settings() settings.configure(backends=[], hooks=[], duration_field='value') def write_metric(name, **metric): for backend in settings.backends: backend.write(name, **metric) def _apply_hooks(**kwargs): metadata = dict() for hook in settings.hooks: hook_result = hook(**kwargs) if hook_result: metadata.update(hook_result) return metadata
""" Time Execution decorator """ import socket import time import six from fqn_decorators import Decorator from pkgsettings import Settings SHORT_HOSTNAME = socket.gethostname() settings = Settings() settings.configure( backends=[], hooks=[], duration_field='value' ) def write_metric(name, **metric): for backend in settings.backends: backend.write(name, **metric) def _apply_hooks(**kwargs): metadata = dict() for hook in settings.hooks: hook_result = hook(**kwargs) if hook_result: metadata.update(hook_result)
# -*- coding: utf-8 -*- from pkgsettings import Settings settings = Settings() settings.configure( DO_USE_SANDBOX=False, CONSUMER_KEY='myorcidappkey', CONSUMER_SECRET='myorcidappsecret', REQUEST_TIMEOUT=30, )
def test_key_not_found(self): settings = Settings() settings.configure() with self.assertRaises(AttributeError): getattr(settings, 'debug')
# -*- coding: utf-8 -*- from pkgsettings import Settings settings = Settings() settings.configure( BASE_URL='localhost/api', REQUEST_TIMEOUT=30, HTTP_AUTH_USERNAME='******', HTTP_AUTH_PASSWORD='******', )
from pkgsettings import Settings DEFAULT_LOCAL_STORAGE_PATH = os.path.join(os.path.expanduser('~'), 'opendatastorage') config = configparser.ConfigParser() config['Storage'] = {} config['Storage']['local_storage_path'] = DEFAULT_LOCAL_STORAGE_PATH config.read('opendata.ini') local_storage = config['Storage']['local_storage_path'] if not os.path.isdir(local_storage): if local_storage == DEFAULT_LOCAL_STORAGE_PATH: os.makedirs(DEFAULT_LOCAL_STORAGE_PATH) print(f'Local storage directory ({DEFAULT_LOCAL_STORAGE_PATH}) created') else: error_message = f'The specified local storage path {local_storage} does not exist' raise OSError(error_message) settings = Settings() settings.configure( bucket_name='goldencheetah-opendata', data_prefix='data', metadata_prefix='metadata', datasets_prefix='datasets', local_storage=config['Storage']['local_storage_path'] )