Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions archinstall/lib/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ArchConfig:
services: list[str] = field(default_factory=list)
custom_commands: list[str] = field(default_factory=list)

def unsafe_json(self) -> dict[str, Any]:
def unsafe_config(self) -> dict[str, Any]:
config: dict[str, list[UserSerialization] | str | None] = {}

if self.auth_config:
Expand All @@ -94,7 +94,7 @@ def unsafe_json(self) -> dict[str, Any]:

return config

def safe_json(self) -> dict[str, Any]:
def safe_config(self) -> dict[str, Any]:
config: Any = {
'version': self.version,
'script': self.script,
Expand Down
18 changes: 13 additions & 5 deletions archinstall/lib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import readline
import stat
from pathlib import Path
from typing import Any

from pydantic import TypeAdapter

from archinstall.lib.menu.helpers import Confirmation, Selection
from archinstall.lib.translationhandler import tr
Expand All @@ -10,7 +13,6 @@

from .args import ArchConfig
from .crypt import encrypt
from .general import JSON, UNSAFE_JSON
from .output import debug, logger, warn
from .utils.util import get_password, prompt_dir

Expand Down Expand Up @@ -40,12 +42,18 @@ def user_credentials_file(self) -> Path:
return self._user_creds_file

def user_config_to_json(self) -> str:
out = self._config.safe_json()
return json.dumps(out, indent=4, sort_keys=True, cls=JSON)
config = self._config.safe_config()

adapter = TypeAdapter(dict[str, Any])
python_dict = adapter.dump_python(config)
return json.dumps(python_dict, indent=4, sort_keys=True)

def user_credentials_to_json(self) -> str:
out = self._config.unsafe_json()
return json.dumps(out, indent=4, sort_keys=True, cls=UNSAFE_JSON)
config = self._config.unsafe_config()

adapter = TypeAdapter(dict[str, Any])
python_dict = adapter.dump_python(config)
return json.dumps(python_dict, indent=4, sort_keys=True)

def write_debug(self) -> None:
debug(' -- Chosen configuration --')
Expand Down
56 changes: 0 additions & 56 deletions archinstall/lib/general.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import json
import re
import secrets
import string
from datetime import date, datetime
from enum import Enum
from pathlib import Path
from shutil import which
from typing import Any, override

from archinstall.lib.exceptions import RequirementError

Expand Down Expand Up @@ -43,55 +39,3 @@ def clear_vt100_escape_codes(data: bytes) -> bytes:

def clear_vt100_escape_codes_from_str(data: str) -> str:
return re.sub(_VT100_ESCAPE_REGEX, '', data)


def jsonify(obj: Any, safe: bool = True) -> Any:
"""
Converts objects into json.dumps() compatible nested dictionaries.
Setting safe to True skips dictionary keys starting with a bang (!)
"""

compatible_types = str, int, float, bool
if isinstance(obj, dict):
return {
key: jsonify(value, safe)
for key, value in obj.items()
if isinstance(key, compatible_types) and not (isinstance(key, str) and key.startswith('!') and safe)
}
if isinstance(obj, Enum):
return obj.value
if hasattr(obj, 'json'):
# json() is a friendly name for json-helper, it should return
# a dictionary representation of the object so that it can be
# processed by the json library.
return jsonify(obj.json(), safe)
if isinstance(obj, datetime | date):
return obj.isoformat()
if isinstance(obj, list | set | tuple):
return [jsonify(item, safe) for item in obj]
if isinstance(obj, Path):
return str(obj)
if hasattr(obj, '__dict__'):
return vars(obj)

return obj


class JSON(json.JSONEncoder, json.JSONDecoder):
"""
A safe JSON encoder that will omit private information in dicts (starting with !)
"""

@override
def encode(self, o: Any) -> str:
return super().encode(jsonify(o))


class UNSAFE_JSON(json.JSONEncoder, json.JSONDecoder):
"""
UNSAFE_JSON will call/encode and keep private information in dicts (starting with !)
"""

@override
def encode(self, o: Any) -> str:
return super().encode(jsonify(o, safe=False))