async def main(): log_levels = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'] config_template = { 'devices': confuse.Sequence(BluetoothDeviceConfuseTemplate(), ), 'log_level': confuse.Choice(log_levels, default=DEFAULT_LOG_LEVEL), 'mqtt': { 'enabled': confuse.Choice([True, False], default=DEFAULT_MQTT_ENABLED), 'host': confuse.String(default=DEFAULT_MQTT_HOST), 'log_level': confuse.Choice(['ERROR', 'WARNING', 'INFO', 'DEBUG'], default=DEFAULT_MQTT_LOG_LEVEL), 'port': confuse.Integer(default=DEFAULT_MQTT_PORT), 'protocol': confuse.String(default=DEFAULT_MQTT_PROTOCOL), }, 'scheduler': { 'log_level': confuse.Choice(log_levels, default=DEFAULT_SCHEDULER_LOG_LEVEL), }, } config = confuse.Configuration('bluetooth_tracker', __name__).get(config_template) kwargs = {} kwargs['bluetooth_rssi_scanner'] = FakeBluetoothScanner kwargs['bluetooth_lookup_name_func'] = lambda *_: 'test' detector = Detector( config, mqtt.Client(), AsyncIOScheduler(), **kwargs, ) detector.start_detecting() killer = GracefulKiller() while not killer.kill_now: await asyncio.sleep(1) logging.info('shutting down') detector.stop_detecting()
import sys import confuse class Dict(confuse.Template): def convert(self, value, view): if isinstance(value, dict): return value else: self.fail(u"must be a dict", view, True) _metric_template = { "name": confuse.String(), "type": confuse.Choice(choices=["Enum", "Gauge"]), "desc": confuse.String(default=""), "kwargs": Dict(default={}), "make_label": confuse.Choice(choices=[True, False], default=False), "uve_type": confuse.String(), "uve_module": confuse.String(), "uve_instances": confuse.StrSeq(default=[]), "json_path": confuse.String(), "append_field_name": confuse.Choice(choices=[True, False], default=True), "labels_from_path": Dict(default={}), } _global_template = { "analytics": { "host": confuse.String(pattern=r"^https?://"), "base_url": confuse.String(pattern=r"^/", default="/analytics/uves"),
def test_validate_bad_choice_in_dict(self): config = _root({'foo': 3}) with self.assertRaises(confuse.ConfigValueError): config['foo'].get(confuse.Choice({2: 'two', 4: 'four'}))
def test_validate_good_choice_in_dict(self): config = _root({'foo': 2}) valid = config['foo'].get(confuse.Choice({2: 'two', 4: 'four'})) self.assertEqual(valid, 'two')
def test_validate_bad_choice_in_list(self): config = _root({'foo': 3}) with self.assertRaises(confuse.ConfigValueError): config['foo'].get(confuse.Choice([1, 2, 4, 8, 16]))
def test_validate_good_choice_in_list(self): config = _root({'foo': 2}) valid = config['foo'].get(confuse.Choice([1, 2, 4, 8, 16])) self.assertEqual(valid, 2)
import sys import confuse from trakt_scrobbler import config, logger APP_NAME = 'Trakt Scrobbler' enable_notifs = config['general']['enable_notifs'].get( confuse.Choice([True, False], default=True) ) if enable_notifs: if sys.platform == 'win32': from win10toast import ToastNotifier toaster = ToastNotifier() else: try: from jeepney import DBusAddress, new_method_call from jeepney.io.blocking import open_dbus_connection except (ImportError, ModuleNotFoundError): import subprocess as sp notifier, notif_id = None, None else: notifier = DBusAddress('/org/freedesktop/Notifications', bus_name='org.freedesktop.Notifications', interface='org.freedesktop.Notifications') notif_id = 0 def dbus_notify(title, body, timeout): global notif_id connection = open_dbus_connection(bus='SESSION')
import confuse configuration_path_env_var = "REDIRECTORY_CONFIG_DIR" configuration_template = { "deployment": confuse.Choice(["prod", "dev", "test"]), "log_level": confuse.Choice(["debug", "info", "warning", "error", "critical"]), "node_type": confuse.Choice(["management", "worker", "compiler"]), "directories": { "data": confuse.String(), "ui": confuse.String() }, "service": { "ip": confuse.String(), "port": confuse.Integer(), "metrics_port": confuse.Integer() }, "database": { "type": confuse.Choice(["sqlite", "mysql"]), "path": confuse.String(default="redirectory_sqlite.db"), "host": confuse.String(default="localhost"), "port": confuse.Integer(default=3306), "name": confuse.String(default="redirectory"), "username": confuse.String(default="user"), "password": confuse.String(default="pass") }, "hyperscan": { "domain_db": confuse.String(default="hs_compiled_domain.hsd"), "rules_db": confuse.String(default="hs_compiled_rules.hsd") },