From d5effa744f23472d328f71d7bda9ebd46540c265 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 18 Oct 2020 20:17:45 +0200 Subject: Added a JSON serializer for certain non-json objects. --- archinstall/lib/general.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'archinstall/lib') diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 393bf69a..10d22c31 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -1,5 +1,6 @@ import os, json, hashlib, shlex, sys import time, pty +from datetime import datetime from subprocess import Popen, STDOUT, PIPE, check_output from select import epoll, EPOLLIN, EPOLLHUP from .exceptions import * @@ -30,6 +31,46 @@ def locate_binary(name): return os.path.join(root, file) break # Don't recurse +def to_json(dictionary): + return json.dumps(dictionary, cls=) + +class JSON_Encoder: + def _encode(obj): + if isinstance(obj, dict): + ## We'll need to iterate not just the value that default() usually gets passed + ## But also iterate manually over each key: value pair in order to trap the keys. + + for key, val in list(obj.items()): + if isinstance(val, dict): + val = json.loads(json.dumps(val, cls=JSON)) # This, is a EXTREMELY ugly hack.. + # But it's the only quick way I can think of to + # trigger a encoding of sub-dictionaries. + else: + val = JSON_Encoder._encode(val) + del(obj[key]) + obj[JSON_Encoder._encode(key)] = val + return obj + elif hasattr(obj, 'json'): + return obj.json() + elif hasattr(obj, '__dump__'): + return obj.__dump__() + elif isinstance(obj, (datetime, date)): + return obj.isoformat() + elif isinstance(obj, (list, set, tuple)): + r = [] + for item in obj: + r.append(json.loads(json.dumps(item, cls=JSON))) + return r + else: + return obj + +class JSON(json.JSONEncoder, json.JSONDecoder): + def _encode(self, obj): + return JSON_Encoder._encode(obj) + + def encode(self, obj): + return super(JSON, self).encode(self._encode(obj)) + class sys_command():#Thread): """ Stolen from archinstall_gui -- cgit v1.2.3-70-g09d2 From 17a70eb9f459230e7e11beb724a68051b2f1c667 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 18 Oct 2020 20:18:20 +0200 Subject: Removed a stub function --- archinstall/lib/general.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'archinstall/lib') diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 10d22c31..2f4713d8 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -31,9 +31,6 @@ def locate_binary(name): return os.path.join(root, file) break # Don't recurse -def to_json(dictionary): - return json.dumps(dictionary, cls=) - class JSON_Encoder: def _encode(obj): if isinstance(obj, dict): -- cgit v1.2.3-70-g09d2 From 3d4eaec4662197e15b8f8c05fcefc2a5fa487da5 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 18 Oct 2020 20:19:21 +0200 Subject: Forgot an import --- archinstall/lib/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'archinstall/lib') diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 2f4713d8..ff834241 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -1,6 +1,6 @@ import os, json, hashlib, shlex, sys import time, pty -from datetime import datetime +from datetime import datetime, date from subprocess import Popen, STDOUT, PIPE, check_output from select import epoll, EPOLLIN, EPOLLHUP from .exceptions import * -- cgit v1.2.3-70-g09d2 From fa4be63e483a0a692f26103b45ad6abe73879f37 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 18 Oct 2020 20:26:09 +0200 Subject: Forgot an import, as well as made BlockDevice() have less verbose output on json.dumps. --- archinstall/lib/disk.py | 10 ++++++++++ examples/guided.py | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'archinstall/lib') diff --git a/archinstall/lib/disk.py b/archinstall/lib/disk.py index 7cd8dd0f..b11f2318 100644 --- a/archinstall/lib/disk.py +++ b/archinstall/lib/disk.py @@ -25,6 +25,16 @@ class BlockDevice(): raise KeyError(f'{self} does not contain information: "{key}"') return self.info[key] + def json(self): + """ + json() has precedence over __dump__, so this is a way + to give less/partial information for user readability. + """ + return { + 'path' : self.path, + 'size' : self.info['size'] if 'size' in self.info else '' + } + def __dump__(self): return { 'path' : self.path, diff --git a/examples/guided.py b/examples/guided.py index 00d2565c..958b1e5f 100644 --- a/examples/guided.py +++ b/examples/guided.py @@ -1,4 +1,5 @@ -import archinstall, getpass, time, json +import archinstall +import getpass, time, json, sys def perform_installation(device, boot_partition, language, mirrors): """ -- cgit v1.2.3-70-g09d2 From 1bd6a19dc46d693ab55d25a0275db8fcf83e20d9 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 18 Oct 2020 20:27:40 +0200 Subject: Made Profile() json-dumpable --- archinstall/lib/profiles.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'archinstall/lib') diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 47e6dd36..e018f753 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -61,6 +61,9 @@ class Profile(): self._cache = None self.args = args + def __dump__(self, *args, **kwargs): + return {'path' : self._path} + def __repr__(self, *args, **kwargs): return f'Profile({self._path} <"{self.path}">)' -- cgit v1.2.3-70-g09d2