def main(): import optparse parser = optparse.OptionParser('''usage: %prog [opts] <cmd> ... %prog validate <doc.json> Validate XPJSON document %prog simplify <doc.json> <out.json> Simplify XPJSON document (compile out inheritance, fill defaults) %prog import <doc.kml> Import a plan into the database. Supported formats: only KML for now. ''') parser.add_option('-s', '--schema', help='Schema to use when processing Plan or PlanLibrary') importOpts = optparse.OptionGroup(parser, 'Import-specific options') importOpts.add_option( '--formatCode', help= 'Specify importer class by format code (default is to infer based on extension)' ) importOpts.add_option('--creator', help='Specify creator username [required]') importOpts.add_option('--planNumber', type='int', help='Specify plan number [required]') importOpts.add_option('--planVersion', help='Specify plan version [required]') parser.add_option_group(importOpts) opts, args = parser.parse_args() if not args: parser.error('expected a command') cmd = args[0] if opts.schema is not None: schema = xpjson.loadDocument(opts.schema) if schema.type != 'PlanSchema': parser.error( '--schema must be the path to a XPJSON PlanSchema document') else: schema = None ###################################################################### if cmd == 'validate': if len(args) != 2: parser.error('validate requires exactly 1 argument') docPath = args[1] try: doc = xpjson.loadDocument(docPath, schema) print 'VALID %s %s' % (doc.get('type'), docPath) except xpjson.NoSchemaError, t: parser.error( 'you must specify the --schema argument to validate a document of type %s' % t) except:
def buildLibrary(planSchema): schema = xpjson.loadDocument(planSchema.schemaSource) library = xpjson.loadDocument(planSchema.librarySource, schema=schema, fillInDefaults=True) xpjson.dumpDocumentToPath(planSchema.simplifiedLibraryPath, library) print 'wrote simplified library for platform %s to %s' % (planSchema.platform, planSchema.simplifiedLibraryPath)
def test(): schema = xpjson.loadDocument('fix') plan = xpjson.loadDocument('/Users/mfsmith3/projects/gds/xgds_isru/apps/xgds_kn/planner/plans/K10Black/20100802/HMP_B013A_PLAN-xp.json', schema=schema) exporter = FillIdsPlanExporter() planDoc = exporter.exportPlan(plan, schema) open('/tmp/foo.json', 'wb').write(xpjson.dumpDocumentToString(planDoc))
def test(): schema = xpjson.loadDocument('fix') plan = xpjson.loadDocument( '/Users/mfsmith3/projects/gds/xgds_isru/apps/xgds_kn/planner/plans/K10Black/20100802/HMP_B013A_PLAN-xp.json', schema=schema) exporter = FillIdsPlanExporter() planDoc = exporter.exportPlan(plan, schema) open('/tmp/foo.json', 'wb').write(xpjson.dumpDocumentToString(planDoc))
def buildLibrary(planSchema): schema = xpjson.loadDocument(planSchema.schemaSource) library = xpjson.loadDocument(planSchema.librarySource, schema=schema, fillInDefaults=True) xpjson.dumpDocumentToPath(planSchema.simplifiedLibraryPath, library) print 'wrote simplified library for platform %s to %s' % ( planSchema.platform, planSchema.simplifiedLibraryPath)
def main(): import optparse parser = optparse.OptionParser( """usage: %prog [opts] <cmd> ... %prog validate <doc.json> Validate XPJSON document %prog simplify <doc.json> <out.json> Simplify XPJSON document (compile out inheritance, fill defaults) %prog import <doc.kml> Import a plan into the database. Supported formats: only KML for now. """ ) parser.add_option("-s", "--schema", help="Schema to use when processing Plan or PlanLibrary") importOpts = optparse.OptionGroup(parser, "Import-specific options") importOpts.add_option( "--formatCode", help="Specify importer class by format code (default is to infer based on extension)" ) importOpts.add_option("--creator", help="Specify creator username [required]") importOpts.add_option("--planNumber", type="int", help="Specify plan number [required]") importOpts.add_option("--planVersion", help="Specify plan version [required]") parser.add_option_group(importOpts) opts, args = parser.parse_args() if not args: parser.error("expected a command") cmd = args[0] if opts.schema is not None: schema = xpjson.loadDocument(opts.schema) if schema.type != "PlanSchema": parser.error("--schema must be the path to a XPJSON PlanSchema document") else: schema = None ###################################################################### if cmd == "validate": if len(args) != 2: parser.error("validate requires exactly 1 argument") docPath = args[1] try: doc = xpjson.loadDocument(docPath, schema) print "VALID %s %s" % (doc.get("type"), docPath) except xpjson.NoSchemaError, t: parser.error("you must specify the --schema argument to validate a document of type %s" % t) except:
def loadDocument(path): """ Return the result of xpjson.loadDocument(), but first setting some configuration we prefer for Astrobee. """ xpjson.CHECK_UNKNOWN_FIELDS = False # suppress some warnings xpjson.KEEP_PARAM_SPECS = True # inhibit deletion of paramSpecs during load return xpjson.loadDocument(path)
def getSchema(self): if not self.schema: try: self.schema = xpjson.loadDocument(self.simplifiedSchemaPath) except: # pylint: disable=W0702 logging.warning('could not load XPJSON schema from ' + self.simplifiedSchemaPath) raise return self.schema
def getLibrary(self): if not self.library: try: self.library = xpjson.loadDocument(self.simplifiedLibraryPath, schema=self.getSchema(), fillInDefaults=True) except: # pylint: disable=W0702 logging.warning('could not load XPJSON library from ' + self.simplifiedLibraryPath) raise return self.library
def writeCommandDictionary(inSchemaPath, outHtmlPath, **kwargs): """ The master function that reads in an XPJSON PlanSchema document and outputs a command dictionary. """ settings = copy.deepcopy(DEFAULT_DICTIONARY_SETTINGS) settings.update(kwargs) xpjson.CHECK_UNKNOWN_FIELDS = False # suppress some warnings schema = xpjson.loadDocument(inSchemaPath) hlist = [] def p(x): hlist.append(x) hlist.append('\n') commandSpecs = sorted(schema.commandSpecs, key=lambda spec: spec.id) if settings['includeIndex']: p('<div class="command_index">') p(' <h6 class="title">Index</h6>') p(' <ul>') for c in commandSpecs: p(' <li><a href="#command_%s">%s</a></li>' % (c.id, c.id)) p(' </ul>') p('</div>') for c in commandSpecs: # logging.debug('%s', json.dumps(getCommandSpecInfo(settings, c), sort_keys=True, indent=4)) commandInfo = getCommandSpecInfo(settings, c) chtml = getCommandSpecHtml(settings['commandSpecTemplateHtml'], commandInfo) p(chtml) outHtmlText = settings['docTemplateHtml'] % { 'title': settings['title'], 'style': settings['styleSheet'], 'commands': ''.join(hlist) } with open(outHtmlPath, 'w') as outHtmlStream: outHtmlStream.write(outHtmlText) logging.info('wrote output to %s', outHtmlPath)
def writeCommandDictionary(inSchemaPath, outHtmlPath, **kwargs): """ The master function that reads in an XPJSON PlanSchema document and outputs a command dictionary. """ settings = copy.deepcopy(DEFAULT_DICTIONARY_SETTINGS) settings.update(kwargs) xpjson.CHECK_UNKNOWN_FIELDS = False # suppress some warnings schema = xpjson.loadDocument(inSchemaPath) hlist = [] def p(x): hlist.append(x) hlist.append('\n') commandSpecs = sorted(schema.commandSpecs, key=lambda spec: spec.id) if settings['includeIndex']: p('<div class="command_index">') p(' <h2 class="title">Index</h2>') p(' <ul>') for c in commandSpecs: p(' <li><a href="#command_%s">%s</a></li>' % (c.id, c.id)) p(' </ul>') p('</div>') for c in commandSpecs: # logging.debug('%s', json.dumps(getCommandSpecInfo(settings, c), sort_keys=True, indent=4)) commandInfo = getCommandSpecInfo(settings, c) chtml = getCommandSpecHtml(settings['commandSpecTemplateHtml'], commandInfo) p(chtml) outHtmlText = settings['docTemplateHtml'] % { 'title': settings['title'], 'style': settings['styleSheet'], 'commands': ''.join(hlist) } with open(outHtmlPath, 'w') as outHtmlStream: outHtmlStream.write(outHtmlText) logging.info('wrote output to %s', outHtmlPath)
def test_schema(self): _schema = xpjson.loadDocument(SCHEMA_PATH)
def test(): schema = xpjson.loadDocument(xpjson.EXAMPLE_PLAN_SCHEMA_PATH) plan = xpjson.loadDocument(xpjson.EXAMPLE_PLAN_PATH, schema=schema) exporter = KmlPlanExporter() open('/tmp/foo.kml', 'wb').write(exporter.exportPlan(plan, schema))
def test_plan(self): schema = xpjson.loadDocument(SCHEMA_PATH) _plan = xpjson.loadDocument(PLAN_PATH, schema=schema)
def buildSchema(planSchema): schema = xpjson.loadDocument(planSchema.schemaSource) xpjson.dumpDocumentToPath(planSchema.simplifiedSchemaPath, schema) print 'wrote simplified schema for platform %s to %s' % (planSchema.platform, planSchema.simplifiedSchemaPath)
def test_library(self): schema = xpjson.loadDocument(SCHEMA_PATH) _library = xpjson.loadDocument(LIBRARY_PATH, schema=schema)
'you must specify the --schema argument to validate a document of type %s' % t) except: traceback.print_exc() print print 'INVALID %s' % docPath ###################################################################### elif cmd == 'simplify': if len(args) != 3: parser.error('simplify requires exactly 2 arguments') docPath = args[1] outPath = args[2] try: doc = xpjson.loadDocument(docPath, schema, fillInDefaults=True) xpjson.dumpDocumentToPath(outPath, doc) print 'wrote simplified %s to %s' % (doc.get('type'), outPath) except xpjson.NoSchemaError, t: parser.error( 'you must specify the --schema argument to validate a document of type %s' % t) except: traceback.print_exc() print print 'ERROR could not simplify %s' % docPath ###################################################################### elif cmd == 'import': if len(args) != 2: parser.error('import requires exactly 1 argument (import path)')
def buildSchema(planSchema): schema = xpjson.loadDocument(planSchema.schemaSource) xpjson.dumpDocumentToPath(planSchema.simplifiedSchemaPath, schema) print 'wrote simplified schema for platform %s to %s' % ( planSchema.platform, planSchema.simplifiedSchemaPath)
except xpjson.NoSchemaError, t: parser.error("you must specify the --schema argument to validate a document of type %s" % t) except: traceback.print_exc() print print "INVALID %s" % docPath ###################################################################### elif cmd == "simplify": if len(args) != 3: parser.error("simplify requires exactly 2 arguments") docPath = args[1] outPath = args[2] try: doc = xpjson.loadDocument(docPath, schema, fillInDefaults=True) xpjson.dumpDocumentToPath(outPath, doc) print "wrote simplified %s to %s" % (doc.get("type"), outPath) except xpjson.NoSchemaError, t: parser.error("you must specify the --schema argument to validate a document of type %s" % t) except: traceback.print_exc() print print "ERROR could not simplify %s" % docPath ###################################################################### elif cmd == "import": if len(args) != 2: parser.error("import requires exactly 1 argument (import path)") importPath = args[1]