#Copyright (c) 2006-2007 SunMedia technology (Chengdu) co. Ltd. 
#All right reserved
#Author:jin.hu
#date:2009/09/15

import re
import os
import sys
import time
import string
import codecs

configLog = '''<Rectangle Canvas.Left="%d" Canvas.Top="%d" Fill="red" Width="60" Height="%d"/>
					<TextBlock Canvas.Left="%d" Canvas.Top="%d" Width="60" Height="50" Text="%d"/>
'''
databaseLog = '''<Rectangle Canvas.Left="%d" Canvas.Top="%d" Fill="Green" Width="60" Height="%d"/>
					<TextBlock Canvas.Left="%d" Canvas.Top="%d" Width="60" Height="80" Text="%d"/>
'''
binFileLog = '''<Rectangle Canvas.Left="%d" Canvas.Top="%d" Fill="Blue" Width="60" Height="%d"/>
					<TextBlock Canvas.Left="%d" Canvas.Top="%d" Width="60" Height="80" Text="%d"/>
'''
outputLog = '''<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
	<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
		<Grid HorizontalAlignment="left" VerticalAlignment="top">
			<Grid.ColumnDefinitions>
				<ColumnDefinition Width="Auto"/>
			</Grid.ColumnDefinitions>
            		<Grid.RowDefinitions>
				<RowDefinition Height="Auto"/>
			</Grid.RowDefinitions>

			<StackPanel Orientation="Horizontal" Grid.Row="0" HorizontalAlignment="left" VerticalAlignment="Top" Margin="95,35,0,20">
				<Border VerticalAlignment="Center">
					<Line Stroke="Red" X2="28" StrokeThickness="2"/>
				</Border>
				<TextBlock Margin="3">Config Size</TextBlock>
			</StackPanel>
			<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="left" VerticalAlignment="Top" Margin="225,35,0,20">
				<Border VerticalAlignment="Center">
					<Line Stroke="Green" X2="28" StrokeThickness="2"/>
				</Border>
				<TextBlock Margin="3">DataBase Size</TextBlock>
			</StackPanel>
			<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="left" VerticalAlignment="Top" Margin="365,35,0,20">
				<Border VerticalAlignment="Center">
					<Line Stroke="Blue" X2="28" StrokeThickness="2"/>
				</Border>
				<TextBlock Margin="3">Bin Size</TextBlock>
			</StackPanel>

            		<Border Grid.Row="0"  Margin = "10,10,0,0" TextBlock.FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="gray" BorderThickness="2">
				<StackPanel>
					<TextBlock Foreground='green' FontSize="30"  TextAlignment="Center">Bin Information</TextBlock>
                    			<Grid>
						<Grid.ColumnDefinitions>
							<ColumnDefinition Width="Auto"/>
							<ColumnDefinition Width="Auto"/>
						</Grid.ColumnDefinitions>
						<Grid.RowDefinitions>
							<RowDefinition Height="Auto"/>
							<RowDefinition Height="Auto"/>
						</Grid.RowDefinitions>
                        	<StackPanel>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="20000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="18000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="15000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="12000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="10000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="8000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="7000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="5000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="3000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="2000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="1000"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="800"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="700"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="500"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="300"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="200"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="100"/></Border>
					<Border Width="80" Height="30">
						<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="0"/></Border>
				</StackPanel>

                        <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
				<Border Width="200" Height="200">
					<TextBlock Foreground="Black"  Padding="40,0,0,4" Text = 'Sat' FontSize="22" VerticalAlignment="top" HorizontalAlignment="left">
					</TextBlock>
				</Border>
				<Border Width="200" Height="200">
					<TextBlock Foreground="Black"  Padding="40,0,0,4" Text = 'TP' FontSize="22" VerticalAlignment="top" HorizontalAlignment="left">
					</TextBlock>
				</Border>
				<Border Width="200" Height="200">
					<TextBlock Foreground="Black"  Padding="40,0,0,4" Text = 'TV' FontSize="22" VerticalAlignment="top" HorizontalAlignment="left">
					</TextBlock>
				</Border>
				<Border Width="200" Height="200">
					<TextBlock Foreground="Black"  Padding="40,0,0,4" Text = 'Radio' FontSize="22" VerticalAlignment="top" HorizontalAlignment="left">
					</TextBlock>
				</Border>
				<Border Width="200" Height="200">
					<TextBlock Foreground="Black"  Padding="40,0,0,4" Text = 'Size(k)' FontSize="22" VerticalAlignment="top" HorizontalAlignment="left">
					</TextBlock>
				</Border>
			</StackPanel>

                        <Canvas Grid.Row="0" Grid.Column="1" Margin="0, 18, 0, 0" >
				<Line Canvas.Top="0" X2="1000" Stroke="Gray"></Line>
   				<Line Canvas.Top="30" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="60" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="90" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="120" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="150" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="180" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="210" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="240" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="270" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="300" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="330" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="360" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="390" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="420" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="450" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="480" X2="1000" Stroke="Gray"></Line>
				<Line Canvas.Top="510" X2="1000" Stroke="Gray"></Line>
			</Canvas>

                        <Canvas Grid.Row="0" Grid.Column="1">
				<Line Y1 ="12"  Y2="530" Stroke="Gray"></Line>
				<Line Canvas.Top="522" Canvas.Left ="0" Y2="0" Stroke="Gray"></Line>
				<Line Canvas.Top="522" Canvas.Left ="200" Y2="5" Stroke="Gray"></Line>
				<Line Canvas.Top="522" Canvas.Left ="400" Y2="5" Stroke="Gray"></Line>
				<Line Canvas.Top="522" Canvas.Left ="600" Y2="5" Stroke="Gray"></Line>
				<Line Canvas.Top="522" Canvas.Left ="800" Y2="5" Stroke="Gray"></Line>
			</Canvas>

      <Canvas Grid.Row="0" Grid.Column="1" Margin="0, 18, 0, 0">
				%s
			</Canvas>
		</Grid>
	</StackPanel>
</Border> 
</Grid>
	</ScrollViewer>
</Page>
'''

