From: Joerg Behrmann Date: Wed, 27 Sep 2023 09:15:52 +0000 (+0200) Subject: json: add alternative default constructors to MkosiArgs and MkosiConfig X-Git-Tag: v18~13^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cca504851ef9d09eae9b1cd72df89d6e96448670;p=thirdparty%2Fmkosi.git json: add alternative default constructors to MkosiArgs and MkosiConfig These can be used to get a default MkosiArgs/MkosiConfig just have if mkosi had been called in an empty directory with no cmdline without adding defaults to the attributes and allowing instances of MkosiArgs/MkosiConfig to be made with missing keys. --- diff --git a/mkosi/config.py b/mkosi/config.py index 9b778570c..e7c556827 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -19,6 +19,7 @@ import platform import shlex import shutil import subprocess +import tempfile import textwrap import uuid from collections.abc import Collection, Iterable, Iterator, Sequence @@ -662,6 +663,18 @@ class MkosiArgs: auto_bump: bool doc_format: DocFormat + @classmethod + def default(cls) -> "MkosiArgs": + """Alternative constructor to generate an all-default MkosiArgs. + + This prevents MkosiArgs being generated with defaults values implicitly. + """ + with tempfile.TemporaryDirectory() as tempdir: + with chdir(tempdir): + args, _ = parse_config([]) + + return args + @classmethod def from_namespace(cls, ns: argparse.Namespace) -> "MkosiArgs": return cls(**{ @@ -815,6 +828,18 @@ class MkosiConfig: preset: Optional[str] + @classmethod + def default(cls) -> "MkosiConfig": + """Alternative constructor to generate an all-default MkosiArgs. + + This prevents MkosiArgs being generated with defaults values implicitly. + """ + with tempfile.TemporaryDirectory() as tempdir: + with chdir(tempdir): + _, [config] = parse_config([]) + + return config + @classmethod def from_namespace(cls, ns: argparse.Namespace) -> "MkosiConfig": return cls(**{