diff --git a/README.md b/README.md index bf28295..3b9adff 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ sudo ln -s $BREW_PY_CELLAR /opt/homebrew ``` +## Config + +There are a few configuration options to modify brew.py behavior. +For default values and configuration options, see [config.ini](config.ini). + + ## FAQ ### Why this project? diff --git a/brew.py b/brew.py index 65a0252..3aae54f 100755 --- a/brew.py +++ b/brew.py @@ -971,41 +971,25 @@ class Config: @staticmethod def load(fname: str) -> None: - if not os.path.exists(fname): - with open(fname, 'w') as fp: - fp.write(''' -[install] -; whether install should link binaries of main package (user-installed) -link_bin_primary = yes ; default: yes -; whether install should link binaries of dependencies -link_bin_dependency = no ; default: no - -[cleanup] -; unit: s|m|h|d (secs, mins, hours, days) -download = 21d ; default: 21d -cache = 5d ; default: 5d -auth = 365d ; default: 365d -''') ini = IniFile(inline_comment_prefixes=(';', '#')) ini.read(fname) def timed(value: str) -> int: unit = value[-1].lower() - mul = {'s': 1, 'm': 60, 'h': 60 * 60, 'd': 24 * 60 * 60}.get(unit) + mul = {'s': 1, 'm': 60, 'h': 3600, 'd': 86_400}.get(unit) if not mul: raise AttributeError(f'Unkown time unit "{value}" in config') return int(value[:-1]) * mul - sec = ini['install'] Config.INSTALL = Config.Install( - LINK_BIN_PRIM=sec.getboolean('link_bin_primary', fallback=True), - LINK_BIN_DEPS=sec.getboolean('link_bin_dependency', fallback=False) + ini.getboolean('install', 'link_bin_primary', fallback=True), + ini.getboolean('install', 'link_bin_dependency', fallback=False), ) - sec = ini['cleanup'] + Config.CLEANUP = Config.Cleanup( - DOWNLOAD=timed(sec.get('download', '21d')), - CACHE=timed(sec.get('cache', '5d')), - AUTH=timed(sec.get('auth', 'never')), + timed(ini.get('cleanup', 'download', fallback='21d')), + timed(ini.get('cleanup', 'cache', fallback='5d')), + timed(ini.get('cleanup', 'auth', fallback='365d')), ) diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..953232b --- /dev/null +++ b/config.ini @@ -0,0 +1,11 @@ +[install] +; whether install should link binaries of main package (user-requested) +link_bin_primary = yes +; whether install should link binaries of dependencies +link_bin_dependency = no + +[cleanup] +; unit: s|m|h|d (secs, mins, hours, days) +download = 21d +cache = 5d +auth = 365d