codePageData = ["ascii", "big5", "cp1256", "iso8859_2","iso8859_3", "iso8859_4", "iso8859_5","iso8859_7", 
    "iso8859_8", "iso8859_9", "iso8859_10", "iso8859_13", "iso8859_14", "iso8859_15", "iso8859_16","cp932", "gb2312"]

class ParserBinFile:
    def __init__(self, binFilePath):
        self.filePath = binFilePath
        self.fp = 0
        self.netNumber = 0
        self.TPNumber = 0
        self.serviceNumber = 0
        self.tvServiceNumber = 0
        self.radioServiceNumber = 0

        self.byteWidth = {}
        self.elementName = []
        self.elementType = {}

        self.bitName = {}
        self.bitNumber = {}

        self.binFileData = ''
        self.fileSeek = 0
        self.binDataPostion = 0

        self.satelliteData = []
        self.TPData = []
        self.serviceData = []
    
        self.platForm = -1
        self.binVersion = 0

        #self.fp1 = open('E:\\DBTool111\\s1\\fff.h','w')
    
    def setCodePage (self, codePageIndex):
     	self.codePage = codePageData[codePageIndex]
    def setByteWidth(self, byteWidth):
        self.byteWidth = byteWidth
    def setElementName(self, elementName):
        self.elementName = elementName
    def setType(self, elementType):
        self.elementType = elementType
    def setBitName(self, bitName):
        self.bitName = bitName
    def setBitWidth(self, bitWidth):
        self.bitNumber = bitWidth
    def setStruct(self, elementStruct):
        return
    def resetData(self):
        self.byteWidth = {}
        self.elementName = []
        self.elementType = {}
        self.bitName = {}
        self.bitNumber = {}
    
    def openBinFile(self):
        self.fp = open(self.filePath, 'rb')
        self.binFileData = self.fp.read()
        self.fp.close()
    
    def getBinFileSize(self):
        binFileSize = len(self.binFileData) - self.binDataPostion
        if binFileSize%(1024*64) != 0:
            binFileSize += (1024*64) - binFileSize%(1024*64)
       
        self.binFileData = self.binFileData[self.binDataPostion:]
        self.getStructNumber()
        return binFileSize
    
    def getBinFileType(self, defaultPath):
        if self.binDataPostion != 0:
            defaultPath += '\\sourceCode.bin'
            sourceFp = open(defaultPath, 'wb')
            sourceCodeData = self.binFileData[:self.binDataPostion]
            sourceFp.write(sourceCodeData)
            sourceFp.close()
        return self.binDataPostion

    def getPlatForm(self):
        db15Count = self.binFileData.count('db15S')
        db02Count = self.binFileData.count('db02S')
        if db15Count == 0 and db02Count == 0:
            return -1
        elif db15Count == 0 and db02Count == 1:
            self.binDataPostion = self.binFileData.find('db02S')
            self.platForm = 1
            self.binVersion = ord(self.binFileData[self.binDataPostion + 5])
            return 1
        elif db15Count == 1 and db02Count == 0:
            self.binDataPostion = self.binFileData.find('db15S')
            self.binVersion = ord(self.binFileData[self.binDataPostion + 5])
            self.platForm = 0
            return 0
        else:
            self.getSpecialFilePlatForm()
    
    def getSpecialFilePlatForm(self):
        for i in range(len(self.binFileData), 64):
            platFormValue = ''
            for j in range(5):
                platFormValue += chr(ord(self.binFileData[i+j]))
            if platFormValue == 'db15S':
                self.binDataPostion = i
                self.binVersion = ord(self.binFileData[self.binDataPostion + 5])
                self.platForm = 0
                return 0
            elif platFormValue == 'db02S':
                self.binDataPostion = i
                self.binVersion = ord(self.binFileData[self.binDataPostion + 5])
                self.platForm = 1
                return 1
        return None
    
    def getStructNumber(self):
        self.TPNumber = (ord(self.binFileData[11])<<24) + (ord(self.binFileData[10])<<16) + \
            (ord(self.binFileData[9])<<8) + (ord(self.binFileData[8]))
        self.tvServiceNumber = (ord(self.binFileData[15])<<24) + (ord(self.binFileData[14])<<16) + \
            (ord(self.binFileData[13])<<8) + (ord(self.binFileData[12]))
        self.radioServiceNumber = (ord(self.binFileData[19])<<24) + (ord(self.binFileData[18])<<16) + \
            (ord(self.binFileData[17])<<8) + (ord(self.binFileData[16]))
        self.serviceNumber = self.tvServiceNumber + self.radioServiceNumber
        self.netNumber = (ord(self.binFileData[23])<<24) + (ord(self.binFileData[22])<<16) + \
            (ord(self.binFileData[21])<<8) + (ord(self.binFileData[20]))
        self.fileSeek = 24
    
    def getTVCount(self):
        return self.tvServiceNumber

    def getNetData(self):
        netByteWidth = 0
        elementCount = len(self.elementName)
        elementIndex = 0
        while elementIndex < elementCount:
            netByteWidth += self.byteWidth[self.elementName[elementIndex]]
            elementIndex = elementIndex + 1
        i = 0
        while i < self.netNumber:
            netData = self.binFileData[self.fileSeek:self.fileSeek+netByteWidth]
            self.satelliteData.append(self.formatStructData(netData))
            self.fileSeek += netByteWidth
            i += 1
        return self.satelliteData
    
    def getTPData(self):
        tpByteWidth = 0
        elementCount = len(self.elementName)
        elementIndex = 0
        while elementIndex < elementCount:
            tpByteWidth += self.byteWidth[self.elementName[elementIndex]]
            elementIndex = elementIndex + 1
        i = 0
        while i < self.TPNumber:
            tpData = self.binFileData[self.fileSeek:self.fileSeek+tpByteWidth]
            self.TPData.append(self.formatStructData(tpData))
            self.fileSeek += tpByteWidth
            i += 1
        return self.TPData
    def getServiceData(self):
        serviceByteWidth = 0
        elementCount = len(self.elementName)
        elementIndex = 0
        while elementIndex < elementCount:
            serviceByteWidth += self.byteWidth[self.elementName[elementIndex]]
            elementIndex = elementIndex + 1
        i = 0
        while i < self.serviceNumber:
            netData = self.binFileData[self.fileSeek:self.fileSeek+serviceByteWidth]
            self.serviceData.append(self.formatStructData(netData))
            self.fileSeek += serviceByteWidth
            i += 1
        return self.serviceData
    
    def formatStructData(self, stringData):
        structValue = []
        index = 0
        elementCount = len(self.elementName)
        elementIndex = 0
        while elementIndex < elementCount:
            element = self.elementName[elementIndex]
            elementValue = stringData[index:index+self.byteWidth[element]]
            formatValue = ''
            if self.elementType[element].find('char') != -1 and self.byteWidth[element] != 1:
                if self.binVersion == 1:
                    newElementValue = ''
                    try:
                        if self.platForm == 0:
                            newElementValue = unicode(elementValue, "UTF-8")
                        else:
                            newElementValue = unicode(elementValue, self.codePage)
                    except:
                        structValue.append(elementValue)
                    structValue.append(newElementValue)
                else:
                    structValue.append(elementValue) 
            else:
                if self.bitName.has_key(element):
                    formatBitStruct = self.formatBitValue(element, elementValue)
                    i = 0
                    while i < len(formatBitStruct):
                        singleBitStruct = formatBitStruct[i]
                        structValue.append(singleBitStruct)
                        i += 1
                else:
                    tempValue = 0
                    i = 0
                    while i < self.byteWidth[element]:
                        tempValue += ord(elementValue[i])<<(i*8)
                        i += 1
                    structValue.append(str(tempValue))
            index += self.byteWidth[element]
            elementIndex += 1
        return structValue
    
    def formatBitValue(self, memberName, memberValue):
        formatBitStruct = []
        bitName = self.bitName[memberName]
        bitNumber = self.bitNumber[memberName]
        actualValue = 0
        valueIndex = 0
        while valueIndex < self.byteWidth[memberName]:
            actualValue |= ord(memberValue[valueIndex])<<(valueIndex*8)
            valueIndex += 1
        bitIndex = 0
        i = 0
        while i < len(bitName):
            moveNumber = 0
            tempValue = 0
            j = 0
            while j < bitIndex:
                moveNumber += bitNumber[j]
                j += 1
            tempValue = actualValue>>moveNumber&(pow(2, bitNumber[i])-1)
            bitIndex += 1
            formatBitStruct.append(str(tempValue))
            i += 1
        return formatBitStruct

