Skip to content

Commit

Permalink
refactor(config: ConfigRoot): continue replacing globals to singleton…
Browse files Browse the repository at this point in the history
…s - 2
  • Loading branch information
actionless committed Jul 16, 2023
1 parent 5f1263a commit b05bb84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
37 changes: 21 additions & 16 deletions pikaur/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@ def get_value(cls) -> Path:
return cls.value


VERSION: "Final" = "1.16.1-dev"

DEFAULT_CONFIG_ENCODING: "Final" = "utf-8"
BOOL: "Final" = "bool"
INT: "Final" = "int"
STR: "Final" = "str"


def pre_arg_parser(key: str, fallback: str) -> str:
if key in sys.argv:
return sys.argv[
Expand All @@ -130,6 +122,14 @@ def pre_arg_parser(key: str, fallback: str) -> str:
return fallback


VERSION: "Final" = "1.16.1-dev"

DEFAULT_CONFIG_ENCODING: "Final" = "utf-8"
BOOL: "Final" = "bool"
INT: "Final" = "int"
STR: "Final" = "str"


class CustomUserId(IntOrBoolSingleton):
@classmethod
def init_value(cls) -> int:
Expand Down Expand Up @@ -199,10 +199,14 @@ def get_value(cls) -> Path:
return CacheRoot()() / "pkg"


CONFIG_ROOT: "Final" = Path(os.environ.get(
"XDG_CONFIG_HOME",
Home()() / ".config/",
))
class ConfigRoot(PathSingleton):
@classmethod
def get_value(cls) -> Path:
return Path(os.environ.get(
"XDG_CONFIG_HOME",
Home()() / ".config/",
))


DATA_ROOT: "Final" = (
Path(os.environ.get(
Expand Down Expand Up @@ -234,7 +238,7 @@ def get_config_path() -> Path:
config_overridden = pre_arg_parser("--pikaur-config", "")
if config_overridden:
return Path(config_overridden)
return CONFIG_ROOT / "pikaur.conf"
return ConfigRoot()() / "pikaur.conf"


class UpgradeSortingValues:
Expand Down Expand Up @@ -511,13 +515,14 @@ def write_config(config: configparser.ConfigParser | None = None) -> None:
need_write = True
if need_write:
update_custom_user_id(int(config["misc"]["UserId"]))
if not CONFIG_ROOT.exists():
CONFIG_ROOT.mkdir(parents=True)
config_root = ConfigRoot()()
if not config_root.exists():
config_root.mkdir(parents=True)
with get_config_path().open("w", encoding=DEFAULT_CONFIG_ENCODING) as configfile:
config.write(configfile)
if custom_user_id := CustomUserId()():
for path in (
CONFIG_ROOT,
config_root,
get_config_path(),
):
os.chown(path, custom_user_id, custom_user_id)
Expand Down
4 changes: 2 additions & 2 deletions pikaur/makepkg_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING

from .args import parse_args
from .config import CONFIG_ROOT, _UserTempRoot
from .config import ConfigRoot, _UserTempRoot
from .core import open_file
from .privilege import using_dynamic_users

Expand Down Expand Up @@ -96,7 +96,7 @@ def get_user_makepkg_path(cls) -> Path | None:
if cls._user_makepkg_path is cls._UNSET:
possible_paths = [
Path("~/.makepkg.conf").expanduser(),
CONFIG_ROOT / "pacman/makepkg.conf",
ConfigRoot()() / "pacman/makepkg.conf",
]
config_path: Path | None = None
for path in possible_paths:
Expand Down

0 comments on commit b05bb84

Please sign in to comment.