def _perform_actions(self): for tl_id, statuses in self._tls.items(): remaining_time = (traci.trafficlight.getNextSwitch(tl_id) - traci.simulation.getCurrentTime()) / 1000.0 msg = { 'tl_id': tl_id, 'remaining_time': remaining_time, } phase_index = traci.trafficlight.getPhase(tl_id) if phase_index != self._tls[tl_id]['current_phase']: self._tls[tl_id]['current_phase'] = phase_index status = self._tls[tl_id].get(phase_index, None) if status == 'many': print('Many vehicles, increasing this TL state duration!') delta_time = remaining_time * (Config().multipliers_many - 1.0) elif status == 'few' and remaining_time >= 6: print('Few vehicles, decreasing this TL state duration!') delta_time = remaining_time * (Config().multipliers_few - 1.0) elif status == 'none': print('No vehicles, decreasing this TL state duration!') delta_time = remaining_time * (Config().multipliers_none - 1.0) else: print( 'Regular number of vehicles, not changing anything...') delta_time = 0.0 traci.trafficlight.setPhaseDuration( tl_id, round(remaining_time + delta_time)) msg['delta_time'] = delta_time Producer.publish(msg, Config().kafka_topic_tl_times)
def main( input, config_file, debug, ): """ Main entry point for testing_oneclick_cli. :param input: Something :param config_file: Path to the yaml config file. :param debug: Debug flag :return Something """ if config_file: with open(config_file, encoding='utf-8') as f: config_contents = yaml.safe_load(f) config = Config(**config_contents) else: config = Config() print(input) if debug: with open('debug.yaml', 'w') as f: yaml.dump(config.dict(), f)
def init(cls): print("Initiating consumer...") try: cls.consumer = KafkaConsumer(bootstrap_servers=Config().kafka_endpoint, value_deserializer=lambda m: json.loads(m.decode('utf-8')), group_id=None, consumer_timeout_ms=100) cls.consumer.subscribe([Config().kafka_topic_tl_status]) except RuntimeError: sys.exit(Fore.RED + "Connection to Kafka failed!" + Fore.RESET)
def apply_car_counter(cls): """ syncs the value of the carCounter to the SUMO simulation """ while len(CarRegistry.cars) < Config().sumo_total_cars: cls.car_index_counter += 1 c = Car("car-" + str(CarRegistry.car_index_counter)) cls.cars[c.id] = c c.add_to_simulation(0) while len(CarRegistry.cars) > Config().sumo_total_cars: # to many cars -> remove cars (k, v) = CarRegistry.cars.popitem() v.remove()
def test_load_cfg(): path = 'test_data\\config.yaml' cfg = Config(path) assert cfg.lastfm.token == 'sometoken' with pytest.raises(SystemExit): path = 'test_data\\nonexisting.yaml' cfg = Config(path) with pytest.raises(TypeError): path = 'test_data\\config_type_error.yaml' cfg = Config(path)
def test__fetch_selection_config_emptyDict_returnsDefaultSettings(): sel_config = {} config = Config() config._fetch_selection_config(sel_config) assert config.selection_method == "roulette" and config.agents_to_save == 0.0
def test_validate_sp_for_branch(mocker): # Mock schema fetch get_schema_from_url_mock = mocker.patch( "lib.system_profile_validate._get_schema_from_url") mock_schema = system_profile_specification() get_schema_from_url_mock.return_value = mock_schema # Mock Kafka consumer fake_consumer = mocker.Mock() config = Config(RuntimeEnvironment.SERVICE) tp = TopicPartition(config.host_ingress_topic, 0) fake_consumer.poll.return_value = { tp: [ SimpleNamespace( value=json.dumps(wrap_message(minimal_host().data()))) for _ in range(5) ] } fake_consumer.offsets_for_times.return_value = { tp: SimpleNamespace(offset=0) } validation_results = validate_sp_for_branch(config, fake_consumer, repo_fork="test_repo", repo_branch="test_branch", days=3) assert "test_repo/test_branch" in validation_results for reporter in validation_results["test_repo/test_branch"]: assert validation_results["test_repo/test_branch"][ reporter].pass_count > 0
def test_config_default_settings(self): expected_base_url = "/r/insights/platform/inventory" expected_api_path = f"{expected_base_url}/api/v1" expected_mgmt_url_path_prefix = "/" with test.support.EnvironmentVarGuard() as env: # Make sure the environment variables are not set for env_var in ( "INVENTORY_DB_USER", "INVENTORY_DB_PASS", "INVENTORY_DB_HOST", "INVENTORY_DB_NAME", "INVENTORY_DB_POOL_TIMEOUT", "INVENTORY_DB_POOL_SIZE", "APP_NAME", "PATH_PREFIX" "INVENTORY_MANAGEMENT_URL_PATH_PREFIX", ): env.unset(env_var) conf = Config("testing") self.assertEqual( conf.db_uri, "postgresql://*****:*****@localhost/test_db") self.assertEqual(conf.api_url_path_prefix, expected_api_path) self.assertEqual(conf.mgmt_url_path_prefix, expected_mgmt_url_path_prefix) self.assertEqual(conf.db_pool_timeout, 5) self.assertEqual(conf.db_pool_size, 5)
def test__fetch_selection_config_validData_fetchesValidVariables(): sel_config = {"method": "roulette", "agents_to_save": 1.0} config = Config() config._fetch_selection_config(sel_config) assert config.selection_method == "roulette" and config.agents_to_save == 1.0
def test_config(monkeypatch): app_name = "brontocrane" path_prefix = "/r/slaterock/platform" expected_base_url = f"{path_prefix}/{app_name}" expected_api_path = f"{expected_base_url}/api/v1" expected_mgmt_url_path_prefix = "/mgmt_testing" with monkeypatch.context() as m: m.setenv("INVENTORY_DB_USER", "fredflintstone") m.setenv("INVENTORY_DB_PASS", "bedrock1234") m.setenv("INVENTORY_DB_HOST", "localhost") m.setenv("INVENTORY_DB_NAME", "SlateRockAndGravel") m.setenv("INVENTORY_DB_POOL_TIMEOUT", "3") m.setenv("INVENTORY_DB_POOL_SIZE", "8") m.setenv("APP_NAME", app_name) m.setenv("PATH_PREFIX", path_prefix) m.setenv("INVENTORY_MANAGEMENT_URL_PATH_PREFIX", expected_mgmt_url_path_prefix) conf = Config("testing") assert conf.db_uri == "postgresql://*****:*****@localhost/SlateRockAndGravel" assert conf.db_pool_timeout == 3 assert conf.db_pool_size == 8 assert conf.api_url_path_prefix == expected_api_path assert conf.mgmt_url_path_prefix == expected_mgmt_url_path_prefix
def create_app(config: Config): app = FastAPI(openapi_url="/openapi/spec.json", docs_url="/swagger", redoc_url="/redoc", debug=True) app.include_router(router) # wire things up app.config = Config() app.session = SessionWrapper(s=Session()) app.db = DB(app.config.DB_DSN.get_secret_value()) app.repositories = lambda: None app.repositories.zipcodes = ZipCodeRepository(db=app.db) app.shutdown = lambda: __import__('os').kill(app.config.PID, __import__('signal').SIGTERM) @app.on_event("startup") async def setup() -> None: logging.info(f"[APP_SETUP] {app} {app.config}") app.loop = asyncio.get_running_loop() await app.db.setup(app) await app.session.s.setup() @app.on_event("shutdown") async def teardown() -> None: logging.info(f"[APP_TEARDOWN] {app} {app.config}") await app.session.s.teardown() await app.db.teardown() return app
def config() -> Config: config = Config() config.timedelta = 1 config.start_date = date(2001, 1, 1) config.end_date = date(2001, 1, 30) config.validations = [(date(2000, 1, 1), date(2001, 1, 1))] return config
def run(input_root, simulation_period): paths.build(input_root) amf_dict = amf_obs_time_series(BASE_AMF_DICT, complete_days_only=True, return_low_err=True) # get_etrm_time_series(paths.amf_extract, dict_=amf_dict) for k, v in amf_dict.iteritems(): for kk, vv in v.iteritems(): if isinstance(vv, DataFrame): p = os.path.join(paths.amf_output_root, '{}_{}.csv'.format(k, kk)) print 'writing to {}'.format(p) vv.to_csv(p) val = amf_dict.values()[0] cfg = Config() for runspec in cfg.runspecs: paths.build(runspec.input_root, runspec.output_root) etrm = Processes(runspec) etrm.configure_run(runspec) etrm.run() save_run(etrm, val)
def __init__(self, token=None, proxies=None, rate_limit=None, error_retries=None, cfg=Config()): if not token: token = cfg.lastfm.token if not proxies: proxies = {} if not rate_limit: rate_limit = cfg.lastfm.rate_limit if not error_retries: error_retries = cfg.lastfm.error_retries # Setup logging self.cfg = cfg self.logger = cfg.get_logger() self.token = token self.headers = {'User-Agent': 'MusicBro2/alpha'} self.proxies = proxies self.error_retries = error_retries self.rate_limit = rate_limit self.request_time = 0
def __init__(self, app: BaseApplication): self._config = Config() self._config._config_path = os.path.expanduser('~/.coc_automator.json') self._device = None # type: Device self._event = dict( config=lambda: self._config, set_preview=lambda: self.signal_set_preview, refresh_scenes=lambda: self.signal_refresh_scene, ) super().__init__(app) app_shell = get_app_shell() self._adb = PyADB('%s/app/res/libs/adb' % app_shell.get_runtime_dir()) self._automator = COCAutomator(self._event) self._automator.load('app/res/data.dat') self.signal_set_preview.connect(self.set_preview) self.signal_refresh_scene.connect(self.refresh_scenes) if self._config.load(): devices = self._adb.devices if len(devices) == 0: self._adb.kill_server() self._adb.start_server() devices = self._adb.devices self._device = devices.get(self._config.device) self._automator.set_device(self._device)
def validate_schema(repo_fork="RedHatInsights", repo_branch="master", days=1, max_messages=10000): # Use the identity header to make sure the user is someone from our team. config = Config(RuntimeEnvironment.SERVICE) identity = get_current_identity() if not hasattr(identity, "user") or identity.user.get("username") not in config.sp_authorized_users: flask.abort(403, "This endpoint is restricted to HBI Admins.") consumer = KafkaConsumer( bootstrap_servers=config.bootstrap_servers, api_version=(0, 10, 1), value_deserializer=lambda m: m.decode(), **config.validator_kafka_consumer, ) try: response = validate_sp_for_branch( consumer, {config.host_ingress_topic, config.additional_validation_topic}, repo_fork, repo_branch, days, max_messages, ) consumer.close() return flask_json_response(response) except (ValueError, AttributeError) as e: consumer.close() flask.abort(400, str(e))
def test_failed_send(self): config = Config(config_file='test_config.yml') config.set_fake_api_call(True, 400) ms = MessageSender(config) ms.WAIT_FACTOR = 0.01 with self.assertRaises(FatalError): ms.send_alert(1, 'text')
def test_send_wrong_alert_level(self): config = Config(config_file='test_config.yml') config.set_fake_api_call(True, 202) ms = MessageSender(config) ms.WAIT_FACTOR = 0.01 alert_level = 3 with self.assertRaises(KeyError): # alert level 3 does not exist in our config config.alert_text['level_%s' % alert_level] text = 'strong rains coming' responses = ms.send_alert(alert_level, text) expected = { 'config_errors': [(202, { 'src': '+41791234566', 'text': 'faloppia.opengis.ch - IT Problem, No alarm numbers set for level 2 alarms', 'dst': '+41791234568' }), (202, { 'src': '+41791234566', 'text': 'faloppia.opengis.ch - IT Problem, No alarm numbers or text set for level 3 alarms', 'dst': '+41791234568' })], 'main': (202, { 'src': '+41791234566', 'text': 'faloppia.opengis.ch - ALLARME, strong rains coming', 'dst': '+41791234567<+41791234568' }) } print(responses) self.assertDictEqual(responses, expected)
def test_validate_sp_for_missing_branch_or_repo(api_post, mocker): # Mock schema fetch get_schema_from_url_mock = mocker.patch( "lib.system_profile_validate._get_schema_from_url") get_schema_from_url_mock.side_effect = ValueError( "Schema not found at URL!") # Mock Kafka consumer fake_consumer = mocker.Mock() config = Config(RuntimeEnvironment.SERVICE) tp = TopicPartition(config.host_ingress_topic, 0) fake_consumer.poll.return_value = { tp: [ SimpleNamespace( value=json.dumps(wrap_message(minimal_host().data()))) for _ in range(5) ] } fake_consumer.offsets_for_times.return_value = { tp: SimpleNamespace(offset=0) } with pytest.raises(expected_exception=ValueError) as excinfo: validate_sp_for_branch(config, fake_consumer, repo_fork="foo", repo_branch="bar", days=3) assert "Schema not found at URL" in str(excinfo.value)
def dist_daily_taw_run(): cfg2 = Config() cfg2.load() etrm2 = Processes(cfg2) etrm2.set_save_dates(cfg2.save_dates) etrm2.modify_taw(cfg2.taw_modification) etrm2.run()
def start(): sumo_binary = checkBinary('sumo') traci.start([ sumo_binary, "-c", Config().sumo_config_file, "--no-step-log", "true", "--no-warnings", "true" ])
def test_fetch_broker_config(mocker: MockFixture): app_config = Config(env=DEVELOPMENT, api_url='http://localhost:3000', api_email='test', api_password='******', broker_uri='test.xyz', broker_port=1234) token = 'test-token' login_response = mocker.Mock() login_response.json.return_value = {'token': token} mocker.patch('requests.post', return_value=login_response) broker_user = '******' broker_password = '******' broker_response = mocker.Mock() broker_response.json.return_value = { 'user': broker_user, 'password': broker_password } mocker.patch("requests.get", return_value=broker_response) broker_config = fetch_broker_config(app_config) assert broker_config.uri == app_config.broker_uri assert broker_config.port == app_config.broker_port assert broker_config.user == broker_user assert broker_config.password == broker_password
def cli(config: str): config = Config(config_path=config) aws_auth_headers = get_aws_auth_headers( sagemaker_config=config.sagemaker_config) if post_request(headers=aws_auth_headers, sagemaker_config=config.sagemaker_config): format_input_data(sagemaker_config=config.sagemaker_config, vegeta_config=config.vegeta_config) write_target_list(headers=aws_auth_headers, config=config) vegeta_helper = VegetaHelper(config.vegeta_config) vegeta_helper.run_load_test() if config.vegeta_config.format == "html": vegeta_helper.plot() if config.vegeta_config.open: vegeta_helper.open_browser() else: vegeta_helper.write_report()
def test__fetch_mutation_config_validData_fetchesValidVariables(): mutation_config = {"method": "gene_creation", "rate": 0.1} config = Config() config._fetch_mutation_config(mutation_config) assert config.mutation_method == "gene_creation" and config.mutation_rate == 0.1
def test_config_default_settings(monkeypatch): expected_base_url = "/r/insights/platform/inventory" expected_api_path = f"{expected_base_url}/api/v1" expected_mgmt_url_path_prefix = "/" with monkeypatch.context() as m: # Make sure the environment variables are not set for env_var in ( "INVENTORY_DB_USER", "INVENTORY_DB_PASS", "INVENTORY_DB_HOST", "INVENTORY_DB_NAME", "INVENTORY_DB_POOL_TIMEOUT", "INVENTORY_DB_POOL_SIZE", "APP_NAME", "PATH_PREFIX" "INVENTORY_MANAGEMENT_URL_PATH_PREFIX", ): if env_var in os.environ: m.delenv(env_var) conf = Config("testing") assert conf.db_uri == "postgresql://*****:*****@localhost/test_db" assert conf.api_url_path_prefix == expected_api_path assert conf.mgmt_url_path_prefix == expected_mgmt_url_path_prefix assert conf.db_pool_timeout == 5 assert conf.db_pool_size == 5
def setUp(self): txt = '''--- input_root: /Volumes/Seagate Expansion Drive output_root: Please Set Me start_date: 12/1/2013 end_date: 12/31/2013 mask: masks/please_set_me.tif polygons: Blank_Geo save_dates: [] daily_outputs: - tot_infil - tot_etrs - tot_eta - tot_precip - tot_kcb ro_reinf_frac: 0.0 swb_mode: fao allen_ceff: 1.0 winter_evap_limiter: 0.3 ''' path = StringIO(txt) self._cfg = Config(path)
def test_configuration_with_env_vars(self): app_name = "brontocrane" path_prefix = "/r/slaterock/platform" expected_base_url = f"{path_prefix}/{app_name}" expected_api_path = f"{expected_base_url}/api/v1" expected_mgmt_url_path_prefix = "/mgmt_testing" with test.support.EnvironmentVarGuard() as env: env.unset(SHARED_SECRET_ENV_VAR) env.set("INVENTORY_DB_USER", "fredflintstone") env.set("INVENTORY_DB_PASS", "bedrock1234") env.set("INVENTORY_DB_HOST", "localhost") env.set("INVENTORY_DB_NAME", "SlateRockAndGravel") env.set("INVENTORY_DB_POOL_TIMEOUT", "3") env.set("INVENTORY_DB_POOL_SIZE", "8") env.set("APP_NAME", app_name) env.set("PATH_PREFIX", path_prefix) env.set("INVENTORY_MANAGEMENT_URL_PATH_PREFIX", expected_mgmt_url_path_prefix) conf = Config("testing") self.assertEqual( conf.db_uri, "postgresql://*****:*****@localhost/SlateRockAndGravel" ) self.assertEqual(conf.db_pool_timeout, 3) self.assertEqual(conf.db_pool_size, 8) self.assertEqual(conf.api_url_path_prefix, expected_api_path) self.assertEqual(conf.mgmt_url_path_prefix, expected_mgmt_url_path_prefix)
def __init__(self, args): Log.append('app_init', 'Info', 'version: %s' % Const.version) set_default(default_zone=Zone(home_dir=pyinstaller.get_runtime_dir())) self.qt = QApplication(args) self.qt.setApplicationName(Const.app_name) self.qt.setWindowIcon( QIcon('%s/app/res/icon.png' % pyinstaller.get_runtime_dir())) self.hook_exception() self.config = Config() self.config.load() self.lang = None # type: English self.load_language(Config.language) self.events = { 'process_events': self.qt.processEvents, 'export_log': self.export_log, 'check_update': self.check_update, 'load_language': self.load_language, 'get_language': lambda: self.lang, 'set_language': self.set_language, 'get_config': lambda: self.config, }
def test_configuration_with_env_vars(self): app_name = "brontocrane" path_prefix = "r/slaterock/platform" expected_base_url = f"/{path_prefix}/{app_name}" expected_api_path = f"{expected_base_url}/v1" expected_mgmt_url_path_prefix = "/mgmt_testing" new_env = {"INVENTORY_DB_USER": "******", "INVENTORY_DB_PASS": "******", "INVENTORY_DB_HOST": "localhost", "INVENTORY_DB_NAME": "SlateRockAndGravel", "INVENTORY_DB_POOL_TIMEOUT": "3", "INVENTORY_DB_POOL_SIZE": "8", "APP_NAME": app_name, "PATH_PREFIX": path_prefix, "INVENTORY_MANAGEMENT_URL_PATH_PREFIX": expected_mgmt_url_path_prefix, } with set_environment(new_env): conf = Config() self.assertEqual(conf.db_uri, "postgresql://*****:*****@localhost/SlateRockAndGravel") self.assertEqual(conf.db_pool_timeout, 3) self.assertEqual(conf.db_pool_size, 8) self.assertEqual(conf.api_url_path_prefix, expected_api_path) self.assertEqual(conf.mgmt_url_path_prefix, expected_mgmt_url_path_prefix)
def test__fetch_mutation_config_emptyDict_returnsDefaultSettings(): mutation_config = {} config = Config() config._fetch_mutation_config(mutation_config) assert config.mutation_method == "normalization" and config.mutation_rate == 0.0