class ExportBinFile:
    def __init__(self, binFilePath):
        self.filePath = binFilePath
        self.fp = 0
        self.byteWidth = {}
        self.elementName = []
        self.elementType = {}

        self.bitName = {}
        self.bitNumber = {}

        self.DataDict = {}
        self.platForm = ''
        self.binFileType = 0
        self.binFileSize = 0
        self.sourceCodeSize = 0
        self.binTotalSize = 0

        self.codePage = ''

        #self.fp1 = open('E:\\DBTool111\\SystemTest\\fff.h','w')

        self.configLog = []
        self.databaseLog = []
        self.binFileLog = []
        self.rowValue = [20000, 18000, 15000, 12000, 10000, 8000, 7000, 5000, 3000, 2000, 1000, 800, 700, 500, 300, 200, 100, 0]
        
    def openBinFile(self, defaultPath):
        self.fp = open(self.filePath, 'wb')
        defaultPath += '\\sourceCode.bin'
        if self.binFileType == 1 and os.path.exists(defaultPath) == True:
            sourceFp = open(defaultPath, 'rb')
            sourceCodeData = sourceFp.read()
            self.sourceCodeSize = len(sourceCodeData)
            sourceFp.close()
            os.remove(defaultPath)
            self.fp.write(sourceCodeData)
        for i in range(24):
            self.fp.write(chr(0))
    def colseBinFile(self, configSize, binTotalSize):
        self.fp.seek(binTotalSize)
        for i in range(configSize - binTotalSize):
            self.fp.write(chr(255))
        self.fp.close()
    def setPlatform(self, platform):
        if platform == 0:
            self.platForm = 'db15S'
        elif platform == 1:
            self.platForm = 'db02S'
    def setCodePage (self, codePageIndex):
     	self.codePage = codePageData[codePageIndex]
    def setBinFileProperty(self, fileType):
        self.binFileType = fileType
    def setByteWidth(self, byteWidth):
        self.byteWidth = byteWidth
    def setElementName(self, elementName):
        self.elementName = elementName
    def setType(self, elementType):
        self.elementType = elementType
    def setBitName(self, bitName):
        self.bitName = bitName
    def setBitWidth(self, bitWidth):
        self.bitNumber = bitWidth
    def setStruct(self, elementStruct):
        return
    def resetData(self):
        self.byteWidth = {}
        self.elementName = []
        self.elementType = {}
        self.bitName = {}
        self.bitNumber = {}

    def setDictItem(self, key , value):
        self.DataDict[key] = value
    def resetValue(self):
        self.DataDict = {}

    def writeHead(self, headData):
        self.fp.seek(self.sourceCodeSize)
        for i in range(len(self.platForm)):
            self.fp.write(self.platForm[i])
        self.fp.write(chr(1&0xff))
        for i in range(2):
            self.fp.write(chr(0&0xff))
        for data in headData:
            self.fp.write(chr(data&0xff))
            self.fp.write(chr(data>>8&0xff))
            self.fp.write(chr(data>>16&0xff))
            self.fp.write(chr(data>>24&0xff))
    
    def writeData(self):
        for element in self.elementName:
            if self.elementType[element].find('char') != -1 and self.byteWidth[element] != 1:
                newStringValue = ''
                newFormatValue = ''
                try:
                    if self.platForm == 'db15S':
                        newStringValue = self.DataDict[element].encode('UTF-8')
                    else:
                        newStringValue = self.DataDict[element].encode(self.codePage)
                except:
                    newFormatValue = '?'
                else:
                    newFormatValue = newStringValue
                stringValue = self.formatString(newFormatValue, self.byteWidth[element])
                self.fp.write(stringValue)
            else:
                if self.bitName.has_key(element):
                    self.writeBitValue(element)
                else:
                    dataValue = self.formatData(self.DataDict[element])
                    bitWidth = 0
                    for i in range(self.byteWidth[element]):
                        self.fp.write(chr(dataValue>>bitWidth&0xff))
                        bitWidth += 8
    
    def writeBitValue(self, memberName):
        bitName = self.bitName[memberName]
        bitNumber = self.bitNumber[memberName]
        dataValue = 0
        bitIndex = 0
        for singleBitName in bitName:
            moveNumber = 0
            for i in range(bitIndex):
                moveNumber += bitNumber[i]
            dataValue |= self.formatData(self.DataDict[singleBitName])<<moveNumber
            bitIndex += 1
        bitWidth = 0
        for j in range(self.byteWidth[memberName]):
            self.fp.write(chr(dataValue>>bitWidth&0xff))
            bitWidth += 8
      
    def formatString(self, formatValue, formatBitWidth):
        charFlag = 0
        for i in range(len(formatValue)):
            if formatValue[i] != ' ':
                charFlag = i
        formatValue = formatValue[:charFlag+1]
        if len(formatValue) == formatBitWidth:
            formatValue = formatValue
        elif len(formatValue) > formatBitWidth:
            formatValue = formatValue[0:formatBitWidth]
        else:
            for i in range(formatBitWidth-len(formatValue)):
                formatValue += chr(0)
        return formatValue

    def formatData(self, dataValue):
        if dataValue == 'NULL':
            return 0x00
        if dataValue == 'Unknown':
            return 0xff
        if dataValue == '':
            return 1
        try:
            base = 10
            if dataValue.find('0x') != -1:
                base = 16
            return int(dataValue, base)
        except Exception:
            return 1
    
    def getPoleHeight(self, value):
        heightNumber = 0
        for i in range(len(self.rowValue)):
            if value >= self.rowValue[i]:
                heightNumber = len(self.rowValue) - i - 1
                break
        rowHeight = ((heightNumber)*30.0 + float((value-self.rowValue[i]))/float((self.rowValue[i-1]-self.rowValue[i]))*30)
        if rowHeight > 0 and rowHeight < 1:
            rowHeight = 1
        return rowHeight

    def getLogData(self, net, tp, tv, radio, size, formatString, logList, startLeft):
        totalHeight = 510
    	netHeight = self.getPoleHeight(net)
        netLog = formatString%(startLeft, totalHeight-netHeight, netHeight, startLeft, totalHeight-netHeight, net)
        logList.append(netLog)
        tpHeight = self.getPoleHeight(tp)
        tpLog = formatString%(startLeft+200, totalHeight-tpHeight, tpHeight, startLeft+200, totalHeight-tpHeight, tp)
        logList.append(tpLog)
        tvHeight = self.getPoleHeight(tv)
        tvLog = formatString%(startLeft+400, totalHeight-tvHeight, tvHeight, startLeft+400, totalHeight-tvHeight, tv)
        logList.append(tvLog)
        radioHeight = self.getPoleHeight(radio)
        radioLog = formatString%(startLeft+600, totalHeight-radioHeight, radioHeight, startLeft+600, totalHeight-radioHeight, radio)
        logList.append(radioLog)
        sizeHeight = self.getPoleHeight(size)
        sizeLog = formatString%(startLeft+800, totalHeight-sizeHeight, sizeHeight, startLeft+800, totalHeight-sizeHeight, size)
        logList.append(sizeLog)


    def setConfigData(self, net, tp, tv, radio, size):
        self.getLogData(net, tp, tv, radio, size/1024, configLog, self.configLog, 10)
    def setDatabaseData(self, net, tp, tv, radio, size):
    	self.getLogData(net, tp, tv, radio, size/1024, databaseLog, self.databaseLog, 70) 	
    def setBinData(self, net, tp, tv, radio, size):
        self.getLogData(net, tp, tv, radio, size/1024, binFileLog, self.binFileLog, 130)
    def writeBinLog(self):
        logData = ''
        for i in range(5):
            logData += self.configLog[i]
            logData += self.databaseLog[i]
            logData += self.binFileLog[i]
        xmalFilePath = self.filePath.replace('.bin', '.xaml')
        fp = open(xmalFilePath, 'w')
        fp.write(outputLog%(logData))
        fp.close()
        return xmalFilePath

def testbin():
    c = ParserBinFile('E:\db.bin')
    c.openBinFile()
    c.byteWidth = {'ucDiseqcType': 1, 'ucDiseqcPort': 1, 'ucFlag': 1, 'uiLNBFreq': 4, 'iLongitude': 4, 'netreserved': 1, 'netreserved_future_use': 2, 'ucInUsed': 1, 'ucPosition': 1, 'szNetName[16]': 16}
    c.elementName = ['ucFlag', 'ucDiseqcType','ucDiseqcPort', 'ucPosition', 'szNetName[16]', 'iLongitude', 'uiLNBFreq', 'ucInUsed', 'netreserved', 'netreserved_future_use']
    c.elementType = {'ucDiseqcType': 'unsigned char', 'ucDiseqcPort': 'unsigned char', 'ucFlag': 'unsigned char', 'uiLNBFreq': 'unsigned int', 'iLongitude': 'unsigned int', 'netreserved': 'unsigned char', 'netreserved_future_use': 'unsigned short', 'ucInUsed': 'unsigned char', 'ucPosition': 'unsigned char', 'szNetName[16]': 'unsigned char'}
    netData = c.getNetData()
    for net in netData:
        print net
    c.byteWidth = {'usFreqM': 2, 'ucInUsed': 1, 'ucFlag': 1, 'ucSkew': 1, 'szTpName[16]': 16, 'usTpIndex': 2, 'usSymRateK': 2, 'tpreserved_future_use': 2, 'usSatIndex': 2, 'tpreserved[2]': 2, 'ucVCRate': 1}
    c.elementName = ['szTpName[16]', 'usFreqM', 'usSymRateK', 'usSatIndex', 'usTpIndex', 'ucSkew', 'ucVCRate', 'ucFlag', 'ucInUsed', 'tpreserved[2]', 'tpreserved_future_use']
    c.elementType = {'usFreqM': 'unsigned short\t', 'ucInUsed': 'unsigned char\t', 'ucFlag': 'unsigned char\t', 'ucSkew': 'unsigned char\t', 'szTpName[16]': 'unsigned char\t', 'usTpIndex': 'unsigned short\t', 'usSymRateK': 'unsigned short\t', 'tpreserved_future_use': 'unsigned short', 'usSatIndex': 'unsigned short\t', 'tpreserved[2]': 'unsigned char', 'ucVCRate': 'unsigned char\t'}
    tpData = c.getTPData()
    for tp in tpData:
        print tp
    c.byteWidth = {'usTsId': 2, 'serreserved[2]': 2, 'usProgNo':2, 'usServiceId': 2, 'stAudioPid1': 8, 'szProgName[40]': 40, 'stVideoPid': 8, 'usOrigNetId': 2, 'usTpIndex': 2, 'usPcrPid': 2, 'serreserved_future_use': 2, 'ucInUsed': 1, 'usServiceType': 2, 'usCASystemID': 2, 'usPMTPid': 2, 'stAudioPid2': 8}
    c.elementName = ['ucInUsed', 'szProgName[41]', 'usProgNo','usTpIndex', 'usServiceId', 'usTsId', 'usOrigNetId', 'stVideoPid', 'stAudioPid[2]', 'usPcrPid', 'usPMTPid', 'usCASystemID', 'serreserved_future_use']
    c.elementType = {'usTsId': 'unsigned short', 'usProgNo':'unsigned short','serreserved[2]': 'unsigned char', 'usServiceId': 'unsigned short', 'stAudioPid1': 'DB_PID_INFO', 'szProgName[40]': 'char         ', 'stVideoPid': 'DB_PID_INFO', 'usOrigNetId': 'unsigned short', 'usTpIndex': 'unsigned short', 'usPcrPid': 'unsigned short', 'serreserved_future_use': 'unsigned short', 'ucInUsed': 'unsigned char', 'usServiceType': 'unsigned short', 'usCASystemID': 'unsigned short', 'usPMTPid': 'unsigned short', 'stAudioPid2': 'DB_PID_INFO'}
    #serviceData = c.getServiceData()
    #for ser in serviceData:
        #print ser
    print c.netNumber
    print c.TPNumber
    print c.serviceNumber

if __name__ == '__main__':
    testbin()