forked from nasa/astrobee
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenCommandConfigLua.py
More file actions
executable file
·125 lines (101 loc) · 3.66 KB
/
genCommandConfigLua.py
File metadata and controls
executable file
·125 lines (101 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
#
# Copyright (c) 2017, United States Government, as represented by the
# Administrator of the National Aeronautics and Space Administration.
#
# All rights reserved.
#
# The Astrobee platform is licensed under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
A library and command-line tool for generating a RAPID-style CommandConstants.idl
file from an XPJSON schema.
"""
import argparse
import logging
import os
import sys
# hack to set up PYTHONPATH
ffroot = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, os.path.join(ffroot, "astrobee", "commands", "xgds_planner2"))
import luaTable
import xpjsonAstrobee
TEMPLATE_MAIN = """
-- Copyright (c) 2015 United States Government as represented by the
-- Administrator of the National Aeronautics and Space Administration.
-- All Rights Reserved.
commandConfig = %(table)s
"""
# END TEMPLATE_MAIN
def getParamConfig(param):
if "." in param.id:
category, baseId = param.id.split(".", 1)
else:
category, baseId = None, param.id
return {
"key": xpjsonAstrobee.fixName(baseId),
"type": xpjsonAstrobee.XPJSON_PARAM_VALUE_TYPE_MAPPINGS[param.valueType],
}
def getCommandConfig(cmd):
assert "." in cmd.id, "CommandSpec without category: %s" % cmd
category, baseId = cmd.id.split(".", 1)
return {
"name": baseId,
"parameters": [getParamConfig(p) for p in cmd.params],
}
def genCommandConfigLua(inSchemaPath, outCommandConfigPath):
schema = xpjsonAstrobee.loadDocument(inSchemaPath)
specs = sorted(schema.commandSpecs, key=lambda c: c.id)
categoryMap = {}
for spec in specs:
assert "." in spec.id, "CommandSpec without category: %s" % spec
category, baseId = spec.id.split(".", 1)
specsInCategory = categoryMap.setdefault(category, [])
specsInCategory.append(getCommandConfig(spec))
categories = sorted(categoryMap.keys())
config = {
"availableSubsystems": [
{"name": c, "subsystemTypeName": c + "Type"} for c in categories
],
"availableSubsystemTypes": [
{"name": k + "Type", "commands": categoryMap[k]} for k in categories
],
}
# import json; print json.dumps(config, indent=4, sort_keys=True)
table = luaTable.dumps(config)
with open(outCommandConfigPath, "w") as outStream:
outStream.write(TEMPLATE_MAIN % {"table": table})
logging.info("wrote command config Lua to %s", outCommandConfigPath)
class CustomFormatter(
argparse.RawDescriptionHelpFormatter, argparse.ArgumentDefaultsHelpFormatter
):
pass
def main():
parser = argparse.ArgumentParser(
description=__doc__ + "\n\n",
formatter_class=CustomFormatter,
)
parser.add_argument(
"inSchemaPath",
help="input XPJSON schema path",
)
parser.add_argument(
"outCommandConfigPath",
help="output Lua command config file",
nargs="?",
default="commands.config",
)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG, format="%(message)s")
genCommandConfigLua(args.inSchemaPath, args.outCommandConfigPath)
if __name__ == "__main__":
main()