def __LoadBuilderConfiguration(self): ## Create JSON configuration parser builder = ConfigBuilder() ## Add the base required fields self.ValidateFieldValues(builder, self.BaseRequiredFields) ## Add the extra required fields self.ValidateFieldValues(builder, self.ExtraRequiredFields) ## Create configuration variable configuration = {} ## Attempt to get configuration from file if not path.exists(self.FilePath): raise FileNotFoundError("Configuration file not found: %s" % self.FilePath) try: ## Parse the configuration configuration = builder.parse_config(self.FilePath) except AssertionError as ass: print("Invalid field: %s" % ass) configuration = None ## Return loaded configuration return configuration
def load_config(parm): builder = ConfigBuilder() config = builder.parse_config(CONFIG_PATH) for i in config.keys(): if parm == i.split('.')[-1]: record = config.get(i) break return record
def load_config(parm): builder = ConfigBuilder() global config_parm config = builder.parse_config(CONFIG_PATH) for i in config.keys(): if parm == i.split('.')[-1]: config_parm = config.get(i) break return config_parm
def __init__(self, config: str): """Parse the content of the config file""" config_path = Path(".").parent.resolve() / config builder = ConfigBuilder() self.config = builder.parse_config(str(config_path)) self.driver = build_driver(self.config) self.wg_gesucht_filter_urls = self.config.wggesucht.urls self.telegram_sender = SenderTelegram(self.config.telegram.bot_token, self.config.telegram.receiver_ids) self.telegram_receiver = ReceiverTelegram(self.config.telegram.bot_token, self.config.telegram.receiver_ids) self.wg_gesucht_crawler = WGGesuchtCrawler()
def save_config(**kwargs): builder = ConfigBuilder() config = builder.parse_config(CONFIG_PATH) for k in kwargs.keys(): if kwargs[k] != None: for i in config.keys(): if k in i: config.update(i, kwargs[k]) for i in config.keys(): print(i.split('.')[-1]) with open(CONFIG_PATH, "w") as f: f.write(config.to_json())
def test_json_schema_validation(path, schema_path): builder = ConfigBuilder() builder.validate_with_schema(schema_path) builder.parse_config(path) with open(path, "r") as config_file: config = json.load(config_file) del config["cache"] builder.parse_config(config) config["server"]["port"] = 1023 with pytest.raises(ValidationError): builder.parse_config(config)
def __init__(self, conf='agents.json'): # import json_utils from python_json_config import ConfigBuilder from saai.saai_conf import saai_conf # self.endpoint = EndpointConfig("http://localhost:5055/webhook") self.conf = f"{saai_conf.runtime_dir}/conf/{conf}" # conf_data=json_utils.read_json_file(self.conf) builder = ConfigBuilder() self.config = builder.parse_config(self.conf) # bot_locs={'genesis': '/pi/ws/sagas-ai/bots/genesis'} # self.bot_locs = conf_data['bot_locs'] # self.config_file=conf_data['config_file'] self.templates_dir = f'{saai_conf.runtime_dir}/templates' self.ruleset_files = '/pi/stack/conf/ruleset_*.json'
def test_get_request_ok(self, mock_get): try: config_json = '{"type": "http","path": "http://someurl.com/success.zip","unzip": true}' conf = ConfigBuilder().parse_config(config_json) reader = ReaderFactory.create(conf) reader.read() except Exception as e: self.fail("Exception should not have occurred")
def test_parse(path): config = ConfigBuilder()\ .validate_field_type("server.port", int)\ .validate_field_value("server.debug_mode", lambda x: not x)\ .transform_field_value("server.port", lambda x: str(x))\ .parse_config(path) assert config is not None assert not config.server.debug_mode assert config.server.port == "5000"
def __init__(self, config_url): self.config_url = config_url self.builder = ConfigBuilder() self.io = DirectConsoleIo() #add the required fields self.builder.set_field_access_required() self.builder.add_required_field('logging') self.builder.add_required_field('logging.writers') self.builder.add_required_field('database') self.builder.add_required_fields('database.db_url') self.builder.add_required_field('ticker_data_source') self.builder.add_required_field('ticker_data_source.source') self.builder.add_required_fields('plugins')
def loadConfiguration(config_path=None): if config_path is None: config_path = os.path.expanduser("~/.config/kb/kb.conf") builder = ConfigBuilder() builder.couch_url = DEFAULT_CONFIG["couch_url"] builder.database = DEFAULT_CONFIG["database"] builder.entitybase = DEFAULT_CONFIG["entitybase"] builder.username = DEFAULT_CONFIG["username"] builder.password = DEFAULT_CONFIG["password"] config = builder.parse_config(config_path) return config
def test_value_transformation(path): builder = ConfigBuilder() config = builder.parse_config(path) assert not config.server.debug_mode assert config.server.port == 5000 builder.transform_field_value("server.debug_mode", lambda x: not x) builder.transform_field_value("server.port", lambda x: x + 4) config = builder.parse_config(path) assert config.server.debug_mode assert config.server.port == 5004
def test_required_access(path): builder = ConfigBuilder() builder.set_field_access_required() builder.add_optional_field("server.nokey") builder.add_optional_fields(["cache.name", "test"]) config = builder.parse_config(path) assert config.test is None with pytest.raises(AttributeError): config.nokey assert config.server.nokey is None with pytest.raises(AttributeError): config.server.nokey2 assert config.cache.name is None with pytest.raises(AttributeError): config.cache.name2
def __init__(self): """ On initialization of Coffee Machine class the config is loaded and updates the class variables """ self.config = ConfigBuilder().parse_config("coffeeMachineConfig.json") self.coffeeMachine = self.config.machine self.slots = self.coffeeMachine.outlets.count_n self.ingredients = json.loads( self.coffeeMachine.total_items_quantity.to_json()) self.beverages = self.coffeeMachine.beverages self.availability = [0 for x in range(self.slots)]
def load_config(path: str): # create config parser builder = ConfigBuilder() # parse config try: config = builder.parse_config(path) config.merge_with_env_variables(["KEYMASTER"]) # TODO: Validate config # networks.name.rpc must be a uri # networks.name.bank must be a hexkey # networks.name.threshold must be in gwei # homes.name must be present in networks # homes.name.replicas must be present in networks # homes.name.addresses must be unique (?) return config.to_dict() except json.decoder.JSONDecodeError: # Failed to load config return False
def test_type_validation_builder(): builder = ConfigBuilder() builder.validate_field_type("key1", int) builder.validate_field_type("key1.key2", list) type_validation_dict = builder._ConfigBuilder__validation_types assert type_validation_dict["key1"] == int assert type_validation_dict["key1.key2"] == list
def main(): configs = ConfigBuilder().parse_config('configs.json') updater = Updater(configs.TelegramToken, use_context=True) bot = CTelegramBot(configs=configs, googleApi=CGoogleAPI( os.path.join(os.path.dirname(__file__), 'google-token.json'))) bot.bind(updater.dispatcher) updater.dispatcher.add_error_handler(error) updater.start_polling() updater.idle()
def test_value_validation_error_messages(path): builder = ConfigBuilder() custom_message = "test error" builder.validate_field_value("server.debug_mode", lambda x: (x, custom_message)) expected_message = f'Error validating field "server.debug_mode" with value "False": {custom_message}' with pytest.raises(AssertionError, match=expected_message): builder.parse_config(path)
def test_value_validation_builder(): builder = ConfigBuilder() builder.validate_field_value("key1", lambda x: x < 4) builder.validate_field_value("key1.key2", lambda x: len(x) == 3) value_validation_dict = builder._ConfigBuilder__validation_functions assert value_validation_dict["key1"][0](1) assert not value_validation_dict["key1"][0](4) assert value_validation_dict["key1.key2"][0]([1, 2, 3]) assert not value_validation_dict["key1.key2"][0](["a", "b"])
def test_value_transformation_builder(): builder = ConfigBuilder() builder.transform_field_value("key1", lambda x: x * 4) builder.transform_field_value( "key1.key2", lambda x: [x[len(x) - 1 - i] for i in range(len(x))]) value_transformation_dict = builder._ConfigBuilder__transformation_functions assert value_transformation_dict["key1"](1) == 4 assert value_transformation_dict["key1"](4) == 16 assert value_transformation_dict["key1.key2"]([1 == 2, 3]), [3, 2, 1] assert value_transformation_dict["key1.key2"](["a" == "b", False]), [False, "b", "a"]
def test_merge_env_variable(): prefix1 = "PYTHONJSONCONFIG" prefixes2 = ["PREFIX1", "PREFIX2"] builder = ConfigBuilder() builder.merge_with_env_variables(prefix1) assert builder._ConfigBuilder__environment_variable_prefixes == [prefix1] builder.merge_with_env_variables(prefixes2) assert builder._ConfigBuilder__environment_variable_prefixes == [ prefix1 ] + prefixes2
def refillIngredients(self, ingredient="hot_water", quantity=0, reloadConfig=False): """ Function can be used to fill in ingredients. Option1: Loads a single ingredient using ingredient and quantity Option2: Load ingredients via dictionary ingredient. Option3: Load ingredients by reloading the configuration. """ if reloadConfig: configPartial = ConfigBuilder().parse_config( "coffeeMachineConfig.json") self.ingredients = json.loads( configPartial.machine.total_items_quantity.to_json()) return if type(ingredient) == dict: for key, val in ingredient.items(): self.ingredients[key] += val self.ingredients[ingredient] = quantity
def test_optional_validation(path): builder = ConfigBuilder() builder.set_field_access_optional() builder.validate_field_type("cache.name", str) builder.validate_field_value("cache.host", "localhost") builder.transform_field_value("cache.host", lambda name: f"https://{name}") builder.parse_config(path)
from python_json_config import ConfigBuilder builder = ConfigBuilder() config = builder.parse_config('json_format.json') access = list(config) k = 0 for i in access: c = i.split('.')[1] a = "print" + "(" + "config." + i + ")" print(i.split('.')[0] + " " + c + "=") exec(a) print('\n')
from python_json_config import ConfigBuilder # create config_util parser builder = ConfigBuilder() builder.set_field_access_required() # parse config_util config = builder.parse_config('config.json')
def test_value_validation(path): builder = ConfigBuilder() builder.validate_field_value("server.debug_mode", lambda x: not x) builder.validate_field_value("server.port", [lambda x: x < 10000, lambda x: x > 1023]) builder.parse_config(path) builder.validate_field_value("server.port", lambda x: x > 6000) with pytest.raises(AssertionError): builder.parse_config(path) builder = ConfigBuilder() builder = builder.validate_field_value("cache.ttl", lambda x: x > 200) with pytest.raises(AssertionError): builder.parse_config(path)
def test_type_validation(path): builder = ConfigBuilder() builder.validate_field_type("server.debug_mode", bool) builder.validate_field_type("server.port", int) builder.parse_config(path) builder.validate_field_type("server.host", int) with pytest.raises(AssertionError): builder.parse_config(path)
from influxdb import InfluxDBClient from python_json_config import ConfigBuilder builder = ConfigBuilder() config = builder.parse_config('config.json') devices = config.devices if config.environment == "production": config = config.production else: config = config.develop client = InfluxDBClient('herbert', 8086, 'root', 'root', 'tasmotaToInflux') def getDatabases(): rs = client.query("show databases") names = [] for n in rs['databases']: name = n['name'] if name != '_internal': names.append(name) #getSeries(name) return names def getSeries(db, m): rs = client.query("SHOW SERIES ON " + db + " FROM " + m) for m in rs.get_points(): print(m.sp)
def main(): # create config parser builder = ConfigBuilder() # parse configuration from file parameters.json config = builder.parse_config('parameters_3d.json') # Load parameters for environment dimensions = config.dimensions base_length = config.lamp.base_length base_width = config.lamp.base_width base_slope = config.lamp.base_slope road_start = config.road.start road_end = config.road.end road_width = config.road.width road_depth = config.road.depth road_sections = config.road.sections configuration = config.lamp.configuration # Load parameters for evaluation criterion = config.evaluation.criterion cosine_error = config.evaluation.cosine_error reflective_factor = config.evaluation.reflective_factor # Init environment env = Environment(dimensions, base_length, base_width, base_slope, road_start, road_end, road_width, road_depth, road_sections, criterion, cosine_error, reflective_factor, configuration) # Load parameters for LED sqrt_of_number_of_rays = config.lamp.sqrt_of_number_of_rays ray_distribution = config.lamp.ray_distribution # Load limits for two connected reflective surfaces angle_lower_bound = config.lamp.two_connected.angle_lower_bound angle_upper_bound = config.lamp.two_connected.angle_upper_bound length_lower_bound = config.lamp.two_connected.length_lower_bound length_upper_bound = config.lamp.two_connected.length_upper_bound # Load limits for multiple free reflective surfaces no_of_reflective_segments = config.lamp.multiple_free.no_of_reflective_segments distance_limit = config.lamp.multiple_free.distance_limit length_limit = config.lamp.multiple_free.length_limit population_size = config.evolution.population_size number_of_generations = config.evolution.number_of_generations # Load parameters for evolution operators = config.evolution.operators xover_prob = operators.xover_prob angle_mut_prob = operators.mutation.angle_mutation_prob length_mut_prob = operators.mutation.length_mutation_prob shift_segment_prob = operators.mutation.segment_shift_prob rotate_segment_prob = operators.mutation.segment_rotation_prob resize_segment_prob = operators.mutation.segment_resizing_prob # Run evolution algorithm evolution(env, sqrt_of_number_of_rays, ray_distribution, angle_lower_bound, angle_upper_bound, length_lower_bound, length_upper_bound, no_of_reflective_segments, distance_limit, length_limit, population_size, number_of_generations, xover_prob, angle_mut_prob, length_mut_prob, shift_segment_prob, rotate_segment_prob, resize_segment_prob)
def test_build_from_serialized_config(path): builder = ConfigBuilder() config = builder.parse_config(path) assert config == builder.parse_config(config.to_dict()) assert config == builder.parse_config(config.to_json())