rkt-config-lib 1.4.0__py3-none-any.whl → 2.0.3__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.
config/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .config import Config
config/config.py ADDED
@@ -0,0 +1,59 @@
1
+ import os
2
+ from typing import Any
3
+
4
+ import yaml
5
+
6
+ from logger import Logger
7
+ from tool import Singleton, Tool
8
+
9
+
10
+ class Config(metaclass=Singleton): # type: ignore[metaclass]
11
+ """
12
+ Configuration manager wrapping PyYaml.
13
+
14
+ This class handles the loading of YAML configuration files from a specific directory.
15
+ It implements the Singleton pattern to ensure centralized configuration management.
16
+ """
17
+
18
+ __slots__ = ["_me", "_logger", "_tool", "_skills_file", "data"]
19
+
20
+ def __init__(self) -> None:
21
+ self._me = self.__class__.__name__
22
+ self._logger = Logger(caller_class=self._me)
23
+ self._logger.set_logger(caller_class=self._me)
24
+ self._tool = Tool()
25
+ self.data: dict[str, Any] = {}
26
+
27
+ def get_data(self, needed_file: str = "", _config_dir: str = "config", create_if_not_exist: bool = False) -> None:
28
+ """
29
+ Load configuration files from the config directory.
30
+
31
+ It scans the directory for YAML files and loads them into `self.data`.
32
+
33
+ Args:
34
+ needed_file (str, optional): A specific file to load. If empty, loads all YAML files in the directory.
35
+ Defaults to "".
36
+ _config_dir (str, optional): Name of the configuration directory. Defaults to "config".
37
+ create_if_not_exist (bool, optional): Create the config directory if it doesn't exist. Defaults to False.
38
+ """
39
+ config_dir = self._tool.get_dir(_config_dir)
40
+
41
+ if (not config_dir or not os.path.exists(config_dir)) and create_if_not_exist:
42
+ os.makedirs(_config_dir, exist_ok=True)
43
+ config_dir = self._tool.formatted_from_os(os.path.abspath(_config_dir))
44
+
45
+ if needed_file:
46
+ filename = os.path.basename(needed_file).split(".")[0]
47
+ with open(f"{config_dir}{needed_file}", encoding="utf8") as nf:
48
+ self._logger.add(level="info", caller=self._me, message=f"Load '{filename}' file ...")
49
+ self.data[filename] = yaml.safe_load(nf)
50
+ else:
51
+ for file in os.listdir(config_dir):
52
+ try:
53
+ (filename, ext) = os.path.basename(file).split(".")
54
+ except ValueError:
55
+ continue
56
+ if ext in ["yml", "yaml"] and filename not in self.data.keys():
57
+ with open(f"{config_dir}{file}", encoding="utf8") as f:
58
+ self._logger.add(level="info", caller=self._me, message=f"Load '{filename}' file ...")
59
+ self.data[filename] = yaml.safe_load(f)
@@ -0,0 +1,36 @@
1
+ Metadata-Version: 2.4
2
+ Name: rkt_config_lib
3
+ Version: 2.0.3
4
+ Summary: RootKit custom PyYaml Lib
5
+ Author-email: RootKit <rootkit@rootkit-lab.org>
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.7
10
+ Description-Content-Type: text/markdown
11
+ Requires-Dist: PyYaml>=6.0.1
12
+ Requires-Dist: rkt_tool_lib
13
+ Requires-Dist: rkt_logger_lib
14
+
15
+ # rkt_config_lib
16
+
17
+ Centralized Configuration Management wrapper around PyYaml.
18
+
19
+ ## Features
20
+ - **Singleton Pattern**: Ensures a single configuration state across your application.
21
+ - **Auto-Loading**: Automatically scans and loads all YAML files in a `config/` directory.
22
+ - **Safe Loading**: Uses `yaml.safe_load` by default to prevent arbitrary code execution vulnerabilities.
23
+ - **Integrated Logging**: Logs loading operations via `rkt_logger_lib`.
24
+
25
+ ## Usage
26
+
27
+ ```python
28
+ from config import Config
29
+
30
+ conf = Config()
31
+ # Loads all .yml/.yaml files from ./config/ directory
32
+ conf.get_data()
33
+
34
+ # Access data (filename becomes the key)
35
+ my_settings = conf.data.get("settings")
36
+ ```
@@ -0,0 +1,6 @@
1
+ config/__init__.py,sha256=Iu75-w9_nlPmnB_qKA7nYaaaHf7xtTrDmK8N4v2WV34,27
2
+ config/config.py,sha256=Xnza7siGOMmhBR3yAqmq042vASKzsElW97y29FOIGcI,2572
3
+ rkt_config_lib-2.0.3.dist-info/METADATA,sha256=KBwIzrLEMFRQStSFndtbFraQgob0pZrX7o3Bpr3EnoE,1067
4
+ rkt_config_lib-2.0.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
5
+ rkt_config_lib-2.0.3.dist-info/top_level.txt,sha256=9hK4m828QBN59kTX5IVy40cPd9zUw5QWQF2AlSrXCJ4,7
6
+ rkt_config_lib-2.0.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (80.10.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -0,0 +1 @@
1
+ config
rkt_config_lib/Config.py DELETED
@@ -1,53 +0,0 @@
1
- import os
2
- import yaml
3
-
4
- try:
5
- from rkt_logger_lib.Logger import Logger
6
- except ImportError:
7
- from rkt_lib_toolkit.rkt_logger_lib.Logger import Logger
8
-
9
- try:
10
- from rkt_tool_lib.Tool import Tool, Singleton
11
- except ImportError:
12
- from rkt_lib_toolkit.rkt_tool_lib.Tool import Tool, Singleton
13
-
14
-
15
- class Config(metaclass=Singleton):
16
- """
17
- Basic PyYaml wrapper
18
- add custom logger, list of file need to be load
19
-
20
- """
21
- __slots__ = ["_me", "_logger", "_tool", "_skills_file", "data"]
22
-
23
- def __init__(self) -> None:
24
- self._me = self.__class__.__name__
25
- self._logger = Logger(caller_class=self._me)
26
- self._logger.set_logger(caller_class=self._me)
27
- self._tool = Tool()
28
- self.data = {}
29
-
30
- def get_data(self, needed_file: str = "", _config_dir: str = "config", create_if_not_exist: bool = False) -> None:
31
- """
32
- Load all file in 'config_dir' and get data in dict formatted as : {"basename_1": <VALUE_1>, ...}
33
- """
34
- config_dir = self._tool.get_dir(_config_dir)
35
-
36
- if (not config_dir or not os.path.exists(config_dir)) and create_if_not_exist:
37
- os.makedirs(_config_dir, exist_ok=True)
38
-
39
- if needed_file:
40
- filename = os.path.basename(needed_file).split(".")[0]
41
- with open(f"{config_dir}{needed_file}", "r", encoding='utf8') as nf:
42
- self._logger.add(level="info", caller=self._me, message=f"Load '{filename}' file ...")
43
- self.data[filename] = yaml.load(nf, Loader=yaml.FullLoader)
44
- else:
45
- for file in os.listdir(config_dir):
46
- try:
47
- (filename, ext) = os.path.basename(file).split(".")
48
- except ValueError:
49
- continue
50
- if ext in ["yml", "yaml"] and filename not in self.data.keys():
51
- with open(f"{config_dir}{file}", "r", encoding='utf8') as f:
52
- self._logger.add(level="info", caller=self._me, message=f"Load '{filename}' file ...")
53
- self.data[filename] = yaml.load(f, Loader=yaml.FullLoader)
@@ -1 +0,0 @@
1
- from .Config import Config
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016 Dabo Ross
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,104 +0,0 @@
1
- Metadata-Version: 2.2
2
- Name: rkt_config_lib
3
- Version: 1.4.0
4
- Summary: RootKit custom PyYaml Lib
5
- Author: RootKit
6
- Author-email: rootkit@rootkit-lab.org
7
- Classifier: Development Status :: 5 - Production/Stable
8
- Classifier: Intended Audience :: Developers
9
- Classifier: Intended Audience :: End Users/Desktop
10
- Classifier: Intended Audience :: Information Technology
11
- Classifier: License :: OSI Approved :: MIT License
12
- Classifier: Natural Language :: English
13
- Classifier: Natural Language :: French
14
- Classifier: Operating System :: OS Independent
15
- Classifier: Programming Language :: Python :: 3.7
16
- Classifier: Topic :: Utilities
17
- Requires-Python: >=3.7
18
- Description-Content-Type: text/markdown
19
- License-File: LICENSE
20
- Requires-Dist: PyYaml
21
- Requires-Dist: rkt_tool_lib
22
- Requires-Dist: rkt_logger_lib
23
- Dynamic: author
24
- Dynamic: author-email
25
- Dynamic: classifier
26
- Dynamic: description
27
- Dynamic: description-content-type
28
- Dynamic: requires-dist
29
- Dynamic: requires-python
30
- Dynamic: summary
31
-
32
- # rkt_config_lib - Python library
33
-
34
- ![Package Version](https://badgen.net/badge/Package%20Version/latest%20-%201.3.0/green?scale=1.2)
35
-
36
- ![quality](https://sonarqube.tprc.ovh/api/project_badges/measure?project=python_rkt_lib_toolkit_AXqQ32evGCA0VuRY8SuD&metric=alert_status)
37
- ![reliability_rating](https://sonarqube.tprc.ovh/api/project_badges/measure?project=python_rkt_lib_toolkit_AXqQ32evGCA0VuRY8SuD&metric=reliability_rating)
38
- ![security_rating](https://sonarqube.tprc.ovh/api/project_badges/measure?project=python_rkt_lib_toolkit_AXqQ32evGCA0VuRY8SuD&metric=security_rating)
39
- ![vulnerabilities](https://sonarqube.tprc.ovh/api/project_badges/measure?project=python_rkt_lib_toolkit_AXqQ32evGCA0VuRY8SuD&metric=vulnerabilities)
40
- ![coverage](https://sonarqube.tprc.ovh/api/project_badges/measure?project=python_rkt_lib_toolkit_AXqQ32evGCA0VuRY8SuD&metric=coverage)
41
- ![maintainability](https://sonarqube.tprc.ovh/api/project_badges/measure?project=python_rkt_lib_toolkit_AXqQ32evGCA0VuRY8SuD&metric=sqale_rating)
42
-
43
- This Python library is based only on built-in Python libraries and one (1) non-build-in library : [PyYaml](https://pypi.org/project/PyYAML/)
44
-
45
- ##### Python Version 3.7.2
46
- ##### PyYaml Version 5.4.1 (Released Jan 20, 2021)
47
-
48
- ----
49
-
50
- ## What is Python?
51
- Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
52
-
53
- [source](https://en.wikipedia.org/wiki/Python_(programming_language))
54
- ## What is PyYaml?
55
- YAML is a data serialization format designed for human readability and interaction with scripting languages. PyYAML is a YAML parser and emitter for Python.
56
-
57
- PyYAML features a complete YAML 1.1 parser, Unicode support, pickle support, capable extension API, and sensible error messages. PyYAML supports standard YAML tags and provides Python-specific tags that allow to represent an arbitrary Python object.
58
-
59
- PyYAML is applicable for a broad range of tasks from complex configuration files to object serialization and persistence.
60
-
61
- [source](https://pypi.org/project/PyYAML/)
62
- ## Libraries
63
- * Config: overlay of PyYaml library (read-only), use Tool and Logger library (rkt_tool_lib, rkt_logger_lib)
64
-
65
- ## Use it
66
- ### Install
67
- ```bash
68
- (venv) my_project> pip install rkt_config_lib [--index-url https://gitlab.tprc.ovh/api/v4/groups/python/-/packages/pypi]
69
- ```
70
- ### Example
71
- ```python
72
- from rkt_config_lib import Config
73
-
74
- c = Config()
75
-
76
- # by default search folder named "config" in root project folder
77
- # for load all yaml files
78
- c.get_data()
79
-
80
- print(f"{c.data}")
81
- ```
82
-
83
- ### Output (as file, sdtout or both)
84
- ```log
85
- 03/03/2022 16:44:09 :: [Logger] :: INFO :: Create logger for 'Config'
86
- 03/03/2022 16:44:09 :: [Logger] :: INFO :: add 'StreamHandler' in 'Config' logger
87
- 03/03/2022 16:44:09 :: [Logger] :: INFO :: add 'FileHandler' in 'Config' logger
88
- 03/03/2022 16:44:09 :: [Config] :: INFO :: Load 'database' file ...
89
- ```
90
- ```
91
- {'database': {'connect_id': {'dbms': 'mariadb'}}}
92
- ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
93
- | |
94
- | data: file content
95
- file name without extension
96
- ```
97
-
98
- ## Contributing
99
-
100
- If you find this library useful here's how you can help:
101
-
102
- - Send a merge request with your kickass new features and bug fixes
103
- - Help new users with [issues](https://gitlab.tprc.ovh/python/rkt_lib_toolkit/-/issues) they may encounter
104
- - Support the development of this library and star this repo!
@@ -1,7 +0,0 @@
1
- rkt_config_lib/Config.py,sha256=ypDnZEWDrl8wOFn1yfXjTOvw3OV6Lz4k4KzVxTOWYks,2098
2
- rkt_config_lib/__init__.py,sha256=dgAqGP8sTB2ZsUt6k9MYpd3Kscb8fgZKVF_V-UCD7b4,27
3
- rkt_config_lib-1.4.0.dist-info/LICENSE,sha256=cJOcDuuCS3-_wqJQeJzXupPQBeTXBxQR858ECRPkXkw,1097
4
- rkt_config_lib-1.4.0.dist-info/METADATA,sha256=nPDKhDkAbFda8zOnLsnTZVqGH1Z39VkbERjoRMNoV34,4550
5
- rkt_config_lib-1.4.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
6
- rkt_config_lib-1.4.0.dist-info/top_level.txt,sha256=gVNNYSVkxR-HO_zOynapeEAmZpco7DioxliVdgpMRfU,15
7
- rkt_config_lib-1.4.0.dist-info/RECORD,,
@@ -1 +0,0 @@
1
- rkt_config_lib