Esempio n. 1
0
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)
Esempio n. 2
0
 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
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
 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
Esempio n. 8
0
 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)
Esempio n. 9
0
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)
Esempio n. 10
0
 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
Esempio n. 11
0
 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
Esempio n. 12
0
 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
Esempio n. 13
0
 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)
Esempio n. 14
0
 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
Esempio n. 15
0
def xml2struct(inName, inputIsString=False):
    return convertToDotDictRecurse(xml2dict(inName, inputIsString))
Esempio n. 16
0
def loadDictFromFile(f):
    """
    Load a DotDict from the JSON-format file *f*.
    """
    return dotDict.convertToDotDictRecurse(json.load(f))
Esempio n. 17
0
def loadDictFromFile(f):
    """
    Load a DotDict from the JSON-format file *f*.
    """
    return dotDict.convertToDotDictRecurse(json.load(f))
Esempio n. 18
0
def loadDictFromString(s):
    """
    Load a DotDict from the JSON-format string *s*.
    """
    return dotDict.convertToDotDictRecurse(json.loads(s))
Esempio n. 19
0
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')
Esempio n. 20
0
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')
Esempio n. 21
0
def xml2struct(inName, inputIsString=False):
    return convertToDotDictRecurse(xml2dict(inName, inputIsString))
Esempio n. 22
0
def loadDictFromString(s):
    """
    Load a DotDict from the JSON-format string *s*.
    """
    return dotDict.convertToDotDictRecurse(json.loads(s))