Esempio n. 1
0
    def testCFGOverridesForObject(self):
        schema = {'type': 'object', 'attributes': {'a': {'type': 'int'}}}

        class CFGObjectType(object):
            def __init__(self):
                self.a = 1
                self.b = 2

        cfgObject = CFGObjectType()
        override = Persist({'a': 0}, schema)
        loaderState = binaryLoader.LoaderState(
            binaryLoader.defaultLoaderFactories, cfgObject=cfgObject)
        loadedObject = binaryLoader.LoadFromString(
            override, schemaOptimizer.OptimizeSchema(schema, validator.SERVER),
            loaderState)
        self.assertEqual(loadedObject.a, 0)
        self.assertEqual(loadedObject.b, 2)
Esempio n. 2
0
    def testCFGOverridesForDict(self):
        schema = {
            'type': 'dict',
            'keyTypes': {
                'type': 'int'
            },
            'valueTypes': {
                'type': 'object',
                'attributes': {
                    'a': {
                        'type': 'int'
                    }
                }
            }
        }

        class CFGObjectType(object):
            def __init__(self, **kwargs):
                for i in kwargs:
                    setattr(self, i, kwargs[i])

        class CFGDictType(object):
            def __init__(self, d):
                self.__d__ = d

            def GetIfExists(self, k):
                if k in self.__d__:
                    return self.__d__[k]
                else:
                    return None

        cfgDictData = {1: CFGObjectType(a=1, b=2), 2: CFGObjectType(a=3)}
        override = Persist({1: {'a': 0}}, schema)
        loaderState = binaryLoader.LoaderState(
            binaryLoader.defaultLoaderFactories,
            cfgObject=CFGDictType(cfgDictData))
        loadedObject = binaryLoader.LoadFromString(
            override, schemaOptimizer.OptimizeSchema(schema, validator.SERVER),
            loaderState)
        self.assertEqual(loadedObject[1].a, 0)
        self.assertEqual(loadedObject[1].b, 2)
        self.assertEqual(loadedObject[2].a, 3)
Esempio n. 3
0
 def LoadFSDData(self,
                 filename,
                 schemaPath,
                 loadFromString=False,
                 idCacheCount=0):
     platform = boot.role.capitalize()
     res = blue.ResFile()
     if not res.Open(schemaPath):
         self.LogError('Could not load %s' % schemaPath)
     else:
         schema = fsdSchemaPersistence.LoadSchema(res.Read())
         schema = fsdSchemaPersistence.GetUnOptimizedRuntimeSchema(schema)
         schema = fsdSchemaOptimizer.OptimizeSchema(schema, platform)
     if not res.Open(filename):
         self.LogError('Could not load %s' % filename)
     else:
         if loadFromString:
             return fsdBinaryLoader.LoadFromString(res.Read(), schema)
         return fsdBinaryLoader.LoadIndexFromFile(
             fsdBinaryLoader.BlueResFileIOWrapper(res), schema,
             idCacheCount)
Esempio n. 4
0
def PersistAndMarshal(data, schema):
    optSchema = schemaOptimizer.OptimizeSchema(schema, validator.SERVER)
    binaryRepresentation = binaryRepresenter.RepresentAsBinary(data, optSchema)
    return binaryLoader.LoadFromString(binaryRepresentation, optSchema)
Esempio n. 5
0
def Marshal(data, schema):
    optSchema = schemaOptimizer.OptimizeSchema(schema, validator.SERVER)
    return binaryLoader.LoadFromString(data, optSchema)