def initialise_settings() -> None: """Configure and validates settings. This method is called when starting the microservice to ensure all configuration settings are properly provided. Raises: :class:`~dynaconf.validator.ValidationError`: A setting is not valid. """ not_doc = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation"]) not_doc_unittest = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation", "unittest"]) settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="OEO") utils = SettingValidationUtils() settings.validators.register( Validator(SettingKeys.PROCESSES_GITHUB_URL.value, must_exist=True, condition=utils.check_processes_github_url, when=not_doc), Validator(SettingKeys.RABBIT_HOST.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PORT.value, must_exist=True, is_type_of=int, when=not_doc_unittest), Validator(SettingKeys.RABBIT_USER.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PASSWORD.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_USER.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_PASSWORD.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_HOST.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_PORT.value, must_exist=True, is_type_of=int, when=not_doc_unittest), Validator(SettingKeys.DB_NAME.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.LOG_DIR.value, must_exist=True, condition=utils.check_create_folder, when=not_doc_unittest), ) settings.validators.validate() LOGGER.info("Settings validated")
def initialise_settings() -> None: """Configure and validates settings. This method is called when starting the microservice to ensure all configuration settings are properly provided. Raises: :class:`~dynaconf.validator.ValidationError`: A setting is not valid. """ not_doc = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation"]) settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="OEO") utils = SettingValidationUtils() settings.validators.register( Validator(SettingKeys.OPENEO_VERSION.value, must_exist=True, when=not_doc), Validator(SettingKeys.SECRETE_KEY.value, must_exist=True, when=not_doc), Validator(SettingKeys.UPLOAD_TMP_DIR.value, must_exist=True, condition=utils.check_create_folder, when=not_doc), Validator(SettingKeys.RABBIT_HOST.value, must_exist=True, when=not_doc), Validator(SettingKeys.RABBIT_PORT.value, must_exist=True, is_type_of=int, when=not_doc), Validator(SettingKeys.RABBIT_USER.value, must_exist=True, when=not_doc), Validator(SettingKeys.RABBIT_PASSWORD.value, must_exist=True, when=not_doc), ) settings.validators.validate()
async def test_url_no_search(httpx_mock: HTTPXMock): settings.configure(FORCE_ENV_FOR_DYNACONF="testing") url = "http://test3_url" httpx_mock.add_response(url=url, data=b"xyz test123 abcd") with patch("producer.events_topic") as mock_fn: mock_fn.send = mock_coro() await check_single(url) actual_event = mock_fn.send.call_args[1]["value"] assert actual_event.url == url assert actual_event.http_status == 200 assert actual_event.search_results == {}
def setUp(self): settings.configure( settings_module=os.path.dirname(os.path.realpath(__file__)) + '/settings.yml') self.mock_trader = mock.create_autospec(Trader) self.mock_analyser = mock.create_autospec(Analyser) self.mock_reporter = mock.create_autospec(Reporter) self.mock_database = mock.create_autospec(Database) self.mock_helper = mock.create_autospec(Helper) self.strategy = Balance(COIN, LIQUI, BINANCE, self.mock_trader, self.mock_analyser, self.mock_reporter, self.mock_database, self.mock_helper)
def run_site(settings_file, ws_routes): settings.configure(settings_file) dictConfig(settings.LOGGING) # Get a reference to the main loop loop = asyncio.get_event_loop() # Set up the aiohttp application app = web.Application(loop=loop, debug=settings.DEBUG) app['ws_routes'] = ws_routes # Startup and shutdown callbacks for fn in [init_sockets, init_db, init_cache, init_session_mgr]: app.on_startup.append(fn) for fn in [close_sockets]: app.on_shutdown.append(fn) for fn in [close_cache, close_session_mgr, close_db]: app.on_cleanup.append(fn) # Routes app.router.add_get('/ws', websocket_listener) # Run application handler = app.make_handler() server = loop.create_server( protocol_factory=handler, host=settings.HOST, port=settings.PORT, backlog=settings.BACKLOG) loop.run_until_complete(app.startup()) srv = loop.run_until_complete(server) log.info("Running server on %s:%d", settings.HOST, settings.PORT) try: loop.run_forever() except KeyboardInterrupt: pass finally: log.info("Shutting down ...") srv.close() loop.run_until_complete(srv.wait_closed()) loop.shutdown_asyncgens() loop.run_until_complete(app.shutdown()) loop.run_until_complete(handler.shutdown(60.0)) loop.run_until_complete(app.cleanup()) loop.close()
def main(): settings.configure("aetherguild.settings") basicConfig(level=logging.ERROR) # Register our custom password hash function(s) register_hash() # Load all command files CommandLoader.load() parser = argparse.ArgumentParser(description='Aetherguild.net CLI') subparsers = parser.add_subparsers(title='Commands', help="Command help") for cls in CommandLoader.classes: command_name = str(cls.__name__).lower() sub_parser = subparsers.add_parser(command_name, help=cls.description) cls.get_args(sub_parser) sub_parser.set_defaults(func=cls.get_instance) args = parser.parse_args() # Initialize the command object command_obj = None try: command_obj = args.func() except AttributeError as e: print("Command is missing or not defined.") exit(1) loop = asyncio.get_event_loop() app = Application(loop) loop.run_until_complete(init_db(app)) loop.run_until_complete(init_cache(app)) loop.run_until_complete(command_obj.on_init()) try: loop.run_until_complete(command_obj.on_run(args, app)) except KeyboardInterrupt: pass finally: log.info("Shutting down ...") loop.shutdown_asyncgens() loop.run_until_complete(command_obj.on_close()) loop.run_until_complete(close_cache(app)) loop.run_until_complete(close_db(app)) loop.close()
def setup_database(): """ Fixture to set up the postgres database """ settings.configure(FORCE_ENV_FOR_DYNACONF="testing") db = settings.DATABASE print(f"creating the {db.dbname} database") create_db(**(dict(settings.DATABASE))) conn = psycopg2.connect(**(dict(settings.DATABASE))) conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cur = conn.cursor() initialize_db(cur) yield cur cur.close() conn.close() print(f"dropping the {db.dbname} database") destroy_db(**(dict(settings.DATABASE)))
def initialise_settings() -> None: """Configures and validates settings. As this service needs no environment variables in the Python code this is an empty wrapper to be filled in the future. Raises: :py:class:`~dynaconf.validator.ValidationError`: A setting is not valid. """ not_doc_unittest = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation", "unittest"]) settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="OEO") utils = SettingValidationUtils() settings.validators.register( Validator(SettingKeys.RABBIT_HOST.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PORT.value, must_exist=True, is_type_of=int, when=not_doc_unittest), Validator(SettingKeys.RABBIT_USER.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PASSWORD.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.LOG_DIR.value, must_exist=True, condition=utils.check_create_folder, when=not_doc_unittest), ) settings.validators.validate() LOGGER.info("Settings validated")
from __future__ import with_statement import sys from logging.config import fileConfig from alembic import context from dynaconf import settings from sqlalchemy import engine_from_config, pool from sqlalchemy.engine.url import URL sys.path.append('.') from aetherguild.backend.database import tables settings.configure("aetherguild.settings") config = context.config fileConfig(config.config_file_name) target_metadata = tables.metadata db_url = URL( drivername='postgresql+psycopg2', username=settings.DATABASE['username'], password=settings.DATABASE['password'], host=settings.DATABASE['host'], port=settings.DATABASE['port'], database=settings.DATABASE['database'], ) config.set_main_option('sqlalchemy.url', str(db_url))
from aetherguild.deprecated.hash import register_hash register_hash() from passlib.hash import aethery1, aethery2 from dynaconf import settings from passlib.context import CryptContext settings.configure('aetherguild.settings') pwd_context = CryptContext(schemes=["argon2", "aethery2", "aethery1"], deprecated="auto") print(pwd_context.hash("kek")) print(aethery1.hash("kek")) print(aethery2.hash("kek")) print( pwd_context.verify( "kek", "$aethery-sha256$MjM1N2RiNTd1YjV5YXcwbjNYRSVCTj0vNEFXVu+/vUdWQg==$dae5cf5a48160711f7aea2f1b15f1cd244d20d6d" ))
from common.msg_service.sms_send import SendMessageApi, VerifySmsCode from config import PAY_CHANNELS from marketer.config import PHONE_CODE_STATUS, AREA_STATUS_CODE, \ UNIONID_LIMIT, MAX_REQUEST_ONE_DAY, ALIPAY_VERIFY_TRANSFER_AMOUNT from marketer.model_manager import AreaModelManager from marketer.serializers.account import MarketerWithdrawSerializer from marketer.serializers.marketer import CreateMarketerBaseSerializer, \ UpdateMarketerSerializer from marketer.serializers.nested_serializers import MarketerLoginSerializer from marketer.utils.auth import MarketerRegisterAuthentication, \ MarketerAuthentication from marketer.utils.redis_utils import RedisUtil ENV = dynasettings.ENV dynasettings.configure() dynasettings.namespace('PAYUNION') app_id = dynasettings.SUBSCRIPTION_ACCOUNT_APP_ID_MARKETER if ENV != 'dev' else dynasettings.SUBSCRIPTION_ACCOUNT_APP_ID_USER app_secret = dynasettings.SUBSCRIPTION_ACCOUNT_SECRET_MARKETER if ENV != 'dev' else dynasettings.SUBSCRIPTION_ACCOUNT_SECRET_USER logger = logging.getLogger(__file__) class MarketerLoginView(views.APIView): """ 登陆 """ def post(self, request): serializer = MarketerLoginSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(status=status.HTTP_200_OK, data=dict(token=serializer.token))
def set_test_settings(): settings.configure(FORCE_ENV_FOR_DYNACONF="testing")
from __future__ import annotations from dynaconf import settings print("EXAMPLE_ prefix") settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="EXAMPLE") print(settings.VAR1) print(settings.VAR2) print("_ prefix") settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="") print(settings.VAR1) print(settings.VAR2) print("no prefix at all") settings.configure(ENVVAR_PREFIX_FOR_DYNACONF=False) print(settings.VAR1) print(settings.VAR2) # test issue 166 (renamed GLOBAL_ENV_) print("using GLOBAL_ENV_") settings.configure(GLOBAL_ENV_FOR_DYNACONF=False) print(settings.VAR1) print(settings.VAR2)
# GLOBAL ENV VARS environ["DYNACONF_HOSTNAME"] = "host.com" environ["DYNACONF_PORT"] = "@int 5000" environ["DYNACONF_ALIST"] = '@json ["item1", "item2", "item3", 123]' environ["DYNACONF_ADICT"] = '@json {"key": "value", "int": 42}' environ["DYNACONF_DEBUG"] = "@bool true" environ["DYNACONF_MUSTBEFRESH"] = "first" environ["DYNACONF_MUSTBEALWAYSFRESH"] = "first" environ["DYNACONF_SHOULDBEFRESHINCONTEXT"] = "first" environ["DYNACONF_VALUE"] = "@float 42.1" # environ['FRESH_VARS_FOR_DYNACONF'] = '@json ["MUSTBEALWAYSFRESH"]' # settings.configure() settings.configure( FRESH_VARS_FOR_DYNACONF=["MUSTBEALWAYSFRESH"], ROOT_PATH_FOR_DYNACONF=os.path.dirname(os.path.abspath(__file__)), ) SETTINGS_DATA = OrderedDict() # Numbers SETTINGS_DATA["DYNACONF_INTEGER"] = 42 SETTINGS_DATA["DYNACONF_FLOAT"] = 3.14 # Text # 'DYNACONF_STRING': Hello, SETTINGS_DATA["DYNACONF_STRING2"] = "Hello" SETTINGS_DATA["DYNACONF_STRING2_LONG"] = "Hello World!" SETTINGS_DATA["DYNACONF_BOOL"] = True SETTINGS_DATA["DYNACONF_BOOL2"] = False # Use extra quotes to force a string from other type SETTINGS_DATA["DYNACONF_STRING21"] = '"42"' SETTINGS_DATA["DYNACONF_STRING22"] = "'true'"
from chalice import Chalice from dynaconf import settings app = Chalice(app_name='chalice-dynaconf-helloworld') settings.configure(ENVVAR_PREFIX_FOR_DYNACONF=False) @app.route('/') def index(): return {'hello': settings.APP_NAME}
def initialise_settings() -> None: """Configure and validates settings. This method is called when starting the microservice to ensure all configuration settings are properly provided. Raises: :class:`~dynaconf.validator.ValidationError`: A setting is not valid. """ not_doc = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation"]) not_doc_unittest = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation", "unittest"]) settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="OEO") utils = SettingValidationUtils() settings.validators.register( Validator(SettingKeys.OPENEO_VERSION.value, must_exist=True, when=not_doc), Validator(SettingKeys.AIRFLOW_HOST.value, must_exist=True, condition=utils.check_parse_url, when=(not_doc_unittest & not_doc)), Validator(SettingKeys.AIRFLOW_OUTPUT.value, must_exist=True, when=not_doc), Validator(SettingKeys.AIRFLOW_DAGS.value, must_exist=True, condition=utils.check_create_folder, when=not_doc), Validator(SettingKeys.SYNC_DEL_DELAY.value, must_exist=True, is_type_of=int, condition=utils.check_positive_int, when=not_doc), Validator(SettingKeys.SYNC_RESULTS_FOLDER.value, must_exist=True, condition=utils.check_create_folder, when=not_doc), Validator(SettingKeys.WEKEO_STORAGE.value, default="", when=not_doc_unittest), Validator(SettingKeys.RABBIT_HOST.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PORT.value, must_exist=True, is_type_of=int, when=not_doc_unittest), Validator(SettingKeys.RABBIT_USER.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PASSWORD.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_USER.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_PASSWORD.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_HOST.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.DB_PORT.value, must_exist=True, is_type_of=int, when=not_doc_unittest), Validator(SettingKeys.DB_NAME.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.LOG_DIR.value, must_exist=True, condition=utils.check_create_folder, when=not_doc_unittest), ) settings.validators.validate() LOGGER.info("Settings validated")
import os from dynaconf.loaders.env_loader import load from dynaconf import settings # noqa os.environ['DYNACONF_HOSTNAME'] = 'host.com' os.environ['DYNACONF_PORT'] = '@int 5000' os.environ['DYNACONF_VALUE'] = '@float 42.1' os.environ['DYNACONF_ALIST'] = '@json ["item1", "item2", "item3"]' os.environ['DYNACONF_ADICT'] = '@json {"key": "value"}' os.environ['DYNACONF_DEBUG'] = '@bool true' os.environ['PROJECT1_HOSTNAME'] = 'otherhost.com' os.environ['PROJECT1_PORT'] = '@int 8080' settings.configure() def test_env_loader(): assert settings.HOSTNAME == 'host.com' def test_single_key(): load(settings, namespace='PROJECT1', key='HOSTNAME') assert settings.HOSTNAME == 'otherhost.com' assert settings.PORT == 5000
from dynaconf import settings settings.configure(settings_module="/tmp/configure_test/settings.py") assert settings.MESSAGE == "Hello from tmp" print(settings.MESSAGE) # noqa
os.environ['DYNACONF_HOSTNAME'] = 'host.com' os.environ['DYNACONF_PORT'] = '@int 5000' os.environ['DYNACONF_VALUE'] = '@float 42.1' os.environ['DYNACONF_ALIST'] = '@json ["item1", "item2", "item3", 123]' os.environ['DYNACONF_ADICT'] = '@json {"key": "value", "int": 42}' os.environ['DYNACONF_DEBUG'] = '@bool true' os.environ['PROJECT1_HOSTNAME'] = 'otherhost.com' os.environ['PROJECT1_PORT'] = '@int 8080' os.environ['DYNACONF_MUSTBEFRESH'] = 'first' os.environ['DYNACONF_MUSTBEALWAYSFRESH'] = 'first' os.environ['DYNACONF_SHOULDBEFRESHINCONTEXT'] = 'first' # os.environ['FRESH_VARS_FOR_DYNACONF'] = '@json ["MUSTBEALWAYSFRESH"]' # settings.configure() settings.configure(FRESH_VARS_FOR_DYNACONF=["MUSTBEALWAYSFRESH"]) def test_env_loader(): assert settings.HOSTNAME == 'host.com' assert settings.ALIST == ["item1", "item2", "item3", 123] assert settings.ADICT == {"key": "value", "int": 42} def test_single_key(): load(settings, namespace='PROJECT1', key='HOSTNAME') assert settings.HOSTNAME == 'otherhost.com' assert settings.PORT == 5000 def test_dotenv_loader():
def setUp(self): settings.configure( settings_module=os.path.dirname(os.path.realpath(__file__)) + '/settings.yml')
def set_test_settings(): """Set dynaconf env for testing.""" settings.configure(ENV_FOR_DYNACONF='testing')
def initialise_settings() -> None: """Configure and validates settings. This method is called when starting the microservice to ensure all configuration settings are properly provided. Raises: :class:`~dynaconf.validator.ValidationError`: A setting is not valid. """ not_doc = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation"]) not_doc_unittest = Validator("ENV_FOR_DYNACONF", is_not_in=["documentation", "unittest"]) settings.configure(ENVVAR_PREFIX_FOR_DYNACONF="OEO") utils = SettingValidationUtils() settings.validators.register( Validator(SettingKeys.CACHE_PATH.value, must_exist=True, condition=utils.check_create_folder, when=not_doc), Validator(SettingKeys.IS_CSW_SERVER.value, default=False), Validator(SettingKeys.CSW_SERVER.value, SettingKeys.DATA_ACCESS.value, SettingKeys.GROUP_PROPERTY.value, SettingKeys.WHITELIST.value, must_exist=True, when=Validator(SettingKeys.IS_CSW_SERVER.value, eq=True) & not_doc), Validator(SettingKeys.IS_CSW_SERVER_DC.value, default=False), Validator(SettingKeys.CSW_SERVER_DC.value, SettingKeys.DATA_ACCESS_DC.value, SettingKeys.GROUP_PROPERTY_DC.value, SettingKeys.WHITELIST_DC.value, must_exist=True, when=Validator(SettingKeys.IS_CSW_SERVER_DC.value, eq=True) & not_doc), Validator(SettingKeys.IS_HDA_WEKEO.value, default=False), Validator(SettingKeys.WEKEO_API_URL.value, SettingKeys.WEKEO_STORAGE.value, SettingKeys.DATA_ACCESS_WEKEO.value, SettingKeys.WHITELIST_WEKEO.value, must_exist=True, when=Validator(SettingKeys.IS_HDA_WEKEO.value, eq=True) & not_doc), Validator(SettingKeys.WEKEO_USER.value, SettingKeys.WEKEO_PASSWORD.value, must_exist=True, when=Validator(SettingKeys.IS_HDA_WEKEO.value, eq=True) & not_doc_unittest), Validator(SettingKeys.RABBIT_HOST.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PORT.value, must_exist=True, is_type_of=int, when=not_doc_unittest), Validator(SettingKeys.RABBIT_USER.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.RABBIT_PASSWORD.value, must_exist=True, when=not_doc_unittest), Validator(SettingKeys.LOG_DIR.value, must_exist=True, condition=utils.check_create_folder, when=not_doc_unittest), ) settings.validators.validate() if not (settings.IS_CSW_SERVER or settings.IS_CSW_SERVER_DC or settings.IS_HDA_WEKEO): raise Exception( "No (meta)data connector is specified. At least one of the" "following env variables must be true: OEO_IS_CSW_SERVER," " OEO_IS_CSW_SERVER_DC, OEO_IS_HDA_WEKEO.") if settings.ENV_FOR_DYNACONF != "documentation": if settings.IS_CSW_SERVER: settings.WHITELIST = settings.WHITELIST.split(",") if settings.IS_CSW_SERVER_DC: settings.WHITELIST_DC = settings.WHITELIST_DC.split(",") if settings.IS_HDA_WEKEO: settings.WHITELIST_WEKEO = settings.WHITELIST_WEKEO.split(",") LOGGER.info("Settings validated")