allconf 4.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
allconf/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ __version__ = '4.0.0'
2
+
3
+ __author__ = 'Thordur Matthiasson <doddi79@gmail.com>'
4
+ __license__ = 'MIT License'
5
+ __copyright__ = 'Copyright 2024 -Thordur Matthiasson, Copyright 2019-2024 - CCP Games ehf'
File without changes
File without changes
@@ -0,0 +1,66 @@
1
+ import argparse
2
+
3
+ from allconf import __version__ as version
4
+ from allconf.renderers import SimpleStaticRenderer
5
+ from allconf.structs.errors import *
6
+
7
+ import sys
8
+
9
+
10
+ def main():
11
+ parser = argparse.ArgumentParser(description='Renders a single static config from an AllConf formatted file and '
12
+ 'its extends, includes and other expressions.',
13
+ epilog=f'AllConf version {version}')
14
+
15
+ parser.add_argument('file', help='The AllConf formatted config file to read, parse and render')
16
+ parser.add_argument('-o', '--output', help='File to write the results to (otherwise its just printed to stdout)',
17
+ default='', nargs='?')
18
+ parser.add_argument('-f', '--force-overwrite', help='Overwrite existing output file if it exists',
19
+ action='store_true')
20
+
21
+ loudness_group = parser.add_mutually_exclusive_group()
22
+ loudness_group.add_argument('-s', '--silent', action='store_true',
23
+ help='Only outputs the resulting rendered config (and errors print to stderr)')
24
+ loudness_group.add_argument('-v', '--verbose', action="store_true",
25
+ help='Spits out DEBUG level logs')
26
+
27
+ args = parser.parse_args()
28
+
29
+ if args.verbose:
30
+ import logging
31
+ logging.basicConfig(level=logging.DEBUG)
32
+
33
+ if not args.silent:
34
+ print(f'Reading and parsing file: {args.file}...')
35
+
36
+ try:
37
+ if args.output:
38
+ if not args.silent:
39
+ print(f'Writing output to: {args.output}...')
40
+
41
+ SimpleStaticRenderer().render_static_config_to_file(input_file=args.file,
42
+ output_file=args.output,
43
+ overwrite_existing=args.force_overwrite)
44
+
45
+ else:
46
+ if not args.silent:
47
+ print(f'Printing results:')
48
+ print(f'==================================================')
49
+ print(SimpleStaticRenderer().render_static_config_from_file(args.file))
50
+
51
+ if not args.silent:
52
+ print(f'==================================================')
53
+
54
+ if not args.silent:
55
+ print(f'Done!')
56
+
57
+ except AllConfError as e:
58
+ if not args.silent:
59
+ print(f'An error occurred: {e!r}')
60
+ else:
61
+ print(f'An error occurred: {e!r}', file=sys.stderr)
62
+ sys.exit(3)
63
+
64
+
65
+ if __name__ == '__main__':
66
+ main()
File without changes
@@ -0,0 +1,74 @@
1
+ import argparse
2
+
3
+ from allconf import __version__ as version
4
+ from allconf import stubber
5
+ from allconf.structs.errors import *
6
+ import sys
7
+
8
+
9
+ def main():
10
+ parser = argparse.ArgumentParser(description='Generates Python dataclass stubs based on the given allconf type descriptor file.',
11
+ epilog=f'AllConf version {version}')
12
+
13
+ parser.add_argument('file', help='The AllConf config type descriptor file to generate strubs from.')
14
+ parser.add_argument('-o', '--output', help='File to write the generated stub code to (otherwise its just printed to stdout)',
15
+ default='', nargs='?')
16
+ parser.add_argument('-f', '--force-overwrite', help='Overwrite existing output file if it exists', action='store_true')
17
+ parser.add_argument('-n', '--class-name',
18
+ help='The name of the resulting "final" stub class generated when outputting to a file (default is "AllConfConfigStub"). Set to "None" to skip generating the "final" class.',
19
+ default='AllConfConfigStub')
20
+ parser.add_argument('-x', '--export-all', help='Make all stub class names public and export via __all__ when outputting to a file.',
21
+ action='store_true')
22
+
23
+ loudness_group = parser.add_mutually_exclusive_group()
24
+ loudness_group.add_argument('-s', '--silent', action='store_true',
25
+ help='Only outputs the resulting rendered code (and errors print to stderr)')
26
+ loudness_group.add_argument('-v', '--verbose', action="store_true",
27
+ help='Spits out DEBUG level logs')
28
+
29
+ args = parser.parse_args()
30
+
31
+ if args.verbose:
32
+ import logging
33
+ logging.basicConfig(level=logging.DEBUG)
34
+
35
+ if not args.silent:
36
+ print(f'Reading and stubbing file: {args.file}...')
37
+
38
+ cls_name = 'AllConfConfigStub' if args.class_name is None else args.class_name
39
+ if cls_name.lower().strip() == 'none':
40
+ cls_name = ''
41
+
42
+ try:
43
+ if args.output:
44
+ if not args.silent:
45
+ print(f'Writing output to: {args.output}...')
46
+
47
+ stubber.SimpleStubMaker().render_stub_classes_to_file(input_file=args.file,
48
+ output_file=args.output,
49
+ overwrite_existing=args.force_overwrite,
50
+ is_private=not args.export_all,
51
+ class_name=cls_name)
52
+ else:
53
+ if not args.silent:
54
+ print(f'Printing results:')
55
+ print(f'==================================================')
56
+
57
+ print(stubber.SimpleStubMaker().render_stub_classes_from_descriptor_file(args.file, class_name=cls_name, is_private=not args.export_all))
58
+
59
+ if not args.silent:
60
+ print(f'==================================================')
61
+
62
+ if not args.silent:
63
+ print(f'Done!')
64
+
65
+ except AllConfError as e:
66
+ if not args.silent:
67
+ print(f'An error occurred: {e!r}')
68
+ else:
69
+ print(f'An error occurred: {e!r}', file=sys.stderr)
70
+ sys.exit(3)
71
+
72
+
73
+ if __name__ == '__main__':
74
+ main()
@@ -0,0 +1,4 @@
1
+ from .interface import *
2
+ from .jsonfile import *
3
+ from .yamlfile import *
4
+ from .autoloader import *
@@ -0,0 +1,93 @@
1
+ __all__ = [
2
+ 'autoload',
3
+ 'guess_loader_class',
4
+ 'raw_load',
5
+ 'render_load',
6
+ ]
7
+
8
+ import os
9
+
10
+ from allconf.structs import *
11
+ from .interface import *
12
+ from .jsonfile import *
13
+ from .yamlfile import *
14
+
15
+ _EXTENSION_LOADER_MAP = {
16
+ '.json': JsonFileConfigLoader,
17
+ '.yaml': YamlFileConfigLoader,
18
+ '.yml': YamlFileConfigLoader,
19
+ }
20
+
21
+
22
+ def guess_loader_class(file_name: str) -> Type[IAllConfLoader]:
23
+ """Returns the most probable loader class based of the given file's extension.
24
+ """
25
+ ext = os.path.splitext(file_name)[-1]
26
+ loader = _EXTENSION_LOADER_MAP.get(ext, None)
27
+ if not loader:
28
+ raise AllConfUnknownFileTypeError(f'Dont know how to autoload file extension {ext}', file_name=file_name)
29
+ return loader
30
+
31
+
32
+ def raw_load(file_name: str, skip_env_loading: bool = False, skip_fidelius: bool = False, skip_py_inject: bool = False, encoding: str = 'utf-8') -> Dict:
33
+ """Loads and parses the given config file and returns the "raw" results as a
34
+ simple python dictionary.
35
+
36
+ This is mostly intended for the testing and debugging of the configuration
37
+ files themselves if needed.
38
+
39
+ :param file_name: The file to load
40
+ :param skip_env_loading: Set to True to skip resolving environment variable tags
41
+ :param skip_fidelius: Set to True to skip resolving Fidelius tags
42
+ :param skip_py_inject: Set to True to skip injecting Python imported values
43
+ :param encoding: Encoding to use when reading the file (utf-8 by default)
44
+ :return: A dict with the results
45
+ """
46
+ loader = guess_loader_class(file_name)()
47
+ loader.load_file(file_name, no_env_load=skip_env_loading, no_fidelius=skip_fidelius,
48
+ no_py_inject=skip_py_inject, encoding=encoding)
49
+ return loader.data
50
+
51
+
52
+ def render_load(file_name: str, skip_env_loading: bool = True, skip_fidelius: bool = True, skip_py_inject: bool = True, encoding: str = 'utf-8') -> str:
53
+ """Loads and parses the given config file and returns the "render" results
54
+ as a str in the same format the original file used.
55
+
56
+ The main point of this method is to be able to load and parse an AllConf
57
+ configuration file, including all includes, extensions, internal references,
58
+ environment variables, Fidelius values etc. and render as a single
59
+ value/file of the same format.
60
+
61
+ As a use-case example, it was originally built in order to generate
62
+ Kubernetes YAML manifest files using AllConf' format to simplify the process
63
+ (i.e. includes, internal references, secret injection via Fidelius during
64
+ deployment and so on).
65
+
66
+ :param file_name: The file to load
67
+ :param skip_env_loading: Set to True to skip resolving environment variable
68
+ tags
69
+ :param skip_fidelius: Set to True to skip resolving Fidelius tags
70
+ :param skip_py_inject: Set to True to skip injecting Python imported values
71
+ :param encoding: Encoding to use when reading the file (utf-8 by default)
72
+ :return: A string representation of the configuration data loaded, in the
73
+ same format as the target file if possible
74
+ """
75
+ loader = guess_loader_class(file_name)()
76
+ loader.load_file(file_name, no_env_load=skip_env_loading, no_fidelius=skip_fidelius, no_py_inject=skip_py_inject, encoding=encoding)
77
+ return loader.rendered
78
+
79
+
80
+ def autoload(file_name: str, encoding: str = 'utf-8') -> BaseConfig:
81
+ """Loads and parses the given config file and returns as a `BaseConfig`
82
+ object.
83
+
84
+ :param file_name:
85
+ :type file_name:
86
+ :param encoding:
87
+ :type encoding:
88
+ :return:
89
+ :rtype:
90
+ """
91
+ loader = guess_loader_class(file_name)()
92
+ loader.load_file(file_name, encoding=encoding)
93
+ return BaseConfig(**raw_load(file_name))