mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-11-26 18:19:30 +00:00
feat: read includes from list of directories
This commit is contained in:
1 parent
a9a12cd247
commit
255ebe117a
9 files changed
+45
-43
No files matched your search
@@ -18,6 +18,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import argparse
|
||||
import os
|
||||
|
||||
from pathlib import Path
|
||||
from pwd import getpwuid
|
||||
@@ -161,8 +162,9 @@ class Setup(Handler):
|
||||
if args.generate_salt:
|
||||
configuration.set_option("auth", "salt", User.generate_password(20))
|
||||
|
||||
(root.include / "00-setup-overrides.ini").unlink(missing_ok=True) # remove old-style configuration
|
||||
target = root.include / f"00-setup-overrides-{repository_id.id}.ini"
|
||||
include_path = next(path for path in root.getpathlist("settings", "include") if os.access(path, os.W_OK))
|
||||
(include_path / "00-setup-overrides.ini").unlink(missing_ok=True) # remove old-style configuration
|
||||
target = include_path / f"00-setup-overrides-{repository_id.id}.ini"
|
||||
with target.open("w", encoding="utf8") as ahriman_configuration:
|
||||
configuration.write(ahriman_configuration)
|
||||
|
||||
|
||||
@@ -108,16 +108,6 @@ class Configuration(configparser.RawConfigParser):
|
||||
_, repository_id = self.check_loaded()
|
||||
return repository_id.architecture
|
||||
|
||||
@property
|
||||
def include(self) -> Path:
|
||||
"""
|
||||
get full path to include directory
|
||||
|
||||
Returns:
|
||||
Path: path to directory with configuration includes
|
||||
"""
|
||||
return self.getpath("settings", "include")
|
||||
|
||||
@property
|
||||
def logging_path(self) -> Path:
|
||||
"""
|
||||
@@ -325,25 +315,35 @@ class Configuration(configparser.RawConfigParser):
|
||||
section, key = name.rsplit(":", maxsplit=1)
|
||||
self.set_option(section, key, value)
|
||||
|
||||
def load_includes(self, path: Path | None = None) -> None:
|
||||
def load_includes(self) -> None:
|
||||
"""
|
||||
load configuration includes from specified path
|
||||
|
||||
Args:
|
||||
path(Path | None, optional): path to directory with include files. If none set, the default path will be
|
||||
used (Default value = None)
|
||||
"""
|
||||
self.includes = [] # reset state
|
||||
|
||||
try:
|
||||
path = path or self.include
|
||||
# raw processing to make sure that options are applied correctly
|
||||
include_directories = shlex.split(self.get("settings", "include", raw=True))
|
||||
except (configparser.NoOptionError, configparser.NoSectionError):
|
||||
return
|
||||
|
||||
for directory in include_directories:
|
||||
value = self._interpolation.before_get( # type: ignore[attr-defined]
|
||||
self,
|
||||
"settings",
|
||||
"include",
|
||||
directory,
|
||||
self._unify_values("settings", None), # type: ignore[attr-defined]
|
||||
)
|
||||
path = self._convert_path(value)
|
||||
if not path.is_dir():
|
||||
continue
|
||||
|
||||
for include in sorted(path.glob("*.ini")):
|
||||
if include == self.logging_path:
|
||||
continue # we don't want to load logging explicitly
|
||||
self.read(include)
|
||||
self.includes.append(include)
|
||||
except (FileNotFoundError, configparser.NoOptionError, configparser.NoSectionError):
|
||||
pass
|
||||
|
||||
def merge_sections(self, repository_id: RepositoryId) -> None:
|
||||
"""
|
||||
@@ -403,6 +403,7 @@ class Configuration(configparser.RawConfigParser):
|
||||
# create another instance and copy values from there
|
||||
instance = self.from_path(path, repository_id)
|
||||
self.copy_from(instance)
|
||||
self.includes = instance.includes
|
||||
|
||||
def set_option(self, section: str, option: str, value: str) -> None:
|
||||
"""
|
||||
|
||||
@@ -40,10 +40,13 @@ CONFIGURATION_SCHEMA: ConfigurationSchema = {
|
||||
"required": True,
|
||||
},
|
||||
"include": {
|
||||
"type": "path",
|
||||
"coerce": "absolute_path",
|
||||
"path_exists": True,
|
||||
"path_type": "dir",
|
||||
"type": "list",
|
||||
"coerce": "list",
|
||||
"schema": {
|
||||
"type": "path",
|
||||
"coerce": "absolute_path",
|
||||
"path_type": "dir",
|
||||
},
|
||||
},
|
||||
"logging": {
|
||||
"type": "path",
|
||||
|
||||
@@ -190,5 +190,5 @@ class Validator(RootValidator):
|
||||
{"type": "string"}
|
||||
"""
|
||||
fn = getattr(value, f"is_{constraint}")
|
||||
if not fn():
|
||||
if value.exists() and not fn():
|
||||
self._error(field, f"Path {value} must be type of {constraint}")
|
||||
Reference in new issue
Block a user