PK!ېkkmanageconf/__init__.py__version__ = "1.0.1" import ast import json import os import anyconfig import boto3 from manageconf.exceptions import ( BotoRequestFailureError, RemoteConfigurationJSONDecodeError, ) class RemoteSettings: """Methods to fetch and transform remote settings""" def _deserialise(self, name, value): """Deserialise JSON values to Python Args: name (str): the config key name value (str): the config value Returns: value: deserialised config value """ if isinstance(value, str): try: return json.loads(value) except json.JSONDecodeError: raise RemoteConfigurationJSONDecodeError( f"value of item {name} is not valid JSON" ) else: return value def _evaluate(self, name, value): """Literal evaluation of a string containing a Python expression Args: name (str): the config key name value (str): the config value Returns: value: evaluated config value """ if isinstance(value, str): try: return ast.literal_eval(value) except Exception: return value else: return value def get_remote_params(self, parameters_path): """Fetches remote config from AWS Systems Manager Param Store Args: parameters_path (str): the path of the params to fetch /{project_name}/{stage}/ Returns: dict: remote config """ client = boto3.client("ssm") response = {} try: payload = client.get_parameters_by_path( Path=parameters_path, Recursive=True, WithDecryption=True ) remote_params = payload.get("Parameters", []) for param in remote_params: name = param.get("Name", None) name = name.split("/")[-1] value = param.get("Value", None) deserialised_value = self._deserialise(name, value) evaluated_value = self._evaluate(name, deserialised_value) response[name] = evaluated_value return response except Exception as ex: raise BotoRequestFailureError from ex(f"Boto response failed: {ex}") class Config: """Merges in config objects to make one conf object""" conf = {} @classmethod def create_remote_settings_class(cls): setting_class = RemoteSettings return setting_class() @classmethod def make(cls): """Makes the conf object, merging in the following order: - ENV - default config: default.json - stage config: {stage}.json - remote config: remote_settings """ anyconfig.merge(cls.conf, os.environ.copy()) stage = cls.conf.get("stage", None) project_config_dir = cls.conf.get("project_config_dir", ".") project_default_config_file_path = os.path.join( project_config_dir, "default.json" ) if os.path.exists(project_default_config_file_path): anyconfig.merge(cls.conf, anyconfig.load(project_default_config_file_path)) project_stage_config_file_path = os.path.join( project_config_dir, f"{stage}.json" ) if os.path.exists(project_stage_config_file_path): anyconfig.merge(cls.conf, anyconfig.load(project_stage_config_file_path)) remote_settings = cls.conf.get("local_or_remote_settings", None) if remote_settings: try: if remote_settings.lower() == "remote": project_name = cls.conf.get("project_name", None) parameters_path = f"/{project_name}/{stage}/" remote_settings_class = cls.create_remote_settings_class() remote_conf = remote_settings_class.get_remote_params( parameters_path ) anyconfig.merge(cls.conf, remote_conf) except (AttributeError, TypeError): raise AssertionError("Input config variables should be strings") def get_config(key_name, default=None): return Config.conf.get(key_name, default) def make_settings(): Config.make() make_settings() PK!Tmanageconf/exceptions.pyclass BotoRequestFailureError(Exception): """Raised when a Boto request to SSM fails""" class RemoteConfigurationJSONDecodeError(Exception): """Raised when a Parameter Store item fails to be deserialised""" PK!T++"manageconf-1.0.1.dist-info/LICENSEMIT License Copyright (c) 2019 Sam Atkins Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTU manageconf-1.0.1.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!HE B#manageconf-1.0.1.dist-info/METADATAWn6S[$[kФ P -6IHJQ E'9d˞;,s ay-^ mCw 2U>a 鎿2˸ibi)/"0F$ry%3[| 3#Dx*,F 1Ő`Ip5\ύ\4\zpMj>{2( afma J;+~N&Tv.c8b~tb¦RsJje 1vt<#Dec}J*uG)+k)0Ã=p+96IAgpTZUCl+&ZM asOK,HisGmilG;oux?#XYGj;.i`Hwsu•(|=&Q;VE"zymk.Q9cVpLfSq|V-v;t!}SM5v4~lLAoC<W8]p_fS=D:!/ofR < F/w笼mC"P}Le =%eBDF~BE%UiÑRchcD'ulxK /Sx۵Ah<̝ ߸VERZ/olQcGmx/(r~)d+E}t}n>` ׈ƣ{Cтч|[rHpfA"Yhia,&X&tAF`fLt䜤*v-ʢVXILJKOHSN-@Ĉ."$?> nyKhҾ˟UU5m%٪3ZŸi*";}ͳ"Z`Zz!Dk{CR:9}r=IԈ{[;߮ F#]ڏSUvB-\jل._O{x.ª.rʿsSo-꿩\j; ԟFZe];C6$cUbKrvfsƳQ.7M޷AsvڍkiC*( uoUNeeDin}spx%rhJojt*6]Gcm}ik&`$] !L ~ztXuoJX KZ[}pkLxx&.q_{dE>(53xbÃVld7ݥMA 3meKތAi'g]"HnSU.^ot-ph LxPC+gKl~җKOPK!Hxa-!manageconf-1.0.1.dist-info/RECORD}90~0a!.,"F ;~*X3K