Ejemplo n.º 1
0
    def _parseEnumeratedPropDef(propName, valueFormatStr):
        '''
        Parses a definition in which one of either the property name
        (or property value) contains an enumeration. Returns a list of
        GOPropertyDefinition objects.

        @propName - the raw property name from the config file line.
        @valueFormats - the raw value string from the config file line.
        '''
        #~ print '%s' % (
            #~ 'GOPropertySyntaxParser._parseEnumeratedPropDef : "%s"'
             #~ % valueFormatStr)
        
        eTok = GOPropertySyntaxParser.ENUM_TOKEN
        
        assert eTok in propName, 'Expected "%s" in property name!' % (
            eTok)
                    
        assert eTok not in valueFormatStr, '%s' % (
            'Unexpected "' + eTok + '" in property value!')
        
        allEnums = enums.getAllEnumsDict()

        assert isinstance(allEnums, dict)
        
        # Check that propName is a valid Enum name
        enumName = propName.strip(eTok)
        
        assert enumName in allEnums, '%s' % (
            'Invalid enumeration : ' + enumName)
        
        format = GOPropertySyntaxParser._parseStringToValueFormat(
            valueFormatStr)

        assert isinstance(format, PropValueFormat), '%s' % (
            'Expected PropValueFormat, got %s' % format)
        
        defs = []
        print '\t', allEnums[enumName]

        for name in allEnums[enumName].names():
            defs += [GOPropertyDefinition(name, format)]
        return defs        
Ejemplo n.º 2
0
    def _getSingleValueType(valueFormatString):
        '''
        Parses the type of value for SINGLE property values.

        e.g ... = SINGLE(int)
            ... = SINGLE($EnumName)
        '''
        assert enums.PropValueFormat.SINGLE.name + '(' in '%s' % (
            valueFormatString)
        
        assert valueFormatString.count(
            enums.PropValueFormat.SINGLE.name + '(') == 1
        
        assert valueFormatString.count('(') == 1
        assert valueFormatString.count(')') == 1

        startIn = valueFormatString.index('(') + 1
        endIn = valueFormatString.index(')')

        typeString = valueFormatString[startIn : endIn]

        eTok = GOPropertySyntaxParser.ENUM_TOKEN

        if typeString.startswith(eTok):
            # Enumerated type
            enumName = typeString.strip(eTok)
            allEnums = enums.getAllEnumsDict()
            try:
                #~ print '>> found enum :', enumName
                return allEnums[enumName]
            except:
                print 'No Enum found named :', enumName
                sys.exit()
        else:
            # Primitive type, or garbage.
            for name, pType in PRIM_TYPES.iteritems():
                if typeString == name:
                    return pType
            print 'Unrecognised value : "%s"' % typeString
            sys.exit()
Ejemplo n.º 3
0
    def _parseCompTypePropMap(cfg, goGrammar):
        '''
        Parses the COMP_TYPE_TO_PROPS_NAME section from the config
        and returns a GOPropertyComponentMap object.
        
        @cfg - the ConfigParser object containing the ComponentType to
        properties information.
        
        @goGrammar - A GOGrammar object.
        '''
        print 'GameConfigParser._parseCompTypePropMap'
        
        eTok = GOPropertySyntaxParser.ENUM_TOKEN
        
        # Check that all names are valid ComponentType names.
        # We can't check the values until all of the
        # GOPropertyDefinitions have been created.
        sectionName = GameConfig.COMP_TYPE_TO_PROPS_NAME
        allEnums = enums.getAllEnumsDict()
        map = {}        

        for o in cfg.options(sectionName):
            #~ print '>> option :', o, '=', cfg.get(sectionName, o)
            # o must be a valid ComponentType name
            assert o in enums.ComponentType.names(), '%s' % (
                '%s not found in ComponentType!' % o)
            
            ovalue = cfg.get(sectionName, o)
            
            # The value can be an Enum name (if it starts with a
            # GOPropertySyntaxParser.ENUM_TOKEN) or a comma-separated
            # list of property names.
            if ovalue.startswith(eTok):
                enumName = ovalue.strip(eTok)
                if enumName in allEnums:
                    #~ print '[found Enum :', enumName, ']'
                    map[o] = []

                    # Get all GOPropertyDefinitions whose names are
                    # equal to the names() in this Enum
                    for name in allEnums[enumName].names():
                        if goGrammar.hasProperty(name):
                            #~ print ' GOGrammar has "%s"' % (
                                #~ name)
                            map[o].append(goGrammar.getDefinition(
                                name))
            elif ',' in ovalue:
                map[o] = []
                #~ print 'CommaSeparated :', ovalue
                
                for v in ovalue.split(','):
                    v = v.strip()
                    if goGrammar.hasProperty(v):
                        #~ print ' GOGrammar has "%s" prop def' % (v)
                        map[o].append(goGrammar.getDefinition(v))
                    else:
                        v = '"%s"' % v
                        print '>>> No', v, 'GOPropertyDefinition found!'
                        sys.exit()
            else:
                if goGrammar.hasProperty(ovalue):
                    #~ print ' GOGrammar has "%s" prop def' % (ovalue)
                    map[o] = [goGrammar.getDefinition(ovalue)]
                    #~ print '>> found non-enum :', ovalue
        print 'creating new GOPropertyComponentMap'
        return GOPropertyComponentMap(map)