def client(): app.config['TESTING'] = True app.config['OIDC_PUBLIC_KEY'] = PUBLIC_KEY app.config['GATEWAY_ENDPOINT_CONFIG_FILE'] = 'test.json' load_config() client = app.test_client() yield client
def test_config_not_found(): # this does not exist on purpose! bad_config_path = "app/tests/missing_config.json" with pytest.raises(FileNotFoundError): load_config(var_names=dummy_var_names, config_file=bad_config_path)
def get_app() -> FastAPI: app_settings = load_config() title = app_settings[ConfigSections.DEFAULT][DefaultSectionKeys.NAME] version = app_settings[ConfigSections.DEFAULT][DefaultSectionKeys.VERSION] api_prefix = app_settings[ConfigSections.DEFAULT][ DefaultSectionKeys.API_PREFIX] conn_string = app_settings[ConfigSections.DEFAULT][ DefaultSectionKeys.CACHE_CONNECTION_STRING] db = DbEngine(conn_string) server = FastAPI( title=title, version=version, ) server.db = db health.router.version = server.version health.router.title = server.title health.router.db = db server.include_router(health.router, ) users.router.db = db server.include_router(users.router, #prefix=api_prefix, ) most_frequent.router.db = db server.include_router(most_frequent.router, #prefix=api_prefix, ) logger.info('****************** Starting Server *****************') return server
def main(): environment: str = os.getenv("ENVIRONMENT", "dev") config: Dict = load_config(environment) initialize_logger(level=config['logging']['level'], filename=config['logging']['filename']) redis_host = config['redis']['host'] redis_port = config['redis']['port'] logger.debug(f"Connecting to redis at {redis_host}:{redis_port}") redis_client: Redis = Redis( host=redis_host, port=redis_port, db=0 ) pubsub: PubSub = redis_client.pubsub(ignore_subscribe_messages=True) try: say(redis_client, "here we go!") drive(redis_client) while cycle([True]): logger.debug("Reading distance") distance = get_distance(pubsub) logger.debug(f"Distance is {distance}") if distance < 40.0: logger.debug("Too close to an obstacle. Turning away.") stop_motors(redis_client) say(redis_client, "Can't go that way!") turn_randomly(redis_client) drive(redis_client) sleep(0.01) except Exception as e: logger.exception(f"Something bad happened: {str(e)}") finally: logger.debug("Cleaning up") stop_motors(redis_client) pubsub.close() redis_client.close() logger.debug("Shutting down")
def main(): app_dir = Path(__file__).parent.parent config = load_config(app_dir) logger.info("started") foo(config)
def test_dummy_config_file(): config = load_config(var_names=dummy_var_names, config_file=dummy_config) assert(config['dummyhost'] == 'dummyConfHost') assert(config['dummyuser'] == 'dummyConfUser') assert(config['dummypass'] == 'dummyConfPass')
def __init__(self): self.engine = create_engine(load_config().SQLALCHEMY_DATABASE_URI, convert_unicode=True, encoding='utf-8', pool_size=64, max_overflow=0, pool_recycle=30, pool_timeout=10) self.session = self.create_scoped_session()
async def test_catch_all(client): with open(app.config['GATEWAY_ENDPOINT_CONFIG_FILE'], 'w') as f: json.dump( { "": { "endpoint": "http://localhost/api", "processor": "app.tests.test_config.DummyProcessor", "auth": "app.tests.test_config.DummyAuht" } }, f) load_config() rv = await client.get( urljoin(app.config['SERVICE_PREFIX'], 'something/interesting?p=nothing')) assert rv.status_code == 200 assert (await rv.get_data()) == b'something/interesting' assert rv.headers.get('X-DummyAuth') == 'ok'
def test_add_custom_section(self): key = 'test' value = '1' def test_section(c): c[key] = {key: value} CONFIGS_INITS.append(test_section) config = load_config() self.assertEqual(value, config[key][key])
async def test_regex_config(client): with open(app.config['GATEWAY_ENDPOINT_CONFIG_FILE'], 'w') as f: json.dump( { "obj(ect)?/[\d-]+/": { "endpoint": "http://localhost/api", "processor": "app.tests.test_config.DummyProcessor" } }, f) load_config() rv = await client.get( urljoin(app.config['SERVICE_PREFIX'], 'obj/23-3/issues?p=nothing')) assert rv.status_code == 200 assert (await rv.get_data()) == b'issues' rv = await client.get( urljoin(app.config['SERVICE_PREFIX'], 'object/-/issues/4?p=nothing')) assert rv.status_code == 200 assert (await rv.get_data()) == b'issues/4' rv = await client.get( urljoin(app.config['SERVICE_PREFIX'], 'objects/-/issues/4?p=nothing')) assert rv.status_code == 404 assert json.loads(await rv.get_data()) == { "error": "No processor found for this path" } rv = await client.get( urljoin(app.config['SERVICE_PREFIX'], 'obj/2a/issues/4?p=nothing')) assert rv.status_code == 404 assert json.loads(await rv.get_data()) == { "error": "No processor found for this path" }
def test_environment_config(): os.environ['DUMMYHOST'] = 'dummyEnvHost' os.environ['DUMMYUSER'] = '******' os.environ['DUMMYPASS'] = '******' config = load_config(var_names=dummy_var_names, config_file=dummy_config) assert(config['dummyhost'] == 'dummyEnvHost') assert(config['dummyuser'] == 'dummyEnvUser') assert(config['dummypass'] == 'dummyEnvPass') del os.environ['DUMMYHOST'] del os.environ['DUMMYUSER'] del os.environ['DUMMYPASS']
def test_config_all_defaults(): setup_logging.cache_clear() log = setup_logging("main", logfile=None, nocolor=True) config = load_config(ini="") assert config.agencyName == "Search and Rescue" assert config.logo == Path("radiolog_logo.jpg") assert config.timeoutMinutes == 30 assert len(config.tabGroups) == 0 assert config.clueReport == Path("clueReportFillable.pdf") assert config.datum == Datum.WGS84 assert config.coordFormat == CoordFormat.UTM7 assert config.firstWorkingDir == Path("testdata") assert config.secondWorkingDir == None
def create_app(): app = Flask(__name__) app.config.from_object(load_config()) db.init_app(app) for module, url_prefix in DEFAULT_MODULES: app.register_blueprint(module, url_prefix=url_prefix) app.simple_cache = RedisApi(host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT'], password=app.config['REDIS_PASSWORD'], db=app.config['REDIS_DB']) hook_init(app) hook_error_handler(app) app.response_class = MyResponse return app
def main(): environment: str = os.getenv("ENVIRONMENT", "dev") config: Dict = load_config(environment) initialize_logger(level=config['logging']['level'], filename=config['logging']['filename']) redis_host = config['redis']['host'] redis_port = config['redis']['port'] logger.debug(f"Connecting to redis at {redis_host}:{redis_port}") redis_client: Redis = Redis(host=redis_host, port=redis_port, db=0) pubsub: PubSub = redis_client.pubsub(ignore_subscribe_messages=True) try: pubsub.subscribe("subsystem.listener.recording", ignore_subscribe_messages=True) handle_on_wake(redis_client, pubsub, config['wake_word']) except Exception as e: logger.exception(f"Something bad happened: {str(e)}") finally: logger.debug("Cleaning up") pubsub.close() redis_client.close() logger.debug("Shutting down")
def test_config_bad(capsys): ini = """ [mapping] datum = XXX coordformat = XXX """ sys.stderr.write("==START==\n") setup_logging.cache_clear() log = setup_logging("main", logfile=None, nocolor=True) config = load_config(ini=ini) sys.stderr.write("==END==") captured = capsys.readouterr() assert captured.out == "" assert ( captured.err == """==START== INFO Operating system is Windows. INFO PowerShell.exe is in the path. WARNING The configuration setting of [mapping]datum = XXX is invalid. Possible values are: WGS84, NAD27 WARNING The configuration setting of [mapping]coordformat = XXX is invalid. Possible values are: UTM7, UTM5, DEG, DEGMIN, DMS, DEGLIST ==END==""" )
def test_config_all_good(): ini = """ [agency] name = International Rescue logo = thunderbirds.png [mapping] datum = NAD27 coordformat = DMS [display] timeoutminutes = 20 [tabgroups] Numbers = ^Thunderbird [A-Z]+ [reports] cluereport = fabclue.pdf [storage] firstworkingdir = C:\\ir secondworkingdir = E:\\fab """ setup_logging.cache_clear() log = setup_logging("main", logfile=None, nocolor=True) config = load_config(ini=ini) assert config.agencyName == "International Rescue" assert config.logo == Path("thunderbirds.png") assert config.timeoutMinutes == 20 assert len(config.tabGroups) == 1 assert config.tabGroups[0] == "^Thunderbird [A-Z]+" assert config.clueReport == Path("fabclue.pdf") assert config.datum == Datum.NAD27 assert config.coordFormat == CoordFormat.DMS assert config.firstWorkingDir == Path("C:\\ir") assert config.secondWorkingDir == Path("E:\\fab")
# We need a bunch of Flask stuff from flask import Flask from flask import render_template from flask import redirect from flask import request from flask import json from flask import url_for from flask import g from flask import session from flask import jsonify from flask import send_from_directory from flask.ext.login import LoginManager # Some Local Stuff we made from model_files.models import * ###################################################### # SETUP ###################################################### # Set up the Flask app app = Flask(__name__, static_url_path='/static') login_manager = LoginManager() login_manager.init_app(app) login_manager.login_view = 'user_login' from app.config import load_config cfg = load_config('app/config.yaml')
from flask import Flask from app.config import Config, load_config config: Config = load_config() app = Flask(__name__)
def test_env_var_load(self): key = 'CACHE_CONNECTION_STRING' value = 'test' self.add_test_env_var(ConfigSections.DEFAULT, key, value) config = load_config() self.assertEqual(value, config[ConfigSections.DEFAULT][key])
def setUp(self): self.config = load_config()
def run(self): # read configuration files config_root = self.ENV.get("CONFIG_ROOT", "../config") config_schemas = config.load_config(config_root + "/schemas.yml", env=self.ENV) config_services = config.load_config(config_root + "/services.yml", env=self.ENV) agent_admin_url = self.ENV.get("AGENT_ADMIN_URL") if not agent_admin_url: raise RuntimeError( "Error AGENT_ADMIN_URL is not specified, can't connect to Agent." ) app_config["AGENT_ADMIN_URL"] = agent_admin_url # get public DID from our agent response = requests.get( agent_admin_url + "/wallet/did/public", headers=ADMIN_REQUEST_HEADERS, ) result = response.json() did = result["result"] logging.LOGGER.info("Fetched DID from agent: %s", did) app_config["DID"] = did["did"] # determine pre-registered schemas and cred defs existing_schemas = agent_schemas_cred_defs(agent_admin_url) # register schemas and credential definitions for schema in config_schemas: schema_name = schema["name"] schema_version = schema["version"] schema_key = schema_name + "::" + schema_version if schema_key not in existing_schemas: schema_attrs = [] schema_descs = {} if isinstance(schema["attributes"], dict): # each element is a dict for attr, desc in schema["attributes"].items(): schema_attrs.append(attr) schema_descs[attr] = desc else: # assume it's an array for attr in schema["attributes"]: schema_attrs.append(attr) # register our schema(s) and credential definition(s) schema_request = { "schema_name": schema_name, "schema_version": schema_version, "attributes": schema_attrs, } response = agent_post_with_retry( agent_admin_url + "/schemas", json.dumps(schema_request), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() schema_id = response.json() else: schema_id = { "schema_id": existing_schemas[schema_key]["schema"]["id"] } app_config["schemas"]["SCHEMA_" + schema_name] = schema app_config["schemas"]["SCHEMA_" + schema_name + "_" + schema_version] = schema_id["schema_id"] logging.LOGGER.info("Registered schema: %s", schema_id) if (schema_key not in existing_schemas or "cred_def" not in existing_schemas[schema_key]): cred_def_request = {"schema_id": schema_id["schema_id"]} response = agent_post_with_retry( agent_admin_url + "/credential-definitions", json.dumps(cred_def_request), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() credential_definition_id = response.json() else: credential_definition_id = { "credential_definition_id": existing_schemas[schema_key]["cred_def"]["id"] } app_config["schemas"]["CRED_DEF_" + schema_name + "_" + schema_version] = credential_definition_id[ "credential_definition_id"] logging.LOGGER.info("Registered credential definition: %s", credential_definition_id) # what is the TOB connection name? tob_connection_params = config_services["verifiers"]["bctob"] # check if we have a TOB connection response = requests.get( agent_admin_url + "/connections?alias=" + tob_connection_params["alias"], headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() connections = response.json()["results"] tob_connection = None for connection in connections: # check for TOB connection if connection["alias"] == tob_connection_params["alias"]: tob_connection = connection if not tob_connection: # if no tob connection then establish one tob_agent_admin_url = tob_connection_params["connection"][ "agent_admin_url"] if not tob_agent_admin_url: raise RuntimeError( "Error TOB_AGENT_ADMIN_URL is not specified, can't establish a TOB connection." ) response = requests.post( tob_agent_admin_url + "/connections/create-invitation", headers=TOB_REQUEST_HEADERS, ) response.raise_for_status() invitation = response.json() response = requests.post( agent_admin_url + "/connections/receive-invitation?alias=" + tob_connection_params["alias"], json.dumps(invitation["invitation"]), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() tob_connection = response.json() logging.LOGGER.info("Established tob connection: %s", json.dumps(tob_connection)) time.sleep(5) app_config["TOB_CONNECTION"] = tob_connection["connection_id"] synced[tob_connection["connection_id"]] = False for issuer_name, issuer_info in config_services["issuers"].items(): # register ourselves (issuer, schema(s), cred def(s)) with TOB issuer_config = { "name": issuer_name, "did": app_config["DID"], "config_root": config_root, } issuer_config.update(issuer_info) issuer_spec = config.assemble_issuer_spec(issuer_config) credential_types = [] for credential_type in issuer_info["credential_types"]: schema_name = credential_type["schema"] schema_info = app_config["schemas"]["SCHEMA_" + schema_name] ctype_config = { "schema_name": schema_name, "schema_version": schema_info["version"], "issuer_url": issuer_config["url"], "config_root": config_root, "credential_def_id": app_config["schemas"]["CRED_DEF_" + schema_name + "_" + schema_info["version"]], } credential_type['attributes'] = schema_info["attributes"] ctype_config.update(credential_type) ctype = config.assemble_credential_type_spec( ctype_config, schema_info.get("attributes")) if ctype is not None: credential_types.append(ctype) issuer_request = { "connection_id": app_config["TOB_CONNECTION"], "issuer_registration": { "credential_types": credential_types, "issuer": issuer_spec, }, } response = requests.post( agent_admin_url + "/issuer_registration/send", json.dumps(issuer_request), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() response.json() print("Registered issuer: ", issuer_name) synced[tob_connection["connection_id"]] = True print("Connection {} is synchronized".format(tob_connection))
import sqlite3 from app.config import load_config db_config = load_config().db with sqlite3.connect(db_config.db_path) as conn: cur = conn.cursor() cur.execute(""" UPDATE user_karma SET karma = karma + 49; """)
#!/usr/bin/env python3 # coding=utf-8 # author: Xiguang Liu<*****@*****.**> # 2017-11-18 11:58 from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager import os from app.config import load_config config = load_config() if not os.path.exists(config.SESSION_SECRET_KEY_FILE): with open(config.SESSION_SECRET_KEY_FILE, 'wb') as f: f.write(os.urandom(24)) app = Flask('oss') app.config.from_object(config) with open(config.SESSION_SECRET_KEY_FILE, 'rb') as f: # 多个 app 需要共享一个 secret_key 从而达到共享 session 信息 app.secret_key = f.read() db = SQLAlchemy(app) login_manager = LoginManager(app) # import views from app.apis import verify, download, upload, logic_operation
class DBInterface: # global database variables. Access these in code segments either # as cls.db, cls.cursor, etc, OR as DBInterface.db, DBInterface.cursor, etc. DATABASE_NAME = "SofAISurvey" config = load_config() db = pymysql.connect(host=config['hostname'], user=config['username'], password=config['password']) cursor = db.cursor() @classmethod def createMainTable(cls): """Creates the main table for the database. The table will store entries like this: Responder Q1 Q2 Q3 Person1 5 2 “I love it” Person2 4 1 “it’s great” """ cls.cursor.execute("USE {}".format(cls.DATABASE_NAME)) cls.cursor.execute(""" CREATE TABLE survey (responder INT NOT NULL AUTO_INCREMENT PRIMARY KEY) """) cls.cursor.execute("SHOW COLUMNS FROM survey") print(cls.cursor.fetchall()) # db.commit() @classmethod def writeToCsv(cls, csv_file, form) -> None: """ Appends the given survey result to the given csv file. ARGS: - csv_file - string, path to the csv file. - form - dictionary, intended to be survey results coming out of flask.request.form. """ responses = [] for question_number in range(1, NUM_QUESTIONS + 1): if 'q' + str(question_number) in form: responses.append(form['q' + str(question_number)]) else: # if someone doesn't respond to a question, this will make sure it # gets recorded as a NaN responses.append('') current_date = datetime.now().strftime("%Y-%m-%d-%H-%M") responses.append(current_date) # write all responses to the textfile separated by commas with open(csv_file, "a") as fp: fp.write(','.join(responses)) fp.write('\n') @classmethod def writeToDB(cls, RespUser, RespType, RespInt, RespText, RespDate): # get the sql version of the database cls.cursor.execute("select version()") sql_version = cls.cursor.fetchone() print("SQL Version: {}".format(sql_version)) # gets the tables. This was empty until a table was created. cls.cursor.execute("""use {}""".format(cls.DATABASE_NAME)) cls.cursor.execute("""show tables""") all_tables = cls.cursor.fetchall() print("Tables within the {} database: {}".format( cls.DATABASE_NAME, all_tables)) # db.commit() # add a question with the label "1" to the table named survey1 insert_query = "INSERT INTO survey2 (Responder, ResponseType, ResponseInt, ResponseText, ResponseDate) \ VALUES (%s,%s,%s,%s,%s)" # , (RespUser, RespType, RespInt, RespText, RespDate) cls.cursor.execute(insert_query, (RespUser, RespType, RespInt, RespText, RespDate)) # cls.db.commit() # Clean up the table #cursor.execute("DELETE FROM survey2") # db.commit() select_all_query = "SELECT * FROM survey2" cls.cursor.execute(select_all_query) data = cls.cursor.fetchall() print('Output on 25th March:', data)
def run(self): # read configuration files config_root = self.ENV.get("CONFIG_ROOT", "../config/local") config_schemas = config.load_config(config_root + "/schemas.yml", env=self.ENV) config_services = config.load_config(config_root + "/services.yml", env=self.ENV) app_config["config_root"] = config_root app_config["config_services"] = config_services agent_admin_url = self.ENV.get("AGENT_ADMIN_URL") if not agent_admin_url: raise RuntimeError( "Error AGENT_ADMIN_URL is not specified, can't connect to Agent." ) app_config["AGENT_ADMIN_URL"] = agent_admin_url # get public DID from our agent response = requests.get( agent_admin_url + "/wallet/did/public", headers=ADMIN_REQUEST_HEADERS, ) result = response.json() did = result["result"] logging.LOGGER.info("Fetched DID from agent: %s", did) app_config["DID"] = did["did"] # determine pre-registered schemas and cred defs existing_schemas = agent_schemas_cred_defs(agent_admin_url) # register schemas and credential definitions for schema in config_schemas: schema_name = schema["name"] schema_version = schema["version"] schema_key = schema_name + "::" + schema_version if schema_key not in existing_schemas: schema_attrs = [] schema_descs = {} if isinstance(schema["attributes"], dict): # each element is a dict for attr, desc in schema["attributes"].items(): schema_attrs.append(attr) schema_descs[attr] = desc else: # assume it's an array for attr in schema["attributes"]: schema_attrs.append(attr) # register our schema(s) and credential definition(s) schema_request = { "schema_name": schema_name, "schema_version": schema_version, "attributes": schema_attrs, } response = agent_post_with_retry( agent_admin_url + "/schemas", json.dumps(schema_request), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() schema_id = response.json() else: schema_id = { "schema_id": existing_schemas[schema_key]["schema"]["id"] } app_config["schemas"]["SCHEMA_" + schema_name] = schema app_config["schemas"]["SCHEMA_" + schema_name + "_" + schema_version] = schema_id["schema_id"] logging.LOGGER.info("Registered schema: %s", schema_id) if (schema_key not in existing_schemas or "cred_def" not in existing_schemas[schema_key]): cred_def_request = {"schema_id": schema_id["schema_id"]} response = agent_post_with_retry( agent_admin_url + "/credential-definitions", json.dumps(cred_def_request), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() credential_definition_id = response.json() else: credential_definition_id = { "credential_definition_id": existing_schemas[schema_key]["cred_def"]["id"] } app_config["schemas"]["CRED_DEF_" + schema_name + "_" + schema_version] = credential_definition_id[ "credential_definition_id"] logging.LOGGER.info("Registered credential definition: %s", credential_definition_id) # what is the TOB connection name? tob_connection_params = config_services["verifiers"]["bctob"] # check if we have a TOB connection response = requests.get( agent_admin_url + "/connections?alias=" + tob_connection_params["alias"], headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() connections = response.json()["results"] tob_connection = None for connection in connections: # check for TOB connection if connection["alias"] == tob_connection_params["alias"]: tob_connection = connection if not tob_connection: # if no tob connection then establish one (if we can) # (agent_admin_url is provided if we can directly ask the TOB agent for an invitation, # ... otherwise the invitation has to be provided manually through the admin api # ... WITH THE CORRECT ALIAS) if ("agent_admin_url" in tob_connection_params["connection"] and tob_connection_params["connection"]["agent_admin_url"]): tob_agent_admin_url = tob_connection_params["connection"][ "agent_admin_url"] response = requests.post( tob_agent_admin_url + "/connections/create-invitation", headers=TOB_REQUEST_HEADERS, ) response.raise_for_status() invitation = response.json() response = requests.post( agent_admin_url + "/connections/receive-invitation?alias=" + tob_connection_params["alias"], json.dumps(invitation["invitation"]), headers=ADMIN_REQUEST_HEADERS, ) response.raise_for_status() tob_connection = response.json() logging.LOGGER.info("Established tob connection: %s", json.dumps(tob_connection)) time.sleep(5) # if we have a connection to the TOB agent, we can register our issuer if tob_connection: register_issuer_with_orgbook(tob_connection["connection_id"]) else: print( "No TOB connection found or established, awaiting invitation to connect to TOB ..." )
from app.config import load_config from app.models.db.db import generate_schemas if __name__ == "__main__": generate_schemas(load_config().db)
def test_config_attrs(self): """Assert all attributes match those in config file""" x = load_config('logging') self.assertEqual(LogConfig.log_level, x['log_level']) self.assertEqual(LogConfig.log_location, x['log_location'])
def test_config_attrs(self): """Assert all attributes match those in config file""" x = load_config('mongo_db') self.assertEqual(DBConfig.mongo_server, x['mongo_server']) self.assertEqual(DBConfig.mongo_collection, x['mongo_collection']) self.assertEqual(DBConfig.mongo_database, x['mongo_database'])
def test_config_attrs(self): """Assert all attributes match those in config file""" x = load_config('youtube_dl') self.assertEqual(YDLConfig.video_format, x['video_format']) self.assertEqual(YDLConfig.outtmpl, x['outtmpl']) self.assertEqual(YDLConfig.video_dir, x['video_dir'])
from pathlib import Path from app.config import load_config load_config(Path(__file__).parent.parent / "config_dist")