Skip to content

Commit

Permalink
Add formating as json
Browse files Browse the repository at this point in the history
  • Loading branch information
APN-Pucky committed Dec 29, 2022
1 parent e061d91 commit 4648367
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 30 additions & 0 deletions easybuild/tools/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import copy
import inspect
import os
import json
from easybuild.tools import LooseVersion

from easybuild.base import fancylogger
Expand Down Expand Up @@ -72,6 +73,7 @@

FORMAT_TXT = 'txt'
FORMAT_RST = 'rst'
FORMAT_JSON = 'json'


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


def list_software_json(software, detailed=False):
"""
Return overview of supported software in json
:param software: software information (strucuted like list_software does)
:param detailed: whether or not to return detailed information (incl. version, versionsuffix, toolchain info)
:return: multi-line string presenting requested info
"""
lines = ['[']
for key in sorted(software, key=lambda x: x.lower()):
for tmp in software[key]:
if detailed:
# deep copy here to avoid modifying the original dict
x = copy.deepcopy(tmp)
x['description'] = x['description'].split('\n')[0].strip()
else:
x = {}
x['name'] = key

lines.append(json.dumps(x, indent=4) + ",")
if detailed:
break
# remove last comma
if len(lines) > 1:
lines[-1] = lines[-1][:-1]
return '\n'.join(lines) + '\n]'


def list_toolchains(output_format=FORMAT_TXT):
"""Show list of known toolchains."""
_, all_tcs = search_toolchain('')
Expand Down
5 changes: 3 additions & 2 deletions easybuild/tools/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
from easybuild.tools.config import get_pretend_installpath, init, init_build_options, mk_full_default_path
from easybuild.tools.config import BuildOptions, ConfigurationVariables
from easybuild.tools.configobj import ConfigObj, ConfigObjError
from easybuild.tools.docs import FORMAT_TXT, FORMAT_RST
from easybuild.tools.docs import FORMAT_TXT, FORMAT_RST, FORMAT_JSON
from easybuild.tools.docs import avail_cfgfile_constants, avail_easyconfig_constants, avail_easyconfig_licenses
from easybuild.tools.docs import avail_toolchain_opts, avail_easyconfig_params, avail_easyconfig_templates
from easybuild.tools.docs import list_easyblocks, list_toolchains
Expand Down Expand Up @@ -463,7 +463,8 @@ def override_options(self):
'mpi-tests': ("Run MPI tests (when relevant)", None, 'store_true', True),
'optarch': ("Set architecture optimization, overriding native architecture optimizations",
None, 'store', None),
'output-format': ("Set output format", 'choice', 'store', FORMAT_TXT, [FORMAT_TXT, FORMAT_RST]),
'output-format': ("Set output format", 'choice', 'store', FORMAT_TXT,
[FORMAT_TXT, FORMAT_RST, FORMAT_JSON]),
'output-style': ("Control output style; auto implies using Rich if available to produce rich output, "
"with fallback to basic colored output",
'choice', 'store', OUTPUT_STYLE_AUTO, OUTPUT_STYLES),
Expand Down

0 comments on commit 4648367

Please sign in to comment.