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)
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)
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_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 __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 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 __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_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, 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_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 loadConfigFromFile(path) -> 'Config': def makeElement(x: int, y: int, c: typing.TypeVar(T), grid_size: int): return c(Point(x, y), grid_size) builder = ConfigBuilder() config = builder.parse_config(path) builder.set_field_access_required() builder.add_required_field(GRID_CONFIG) builder.add_required_field(MAX_ITER) builder.add_required_field(DEFAULT_CONFIG) builder.add_required_field(DISINTEGRATE_PROB_CONFIG) builder.validate_field_type(GRID_CONFIG, int) builder.validate_field_type(MAX_ITER, int) builder.validate_field_type(DEFAULT_CONFIG, str) builder.validate_field_type(HOLES_CONFIG, [(int, int)]) builder.validate_field_type(SUBSTRATES_CONFIG, [(int, int)]) builder.validate_field_type(CATALYSTS_CONFIG, [(int, int)]) builder.validate_field_type(LINKS_CONFIG, [(int, int)]) builder.validate_field_type(DISINTEGRATE_PROB_CONFIG, float) # set up grid_size = config.grid_size default_element: T = STR_TO_CLASS[config.default] iter = config.max_iter disint_prob = config.disintegration_probability h_plist = [ makeElement(x, y, Hole, grid_size) for x, y in list(ast.literal_eval(config.Holes)) ] if config.Holes else [] s_plist = [ makeElement(x, y, Substrate, grid_size) for x, y in list(ast.literal_eval(config.Substrates)) ] if config.Substrates else [] k_plist = [ makeElement(x, y, Catalyst, grid_size) for x, y in list(ast.literal_eval(config.Catalysts)) ] if config.Catalysts else [] l_plist = [ makeElement(x, y, Link, grid_size) for x, y in list(ast.literal_eval(config.Links)) ] if config.Links else [] return Config(grid_size, default_element, iter, h_plist, s_plist, k_plist, l_plist, disint_prob)
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 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_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())
class Configuration: config_url = None io = None builder = None config = None is_valid = None 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 load_configuration(self): try: if not self.is_config_url_valid(): self.configuration_error("Configuration file not found") return False self.config = self.builder.parse_config(self.config_url) if not self.validate_configuration(): return False else: self.io.info("Configuration loaded") return True except Exception as e: self.configuration_error(e) def is_config_url_valid(self): try: return path.exists(self.config_url) except Exception as e: self.configuration_error(e) def configuration_error(self, e): self.is_valid = False self.io.error("Loading configuration file.") self.io.error(e) def get_logging_config(self): return self.config.logging.writers def get_plugin_config(self): return self.config.plugins def get_plugin_args(self): return self.config.args def validate_configuration(self): try: self.config.logging self.config.logging.writers self.config.database self.config.database.db_url self.config.plugins return True except Exception as e: self.io.error("Error validating configuration file ") self.io.error(e) def get_plugin_args_by_name(self, name): if name in self.get_plugin_args().to_dict().keys(): args = [] index = list(self.get_plugin_args().to_dict().keys()).index(name) self.io.debug("index {}".format(index)) vals = list(self.get_plugin_args().to_dict().values()) self.io.debug(vals[index]) return vals[index] else: self.io.error( "plugin configuration not found for : {}".format(name)) return -1
from flask import Flask, request import re import os import socket import struct import datetime from nameko.standalone.rpc import ClusterRpcProxy from flask_httpauth import HTTPBasicAuth from python_json_config import ConfigBuilder from passlib.hash import pbkdf2_sha256 from validation import check_rule, remove_error_rule auth = HTTPBasicAuth() builder = ConfigBuilder() apiProfile = builder.parse_config('etc/users.json') CONFIG = {'AMQP_URI': "amqp://*****:*****@localhost:5672"} intent_archive = "intent.txt" app = Flask(__name__) def cidr_to_netmask(cidr): network, net_bits = cidr.split('/') hots_bits = 32 - int(net_bits) netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << hots_bits))) return network, netmask def get_line(word): with open(intent_archive) as archive:
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 main(): # create config parser builder = ConfigBuilder() # parse configuration from file parameters.json config = builder.parse_config('parameters.json') # Load parameters for environment base_length = config.lamp.base_length base_slope = config.lamp.base_slope road_start = config.road.start road_end = config.road.end 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 reflections_timeout = config.evaluation.reflections_timeout weights = config.evaluation.weights number_of_leds = config.lamp.number_of_LEDs separating_distance = config.lamp.separating_distance modification = config.lamp.modification invalid_parameters = check_parameters_environment( road_start, road_end, road_depth, road_sections, criterion, cosine_error, reflective_factor, configuration, number_of_leds, separating_distance, modification, weights, reflections_timeout) if invalid_parameters: print(f"Invalid value for parameters {invalid_parameters}") return else: print(f" Environment parameters: ok") # Init environment env = Environment(road_start, road_end, road_depth, road_sections, criterion, cosine_error, reflective_factor, configuration, number_of_leds, separating_distance, modification, weights, reflections_timeout) # Load parameters for LED number_of_rays = config.lamp.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 base_angle_limit_min = config.lamp.multiple_free.base_angle_limit_min base_angle_limit_max = config.lamp.multiple_free.base_angle_limit_max 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 tilt_base_prob = operators.mutation.tilt_base_prob invalid_parameters = check_parameters_evolution( 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, tilt_base_prob, base_length, base_slope, base_angle_limit_min, base_angle_limit_max) if invalid_parameters: print(f"Invalid value for parameters {invalid_parameters}") return else: print(f" Evolution parameters: ok") # Run evolution algorithm evolution(env, 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, tilt_base_prob, base_length, base_slope, base_angle_limit_min, base_angle_limit_max)
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')
def utcoffset(self, dt): return timedelta(0) def plot_goal(goal, achievements): logger.info(f"Plotting current goal:{goal.get('goal_title')}") #sort by datetime #sort by # create config parser builder = ConfigBuilder() # parse config config = builder.parse_config( '/Users/daviddawson/Documents/projects/new_years_resolutions/2020.json') current_year = 2020 # access elements goals = config.get("goals") achievements = config.get("achievements") goals_df = pd.DataFrame(goals) achievements_df = pd.DataFrame(achievements) # For Each Goal Build a curve of values. for goal in goals: logger.debug( f"Working on goal ({goal.get('id')}): {goal.get('goal_title')}") temp_df = pd.DataFrame() for achievement in achievements:
import sys import logging.handlers import tasks from python_json_config import ConfigBuilder # define path to config file config_file = '.\\' + sys.argv[1:][0] # create config parser builder = ConfigBuilder() # parse config if tasks.check_file_exists(config_file): config = builder.parse_config(config_file) # create logger logger = logging.getLogger(config.site_name) logger.setLevel(logging.DEBUG) # create file handler which logs messages fh = logging.handlers.RotatingFileHandler(config.logpath + config.site_name + '.log', maxBytes=10500000, backupCount=5) #fh = logging.FileHandler(config.logpath + config.site_name + '.log') fh.setLevel(logging.DEBUG) # create console handler ch = logging.StreamHandler() ch.setLevel(logging.INFO)
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)
import speech_recognition as sr import pyttsx3 import datetime import wikipedia import webbrowser import time from python_json_config import ConfigBuilder # create config parser builder = ConfigBuilder() config = builder.parse_config('config.json') lang_file = 0 language = config.assistant.language print(language) if language == "de": lang_file = builder.parse_config('lang_de.json') elif language == "en": lang_file = builder.parse_config('lang.json') else: lang_file = builder.parse_config('lang.json') engine = pyttsx3.init('sapi5') voices = engine.getProperty('voices') engine.setProperty('voice', 'voices[0].id') if language == "de": engine.setProperty( 'voice', 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_DE-DE_ZIRA_11.0' )
import sys import os from python_json_config import ConfigBuilder from influxdb import InfluxDBClient sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) builder = ConfigBuilder() configFull = builder.parse_config('config.json') config = configFull.develop if configFull.environment == 'production': config = configFull.production broker = config.mqtt.broker influx = config.influx.database InfluxClient = InfluxDBClient(influx.ip, influx.port, influx.user, influx.passwort, influx.db) from .core import Core