def loadConfig(path): configObject = None # if config file is valid JSON, interpret it as JSON text = open(path, 'r').read() try: configObject = json.loads(text) except ValueError: if os.access(path, os.X_OK): pass else: raise # fallback plan -- execute config file and use its output if configObject is None: logging.info( 'config file "%s" is executable; running it and using output as config data', path) p = os.path.abspath(path) proc = subprocess.Popen([p], stdout=subprocess.PIPE) stdoutData, _stderrData = proc.communicate() if proc.returncode != 0: logging.warning( 'executing config file "%s" failed with return code %s', path, proc.returncode) configObject = json.loads(stdoutData) return convertToDotDictRecurse(configObject)
def to_python(self, value): ''' Takes json string from database and builds a DotDict structure ''' if value == '': return DotDict() elif type(value) == DotDict: return value else: theDotDict = DotDict() try: jsonStruct = json.loads(value) theDotDict = convertToDotDictRecurse(jsonStruct) assert isinstance( theDotDict, DotDict), 'expected a DotDict object, found a %s' % type( theDotDict).__name__ for key in theDotDict.iterkeys(): assert type(key) in ( unicode, str ), 'expected unicode or str keys, found a %s' % type( key).__name__ except (ValueError, AssertionError), e: #print 'problem with json for %s ' % (value) raise ValidationError( 'Invalid JSON data in ExtrasDotField: %s' % e) return theDotDict
def loadConfig(path): configObject = None # if config file is valid JSON, interpret it as JSON text = open(path, 'r').read() try: configObject = json.loads(text) except ValueError: if os.access(path, os.X_OK): pass else: raise # fallback plan -- execute config file and use its output if configObject is None: logging.info('config file "%s" is executable; running it and using output as config data', path) p = os.path.abspath(path) proc = subprocess.Popen([p], stdout=subprocess.PIPE) stdoutData, _stderrData = proc.communicate() if proc.returncode != 0: logging.warning('executing config file "%s" failed with return code %s', path, proc.returncode) configObject = json.loads(stdoutData) return convertToDotDictRecurse(configObject)
def validateJson(newJsonObj): ''' Validate input json against defined schema ''' try: theSchema = getPlanSchema(newJsonObj['platform']['name']) loadDocumentFromDict(convertToDotDictRecurse(newJsonObj), theSchema.schema) return True except Exception, e: return "Invalid JSON: " + str(e)
def planDocFromPlanDict(planDict, schema): # downstream processing tools assume plan is a DotDict planDict = convertToDotDictRecurse(planDict) planDoc = (xpjson.loadDocumentFromDict (planDict, schema=schema, parseOpts=xpjson.ParseOpts(fillInDefaults=True))) # fill in ids fillIds = FillIdsPlanExporter() return fillIds.exportPlan(planDoc, schema)
def exportStation(self, station, context): """ Because we are adding up the timing we have to change the order here and build the station first before the children """ tsequence = [] tsequence.append(self.transformStation(station, tsequence, context)) for i, cmd in enumerate(convertToDotDictRecurse(station.commands)): ctx = context.copy() ctx.command = cmd ctx.commandIndex = i tsequence.append(self.transformStationCommand(cmd, ctx)) return tsequence
def exportSegment(self, segment, context): tsequence = [] if hasattr(segment, 'commands'): segment.sequence = convertToDotDictRecurse(segment.commands) for i, cmd in enumerate(segment.sequence): ctx = context.copy() ctx.command = cmd ctx.commandIndex = i tsequence.append(self.transformSegmentCommand(cmd, ctx)) else: segment.sequence = [] return self.transformSegment(segment, tsequence, context)
def planDocFromPlanDict(planDict, schema): # downstream processing tools assume plan is a DotDict planDict = convertToDotDictRecurse(planDict) planDoc = (xpjson.loadDocumentFromDict( planDict, schema=schema, parseOpts=xpjson.ParseOpts(fillInDefaults=True))) # fill in ids fillIds = FillIdsPlanExporter() return fillIds.exportPlan(planDoc, schema)
def exportSegment(self, segment, context): """ For a segment, the activities come first and then the timing for the drive. """ tsequence = [] for i, cmd in enumerate(convertToDotDictRecurse(segment.commands)): ctx = context.copy() ctx.command = cmd ctx.commandIndex = i tsequence.append(self.transformSegmentCommand(cmd, ctx)) tsequence.append(self.transformSegment(segment, tsequence, context)) return tsequence
def to_python(self, value): ''' Takes json string from database and builds a DotDict structure ''' if value == '': return DotDict() elif type(value) == DotDict: return value else: theDotDict = DotDict() try: jsonStruct = json.loads(value) theDotDict = convertToDotDictRecurse(jsonStruct) assert isinstance(theDotDict, DotDict), 'expected a DotDict object, found a %s' % type(theDotDict).__name__ for key in theDotDict.iterkeys(): assert type(key) in (unicode, str), 'expected unicode or str keys, found a %s' % type(key).__name__ except (ValueError, AssertionError), e: raise ValidationError('Invalid JSON data in ExtrasDotField: %s' % e) return theDotDict
def xml2struct(inName, inputIsString=False): return convertToDotDictRecurse(xml2dict(inName, inputIsString))
def loadDictFromFile(f): """ Load a DotDict from the JSON-format file *f*. """ return dotDict.convertToDotDictRecurse(json.load(f))
def loadDictFromString(s): """ Load a DotDict from the JSON-format string *s*. """ return dotDict.convertToDotDictRecurse(json.loads(s))
def plan_REST(request, plan_id, jsonPlanId): """ Read and write plan JSON. jsonPlanId is ignored. It's for human-readabilty in the URL """ plan = PLAN_MODEL.get().objects.get(pk=plan_id) if request.method == "PUT": data = json.loads(request.body) for k, v in data.iteritems(): if k == "_simInfo": continue plan.jsonPlan[k] = v # print json.dumps(data, indent=4, sort_keys=True) plan.extractFromJson(overWriteDateModified=True) plan.save() plan = handleCallbacks(request, plan, settings.SAVE) elif request.method == "POST": # we are doing a save as plan.jsonPlan.creator = request.user.username plan.creationDate = datetime.datetime.now(pytz.utc) plan.uuid = None plan.pk = None print "PLAN REST: POST body: %s" % request.body data = json.loads(request.body) for k, v in data.iteritems(): if k == "_simInfo": continue plan.jsonPlan[k] = v plan.extractFromJson(overWriteDateModified=True, overWriteUuid=True) # plan.name = plan.jsonPlan['planName'] # plan.jsonPlan['name'] = plan.jsonPlan['planName'] # TODO I don't understand why this did not work above plan.creator = request.user plan.jsonPlan.creator = request.user.username #make sure it is not read only plan.readOnly = False plan.save() newid = plan.pk plan.jsonPlan["serverId"] = newid plan.jsonPlan["planNumber"] = newid plan.jsonPlan.url = plan.get_absolute_url() # we still need to renumber the plan schema = models.getPlanSchema(plan.jsonPlan.platform['name']) exporter = fillIdsPlanExporter.FillIdsPlanExporter() planDict = convertToDotDictRecurse(plan.jsonPlan) updateAllUuids(planDict) plan.jsonPlan = json.dumps(exporter.exportPlan(planDict, schema.schema)) plan.uuid = planDict.uuid plan.save() handleCallbacks(request, plan, settings.SAVE) response = {} response["msg"] = "New plan created" response["data"] = newid return HttpResponse(json.dumps(response), content_type='application/json') return HttpResponse(json.dumps(plan.jsonPlan), content_type='application/json')
def plan_REST(request, plan_id, jsonPlanId): """ Read and write plan JSON. jsonPlanId is ignored. It's for human-readabilty in the URL """ plan = PLAN_MODEL.get().objects.get(pk=plan_id) if request.method == "PUT": data = json.loads(request.body) for k, v in data.iteritems(): if k == "_simInfo": continue plan.jsonPlan[k] = v # print json.dumps(data, indent=4, sort_keys=True) plan.extractFromJson(overWriteDateModified=True) plan.save() plan = handleCallbacks(request, plan, settings.SAVE) elif request.method == "POST": # we are doing a save as plan.jsonPlan.creator = request.user.username plan.creationDate = datetime.datetime.now(pytz.utc) plan.uuid = None plan.pk = None data = json.loads(request.body) for k, v in data.iteritems(): if k == "_simInfo": continue plan.jsonPlan[k] = v plan.extractFromJson(overWriteDateModified=True, overWriteUuid=True) plan.name = plan.jsonPlan['planName'] plan.jsonPlan['name'] = plan.jsonPlan['planName'] # TODO I don't understand why this did not work above plan.creator = request.user plan.jsonPlan.creator = request.user.username #make sure it is not read only plan.readOnly = False plan.save() newid = plan.pk plan.jsonPlan["serverId"] = newid plan.jsonPlan["planNumber"] = newid plan.jsonPlan.url = plan.get_absolute_url() # we still need to renumber the plan schema = models.getPlanSchema(plan.jsonPlan.platform['name']) exporter = fillIdsPlanExporter.FillIdsPlanExporter() planDict = convertToDotDictRecurse(plan.jsonPlan) updateAllUuids(planDict) plan.jsonPlan = json.dumps(exporter.exportPlan(planDict, schema.schema)) plan.uuid = planDict.uuid plan.save() handleCallbacks(request, plan, settings.SAVE) response = {} response["msg"] = "New plan created" response["data"] = newid return HttpResponse(json.dumps(response), content_type='application/json') return HttpResponse(json.dumps(plan.jsonPlan), content_type='application/json')