configirl
The MIT License (MIT)
Copyright 2022 Sanhe Hu <https://github.com/MacHu-GWU/configirl-project>
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.
This is a python config management tool to manage config parameter in centralized place. The purpose of this tool is to avoid maintain complex config/paramater handling logic in shell script, cloudformation, terraform and any other devops tools. Instead, we manage that in Python.
Since Python is a full featured general programming language and it is available on any Mac / Linux machine.
It allows different DevOps tools to easily talk to each other via JSON.
This library implemented in pure Python with no dependencies.
-
configirl.
strip_comments
(string, comment_symbols=frozenset({'//', '#'}))[source] Strip comments from json string.
Parameters: - string – A string containing json with comments started by comment_symbols.
- comment_symbols – Iterable of symbols that start a line comment (default # or //).
Returns: The string with the comments removed.
-
configirl.
add_metaclass
(metaclass)[source] Class decorator for creating a class with a metaclass.
This method is copied from six.py
-
exception
configirl.
ValueNotSetError
[source] Raises when trying to get value of a field that have not set value before.
-
exception
configirl.
DerivableSetValueError
[source] Raises when trying to set value for Derivable Field.
-
class
configirl.
Field
(default=<NOTHING>, dont_dump=False, printable=True, cache=False)[source] Base class for config value field.
Parameters: - default (typing.Any) – default value for this field. can be a callable function.
- dont_dump (bool) – if true, then you can’t get the value if
check_dont_dump = True
inBaseConfigClass.to_dict()
andBaseConfigClass.to_json()
. this prevent from writing sensitive information to file. - printable (bool) – if False, then it will not be displayed with print function. this prevent from displaying sensitive information to the console.
- cache (bool) – A flag indicates that whether the cache is enabled.
only available for
Derivable
if True, then it will cache computation expensive derived value.
-
get_value
(check_dont_dump=False, check_printable=False, **kwargs)[source] Returns the value for this field.
Parameters: Returns: CN Doc
对于 Constant Field:
- 如果: self.value = NOTHING, 同时 .set_value(…) 方法从来没有被调用过.
- 如果: self.value 不等于 NOTHING, 说明 .set_value(…) 方法被吊用过, 则
- 返回 self.value
对于 Derivable Field:
- 如果: self._getter_method() 没有成功
-
get_value_from_env
(prefix='')[source] Use config value stored in environment variables. This usually used for computation server that doesn’t come with the config file. Since config file with sensitive information may not easy to manage. A common use case is AWS Lambda Function.
Parameters: prefix – a prefix append left to the config field name. For exmaple, if the config field is PROJECT_NAME
, and the prefix isMY_PROJECT_
, then it will read value fromMY_PROJECT_PROJECT_NAME
.
-
exception
configirl.
DontDumpError
[source] Raises when trying to dump a
dont_dump=True
config value.
-
class
configirl.
Constant
(default=<NOTHING>, dont_dump=False, printable=True, cache=False)[source] Constant Value Field.
-
class
configirl.
Derivable
(default=<NOTHING>, dont_dump=False, printable=True, cache=False)[source] Derivable Value Field.
-
configirl.
is_instance_or_subclass
(val, class_)[source] Return True if
val
is either a subclass or instance ofclass_
.
-
class
configirl.
ConfigMeta
[source] Config class meta class. Collect declared
Field
, assign field name.
-
class
configirl.
BaseConfigClass
(**kwargs)[source] Config class base class.
BaseConfigClass._declared_fields
:BaseConfigClass._constant_fields
:BaseConfigClass._deriable_fields
:
-
classmethod
from_dict
(dct)[source] A factory classmethod construct config object from dict. Only loads constant field. If a key is an undefined field, it automatically been ignored.
Return type: BaseConfigClass
-
classmethod
from_json_str
(json_str)[source] A factory classmethod construct config object from json string. json string can includes comments. Only loads constant field. If a key is an undefined field, it automatically been ignored.
Return type: BaseConfigClass
-
classmethod
from_json_file
(json_file)[source] A factory classmethod construct config object from a json file. json string can includes comments. Only loads constant field. If a key is an undefined field, it automatically been ignored.
Return type: BaseConfigClass
-
classmethod
from_env_var
(prefix)[source] A factory classmethod construct config object from environment variables. Only loads constant field. If a key is an undefined field, it automatically been ignored.
Return type: BaseConfigClass
-
update
(dct)[source] Update constant config values from a dictionary. Only those fields defines as Constant value will be loaded. If a key is an undefined field, it automatically been ignored.
Return type: dict Returns: loaded data
-
update_from_raw_json_file
()[source] Update constant config values from the
BaseConfigClass.CONFIG_RAW_JSON_FILE
.Return type: dict Returns: loaded data
-
update_from_env_var
(prefix)[source] Update constant config values from environment variables.
Parameters: prefix (str) – a prefix used in all related environment variable. Return type: dict Returns: loaded data
-
to_dict
(check_dont_dump=True, check_printable=False, ignore_na=False, prefix='')[source] Dump config values to dictionary.
Parameters: - check_dont_dump (bool) – if True, then it will check if a field has
a True value
dont_dump
flag, thenDontDumpError
error is raised. - check_printable (bool) – if True, then it will check if a field has
a False value
printable
flag, then it returns HIDDEN. - ignore_na (bool) – if True, then
ValueNotSetError
error will be ignored. - prefix (str) – a prefix that appended to the left of every field
Return type: - check_dont_dump (bool) – if True, then it will check if a field has
a True value
-
to_json
(check_dont_dump=True, check_printable=False, ignore_na=False, prefix='')[source] Dump config values to json.
Parameters: - check_dont_dump (bool) – if True, then it will check if a field has
a True value
dont_dump
flag, thenDontDumpError
error is raised. - check_printable (bool) – if True, then it will check if a field has
a False value
printable
flag, then it returns HIDDEN. - ignore_na (bool) – if True, then
ValueNotSetError
error will be ignored. - prefix (str) – a prefix that appended to the left of every field
Return type: - check_dont_dump (bool) – if True, then it will check if a field has
a True value
-
classmethod
is_aws_ec2_amz_linux_runtime
()[source] Check whether it is Amazon Linux EC2 runtime.
Return type: bool
-
classmethod
is_aws_ec2_redhat_runtime
()[source] Check whether it is RedHat AWS EC2 runtime.
Return type: bool
-
classmethod
is_aws_ec2_freebsd_runtime
()[source] Check whether it is FreeBSD AWS EC2 runtime.
Return type: bool
-
classmethod
is_aws_lambda_runtime
()[source] Check whether it is Amazon Lambda Function runtime.
Ref: https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
Return type: bool
-
classmethod
is_aws_code_build_runtime
()[source] Check whether it is AWS Code Build runtime.
Ref: https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
Return type: bool
-
classmethod
is_circle_ci_runtime
()[source] Check whether it is CircleCI runtime.
Ref: https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables
Return type: bool
-
classmethod
is_travis_ci_runtime
()[source] Check whether it is TravisCI runtime.
Ref: https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
Return type: bool
-
classmethod
is_gitlab_ci_runtime
()[source] Check whether it is Gitlab CI runtime.
Ref: https://docs.gitlab.com/ee/ci/variables/
Return type: bool
-
configirl.
read_json_value
(path, field)[source] Return a value of a field from a Json file, ignoring the comments
-
configirl.
get_config_value
(module, field)[source] Initialize a Config Class defined in a python module, and get the value of a field. The module has to be able to be import.
-
configirl.
import_config_value
(sys_path, module, field)[source] Initialize a Config Class defined in a python module, and get the value of a field. The module has to be able to be import when arg
sys_path
been add to sys.path.