#!/usr/bin/python ############################################################################# # Copyright (C) 2010 Spidcom # ############################################################################# import os from const import * class MMTypeConstGen: """MMType Generation.""" def __init__ (self, infile, outfile = None): """Initialise class. infile: the input file to parse. outfile: the output file to write, default stdout. """ self.infile = infile self.outfile = outfile def __check_entries (self, mmtypelist): """Check entries MMType duplicity and Base.""" for i in range (len(mmtypelist)): mmtype = int (mmtypelist[i].value, 0) for c in MMTYPES_MIN_MAX: if mmtypelist[i].name.startswith(c[0]) \ and (mmtype < c[1] or mmtype > c[2]): msg = "%s has an MME Type outside bounds" \ % mmtypelist[i].name raise RuntimeError (msg) if mmtype & 0x3 != 0: msg = "%s base value must be 0x0, 0x4, 0x8, 0xC" \ % mmtypelist[i].name raise RuntimeError (msg) for j in range (i + 1, len (mmtypelist)): for c in MMTYPES_MIN_MAX: if mmtypelist[i].name.startswith(c[0]): len_i = len (c[0]) if mmtypelist[j].name.startswith(c[0]): len_j = len (c[0]) if mmtypelist[i].value == mmtypelist[j].value \ and mmtypelist[i].name[0:len_i] \ == mmtypelist[j].name[0:len_j]: msg = "Duplicated MMType value for %s %s" % \ (mmtypelist[i].name, mmtypelist[j].name) raise RuntimeError (msg) def writeOutputFile (self): """Store the parse data into the output file. Only header files for C code is supported.""" from c import C cheader = C () # defines part. defines = "" enums = "" for i in self.mmtypelist: defines += cheader.line % (i.name, i.value) for j in i.types: data = MME_SUBTYPES[j] enums += \ cheader.mmtypes % (i.name + data[0], i.name, data[1]) for i in MME_SUBTYPES.iterkeys (): i = MME_SUBTYPES[i] defines += cheader.line % (i[1], i[2]) mmtypes_min_max = "" defines_is = "" for i in MMTYPES_MIN_MAX: mmtypes_min_max += \ cheader.mmtypes_min_max % ("%s_MIN" % i[0], i[1]) mmtypes_min_max += \ cheader.mmtypes_min_max % ("%s_MAX" % i[0], i[2]) defines_is += \ cheader.define_is % (i[0], "%s_MIN" % i[0], "%s_MAX" % i[0]) data = cheader.template.substitute ( {'defines':defines, 'mmtypes':enums, 'mmtypes_min_max':mmtypes_min_max, 'defines_is': defines_is}) if self.outfile: f = open (self.outfile, 'w') f.write (data) f.close () else: print data def parse (self): """Parse file and export.""" try: from parser import parse except: raise RuntimeError ("run yapps on parser.g (package yapps2)") f = open (self.infile, 'r') self.mmtypelist = parse ('parse', f.read()) f.close () self.__check_entries (self.mmtypelist) def process (self): """Parse and write output file.""" self.parse () self.writeOutputFile () def mmtype_dict (self): """Convert the mmtype list got after parsing the file into a dictionary.""" self.parse () mmtypes_dict = {} for i in self.mmtypelist: for j in i.types: subtype = MME_SUBTYPES[j] mmtype_name = "".join ([i.name, subtype[0]]) mmtypes_dict[mmtype_name] = \ int (i.value, 0) + int (subtype[2], 0) return mmtypes_dict