import json
from typing import List, Dict
+from xml.dom.minidom import parseString
from .cli import StatsCLICommand
from .fs.perf_stats import FSPerfStats
+try:
+ from dicttoxml import dicttoxml # type: ignore[import-not-found]
+except ImportError:
+ dicttoxml = None
+
class Module(MgrModule):
CLICommand = StatsCLICommand
COMMANDS = [
prefix = cmd['prefix']
# only supported command is `fs perf stats` right now
if prefix.startswith('fs perf stats'):
- return self.fs_perf_stats.get_perf_data(cmd)
+ result = self.fs_perf_stats.get_perf_data(cmd)
+ if 'format' in cmd:
+ if cmd['format'] == 'json-pretty':
+ return 0, json.dumps(result, indent=2), ""
+ elif cmd['format'] == 'xml':
+ if dicttoxml is None:
+ raise ImportError("dicttoxml package required for xml")
+ result = json.loads(json.dumps(result, default=str))
+ return dicttoxml(result)
+ elif cmd['format'] == 'xml-pretty':
+ if dicttoxml is None:
+ raise ImportError("dicttoxml package required for xml")
+ res_xml = parseString(dicttoxml(result))
+ return res_xml.toprettyxml()
+ return 0, json.dumps(result), ""
raise NotImplementedError(cmd['prefix'])