Skip to content

Instantly share code, notes, and snippets.

@evindunn
Last active April 4, 2021 06:15
Show Gist options
  • Save evindunn/7528d0a863a7e842ac7cda72ff0c959c to your computer and use it in GitHub Desktop.
Save evindunn/7528d0a863a7e842ac7cda72ff0c959c to your computer and use it in GitHub Desktop.
#!/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))
@evindunn
Copy link
Author

evindunn commented Apr 4, 2021

Output is a list of commands, each looks like:

  {
    "name": "mroctx2isis",
    "description": "Import an MRO CTX image as an Isis cube",
    "parameters": [
      {
        "name": "FROM",
        "type": "filename",
        "description": "Input file"
      },
      {
        "name": "TO",
        "type": "cube",
        "description": "Output cube"
      },
      {
        "name": "PREFIX",
        "type": "integer",
        "description": "Prefix Pixels"
      },
      {
        "name": "SUFFIX",
        "type": "integer",
        "description": "Suffix Pixels"
      },
      {
        "name": "FILLGAP",
        "type": "boolean",
        "description": "Set zeros to null"
      }
    ]
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment