index : archinstall32 | |
Archlinux32 installer | gitolite user |
summaryrefslogtreecommitdiff |
author | Werner Llácer <wllacer@gmail.com> | 2022-01-02 12:28:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-02 12:28:49 +0100 |
commit | b89408ab7ba89fe03b62c9ae7f3d32ce74ebc035 (patch) | |
tree | 6b539a4488b5fdd244ca21b6926de1c5f67e2f64 /archinstall/lib/general.py | |
parent | e388537bc36534fe782e1e68a70ddcdb2e13e99a (diff) |
-rw-r--r-- | archinstall/lib/general.py | 28 |
diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 7c8f8ea3..cc50e80a 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -481,3 +481,31 @@ def run_custom_user_commands(commands, installation): execution_output = SysCommand(f"arch-chroot {installation.target} bash /var/tmp/user-command.{index}.sh") log(execution_output) os.unlink(f"{installation.target}/var/tmp/user-command.{index}.sh") + +def json_stream_to_structure(id : str, stream :str, target :dict) -> bool : + """ Function to load a stream (file (as name) or valid JSON string into an existing dictionary + Returns true if it could be done + Return false if operation could not be executed + +id is just a parameter to get meaningful, but not so long messages + """ + from pathlib import Path + if Path(stream).exists(): + try: + with open(Path(stream)) as fh: + target.update(json.load(fh)) + except Exception as e: + log(f"{id} = {stream} does not contain a valid JSON format: {e}",level=logging.ERROR) + return False + else: + log(f"{id} = {stream} does not exists in the filesystem. Trying as JSON stream",level=logging.DEBUG) + # NOTE: failure of this check doesn't make stream 'real' invalid JSON, just it first level entry is not an object (i.e. dict), so it is not a format we handle. + if stream.strip().startswith('{') and stream.strip().endswith('}'): + try: + target.update(json.loads(stream)) + except Exception as e: + log(f" {id} Contains an invalid JSON format : {e}",level=logging.ERROR) + return False + else: + log(f" {id} is neither a file nor is a JSON string:",level=logging.ERROR) + return False + return True |