]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
json: add alternative default constructors to MkosiArgs and MkosiConfig
authorJoerg Behrmann <behrmann@physik.fu-berlin.de>
Wed, 27 Sep 2023 09:15:52 +0000 (11:15 +0200)
committerJoerg Behrmann <behrmann@physik.fu-berlin.de>
Wed, 4 Oct 2023 07:48:07 +0000 (09:48 +0200)
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.

mkosi/config.py

index 9b778570cf1f1548d8d4c3776f01ebabc2c7035a..e7c556827fa5801a81c5758a1ac8b11ead734c5a 100644 (file)
@@ -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(**{