Skip to content

Commit 962c7f9

Browse files
committed
Add JSON encoders, string and JSON representation for identifier
Use this to print identifier as JSON string. This will increase the readability of the command-line results. Signed-off-by: Bernd Hufmann <[email protected]>
1 parent 46fd01b commit 962c7f9

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

tsp/identifier.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
"""Identifier Service Class"""
2424

25+
import json
26+
2527
SERVER_VERSION = "version"
2628
BUILD_TIME = "buildTime"
2729
OS_NAME = "os"
@@ -113,8 +115,26 @@ def __init__(self, params):
113115
else:
114116
self.tsp_version = None
115117

116-
def to_string(self):
117-
'''
118-
to_string method
119-
'''
120-
return f"Identifier[version={self.server_version}, buildTime={self.build_time}, os={self.os_name}, osArch={self.os_arch}, osVersion={self.os_version}, cpuCount={self.cpu_count}, maxMemory={self.max_memory}, productId={self.product_id}, launcherName={self.launcher_name}, tspVersion={self.tsp_version}]"
118+
def __repr__(self):
119+
return f'Identifier(version={self.server_version}, build_time={self.build_time}, os={self.os_name}, os_arch={self.os_arch}, os_version={self.os_version}, cpu_count={self.cpu_count}, max_memory={self.max_memory}, product_id={self.product_id}, launcher_name={self.launcher_name}, tsp_version={self.tsp_version})'
120+
121+
def to_json(self):
122+
return json.dumps(self, cls=IdentifierEncoder, indent=4)
123+
124+
125+
class IdentifierEncoder(json.JSONEncoder):
126+
def default(self, obj):
127+
if isinstance(obj, Identifier):
128+
return {
129+
SERVER_VERSION: obj.server_version,
130+
BUILD_TIME: obj.build_time,
131+
OS_NAME: obj.os_name,
132+
OS_ARCH: obj.os_arch,
133+
OS_VERSION: obj.os_version,
134+
CPU_COUNT: obj.cpu_count,
135+
MAX_MEMORY: obj.max_memory,
136+
PRODUCT_ID: obj.product_id,
137+
LAUNCHER_NAME: obj.launcher_name,
138+
TSP_VERSION: obj.tsp_version
139+
}
140+
return super().default(obj)

tsp_cli_client

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,9 @@ if __name__ == "__main__":
591591
if options.get_identifier:
592592
response = tsp_client.fetch_identifier()
593593
if response.status_code == 200:
594-
print(' {0}'.format(response.model.to_string()))
594+
print('Successfully listed identifier')
595+
print('------------------------------')
596+
print(response.model.to_json())
595597
sys.exit(0)
596598
else:
597599
sys.exit(1)

0 commit comments

Comments
 (0)