Skip to content

Commit b792b58

Browse files
committed
Add formating as json
1 parent e061d91 commit b792b58

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

easybuild/tools/docs.py

+30
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import copy
3838
import inspect
3939
import os
40+
import json
4041
from easybuild.tools import LooseVersion
4142

4243
from easybuild.base import fancylogger
@@ -72,6 +73,7 @@
7273

7374
FORMAT_TXT = 'txt'
7475
FORMAT_RST = 'rst'
76+
FORMAT_JSON = 'json'
7577

7678

7779
def generate_doc(name, params):
@@ -736,6 +738,34 @@ def list_software_txt(software, detailed=False):
736738
return '\n'.join(lines)
737739

738740

741+
def list_software_json(software, detailed=False):
742+
"""
743+
Return overview of supported software in json
744+
745+
:param software: software information (strucuted like list_software does)
746+
:param detailed: whether or not to return detailed information (incl. version, versionsuffix, toolchain info)
747+
:return: multi-line string presenting requested info
748+
"""
749+
lines = ['[']
750+
for key in sorted(software, key=lambda x: x.lower()):
751+
for tmp in software[key]:
752+
if detailed:
753+
# deep copy here to avoid modifying the original dict
754+
x = copy.deepcopy(tmp)
755+
x['description'] = x['description'].split('\n')[0].strip()
756+
else:
757+
x = {}
758+
x['name'] = key
759+
760+
lines.append(json.dumps(x, indent=4) + ",")
761+
if detailed:
762+
break
763+
# remove last comma
764+
if len(lines) > 1:
765+
lines[-1] = lines[-1][:-1]
766+
return '\n'.join(lines) + '\n]'
767+
768+
739769
def list_toolchains(output_format=FORMAT_TXT):
740770
"""Show list of known toolchains."""
741771
_, all_tcs = search_toolchain('')

easybuild/tools/options.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
from easybuild.tools.config import get_pretend_installpath, init, init_build_options, mk_full_default_path
7777
from easybuild.tools.config import BuildOptions, ConfigurationVariables
7878
from easybuild.tools.configobj import ConfigObj, ConfigObjError
79-
from easybuild.tools.docs import FORMAT_TXT, FORMAT_RST
79+
from easybuild.tools.docs import FORMAT_TXT, FORMAT_RST, FORMAT_JSON
8080
from easybuild.tools.docs import avail_cfgfile_constants, avail_easyconfig_constants, avail_easyconfig_licenses
8181
from easybuild.tools.docs import avail_toolchain_opts, avail_easyconfig_params, avail_easyconfig_templates
8282
from easybuild.tools.docs import list_easyblocks, list_toolchains
@@ -463,7 +463,8 @@ def override_options(self):
463463
'mpi-tests': ("Run MPI tests (when relevant)", None, 'store_true', True),
464464
'optarch': ("Set architecture optimization, overriding native architecture optimizations",
465465
None, 'store', None),
466-
'output-format': ("Set output format", 'choice', 'store', FORMAT_TXT, [FORMAT_TXT, FORMAT_RST]),
466+
'output-format': ("Set output format", 'choice', 'store', FORMAT_TXT, [FORMAT_TXT, FORMAT_RST,
467+
FORMAT_JSON]),
467468
'output-style': ("Control output style; auto implies using Rich if available to produce rich output, "
468469
"with fallback to basic colored output",
469470
'choice', 'store', OUTPUT_STYLE_AUTO, OUTPUT_STYLES),

0 commit comments

Comments
 (0)