Last active
April 4, 2021 06:15
-
-
Save evindunn/7528d0a863a7e842ac7cda72ff0c959c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from urllib.request import urlretrieve | |
from tempfile import NamedTemporaryFile, TemporaryDirectory | |
from zipfile import ZipFile | |
from glob import iglob | |
from os.path import join as path_join | |
from xml.etree import ElementTree | |
from json import dumps as json_stringify | |
from re import sub as regex_sub | |
ISIS_RELEASE = "4.4.0" | |
ISIS_URL_PREFIX = "https://github.com/USGS-Astrogeology/ISIS3/archive/refs/tags" | |
isis_cmds = list() | |
def collapse_whitespace(input_str: str) -> str: | |
return regex_sub(r"\s+", " ", input_str).strip() | |
with NamedTemporaryFile() as f, TemporaryDirectory() as d: | |
isis_url = "{}/{}.zip".format(ISIS_URL_PREFIX, ISIS_RELEASE) | |
urlretrieve(isis_url, f.name) | |
with ZipFile(f.name) as zf: | |
zf.extractall(d) | |
glob_path = path_join(d, "ISIS*", "isis", "src", "*", "apps", "*", "*.xml") | |
for xml_file in iglob(glob_path, recursive=True): | |
isis_cmd = dict() | |
xml_tree = ElementTree.parse(xml_file) | |
isis_xml = xml_tree.getroot() | |
isis_cmd["name"] = isis_xml.attrib.get("name") | |
isis_cmd["description"] = collapse_whitespace(isis_xml.find("brief").text) | |
isis_cmd["parameters"] = list() | |
param_group_xml = isis_xml.find("groups") | |
if param_group_xml: | |
param_groups = param_group_xml.findall("group") | |
else: | |
param_groups = list() | |
for param_group in param_groups: | |
parameters = param_group.findall("parameter") | |
for param in parameters: | |
param_name = param.attrib.get("name") | |
param_type = collapse_whitespace(param.find("type").text) | |
param_desc = collapse_whitespace(param.find("brief").text) | |
isis_cmd["parameters"].append({ | |
"name": param_name, | |
"type": param_type, | |
"description": param_desc | |
}) | |
isis_cmds.append(isis_cmd) | |
print(json_stringify(isis_cmds, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output is a list of commands, each looks like: