遍历文件夹获取CRC校验码

工作中需要提取车载的CRC校验码,文件数量比较大,手动提取较麻烦,临时用Pyhton写了一个CRC提取的小工具。`

#! /usr/bin/env python
#coding=utf-8
import wx
import os.path
import xlsxwriter
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class MainWindow(wx.Frame):
    def __init__(self,filename=u'CRC'):
        super(MainWindow, self).__init__(None, size=(650,650))
        self.filename=filename
        self.opendir='.'
        self.savedir='.'
        self.traindatafile=''
        self.traindata=''
        self.lcofile=[]
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.CreateInteriorWindowComponents()
        self.CreateExteriorWindowComponents()

    def CreateInteriorWindowComponents(self):
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_READONLY)

    def CreateExteriorWindowComponents(self):
        self.CreateMenu()
        self.SetTitle()

    def SetTitle(self):
        super(MainWindow,self).SetTitle(u'CRC抓取工具')

    def CreateMenu(self):
        #菜单
        Menu=wx.Menu()
        itemLoadCfg=Menu.Append(0,u'加载配置')
        itemSaveAs=Menu.Append(1,u'另存为')
        itemExit=Menu.Append(2,u'退出')

        self.Bind(wx.EVT_MENU,self.OpenFolder,itemLoadCfg)
        self.Bind(wx.EVT_MENU,self.SaveAs,itemSaveAs)
        self.Bind(wx.EVT_MENU,self.Exit,itemExit)

        ATPModuleMenu=wx.Menu()
        itemEvc=ATPModuleMenu.Append(4,u'EVC')
        itemAlm=ATPModuleMenu.Append(5,u'ALM')
        itemTmm=ATPModuleMenu.Append(6,u'TMM')
        itemBtm=ATPModuleMenu.Append(7,u'BTM')
        itemRim=ATPModuleMenu.Append(8,u'RIM')
        itemAutoRim=ATPModuleMenu.Append(9,u'自主RIM')

        self.Bind(wx.EVT_MENU,self.GetEvcCrc,itemEvc)
        self.Bind(wx.EVT_MENU,self.GetAlmCrc,itemAlm)
        self.Bind(wx.EVT_MENU,self.GetTmmCrc,itemTmm)
        self.Bind(wx.EVT_MENU,self.GetBtmCrc,itemBtm)
        self.Bind(wx.EVT_MENU,self.GetRimCrc,itemRim)
        self.Bind(wx.EVT_MENU,self.GetAutoRimCrc,itemAutoRim)

        menuBar=wx.MenuBar()
        menuBar.Append(Menu,u'菜单')
        menuBar.Append(ATPModuleMenu,u'ATP模块')

        self.SetMenuBar(menuBar)

    def GetEvcCrc(self,event):
        #清空
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.lcoInformation=['evccfg_n.lco','evccfg_r.lco',r'\\M_evc\\M_ConfSB\\lco',
        'CRC校验码_EVC模块_主系','CRC校验码_EVC模块_备系',8,8,2]
        self.control.AppendText(u'选择EVC模块!\n')

    def GetAlmCrc(self,event):
        #清空
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.lcoInformation=['almcfg_n.lco','almcfg_r.lco',r'\\M_alm\\M_ConfSB\\lco',
        'CRC校验码_ALM模块_主系','CRC校验码_ALM模块_备系',8,8,2]
        self.control.AppendText(u'选择ALM模块!\n')

    def GetTmmCrc(self,event):
        #清空
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.lcoInformation=['tmmcfg_n.lco','tmmcfg_r.lco',r'\\M_tmm\\M_ConfSB\\lco',
        'CRC校验码_TMM模块_主系','CRC校验码_TMM模块_备系',8,8,2]
        self.control.AppendText(u'选择TMM模块!\n')

    def GetBtmCrc(self,event):
        #清空
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.lcoInformation=['btmCfg_1.lco','btmCfg_2.lco',r'\\M_BTM\\M_confsb\\lco',
        'CRC校验码_BTM模块_主系','CRC校验码_BTM模块_备系',8,8,2]
        self.control.AppendText(u'选择BTM模块!\n')

    def GetRimCrc(self,event):
        #清空
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.lcoInformation=             ['rimcfg_a.lco','rimcfg_b.lco',r'\\M_rim\\M_ConfSB\\lco',
        'CRC校验码_RIM模块_主系','CRC校验码_RIM模块_备系',8,8,2]
        self.control.AppendText(u'选择RIM模块!\n')

    def GetAutoRimCrc(self,event):
        #清空
        self.Train_list=[]
        self.allTrainlist=[]
        self.lcoInformation=[]
        self.lcoInformation=['WLCU_cfg_A.dat','WLCU_cfg_B.dat',r'\\自主RIM配置文件_v\d+_\d+\\CR\w+_\d+_T[12]_v\d+',
                'CRC校验码_自主RIM模块_主系','CRC校验码_自主RIM模块_备系',4,0,0]        
        self.control.AppendText(u'选择自主RIM模块!\n')

    def defaultFolderStyle(self):
        return dict(message=u'选择文件夹', style=wx.DD_DEFAULT_STYLE)

    #打开配置文件夹
    def OpenFolder(self,event):
        dirDialog=wx.DirDialog(self, message=u'选择文件夹',style=wx.DD_DEFAULT_STYLE)
        if dirDialog.ShowModal()==wx.ID_OK:
            self.opendir=dirDialog.GetPath()
            self.control.AppendText(u'配置文件路径:\n %s\n' %self.opendir)
        dirDialog.Destroy()

    #匹配列车型号
    def MatchTrainData(self):
        #筛选出M_evc\M_consb\lco
        train_re=re.compile(r'CR\w+_\d+_T[12]')
        traindata=''.join(train_re.findall(self.traindatafile))
        return traindata

    #是否匹配目标程序文件夹    
    def MatchDestinationFolder(self):
        matchFolder=re.compile(self.lcoInformation[2].encode('gbk'))
        isDesFolder=matchFolder.findall(self.traindatafile)
        return isDesFolder

    #每个字节之间添加一个空格(CRC共八个字节)
    def CrcAddSpace(self,file):
        crclist=[]
        #CRC八个字节
        for i in range(0,self.lcoInformation[5]):
            byteCrc=file.read(1).encode('hex')
            crclist.append(byteCrc)
        return ' '.join(crclist)

     #遍历文件夹并生成list[车型,主系,备系]   
    def SearchCrc(self):
        for lcofile in self.lcofile:
            if self.lcoInformation[0]==lcofile:
                list_n=[]                      
                f_n=open(os.path.join(self.traindatafile,self.lcoInformation[0]),'rb')
                f_n.seek(-self.lcoInformation[6],self.lcoInformation[7])
                #crc_n=f_n.read(8)
                crc_n=self.CrcAddSpace(f_n)
                hex_crc_n=crc_n.upper()
                self.Train_list.append(hex_crc_n)
                f_n.close()
            if self.lcoInformation[1]==lcofile:
                f_r=open(os.path.join(self.traindatafile,self.lcoInformation[1]),'rb')
                f_r.seek(-self.lcoInformation[6],self.lcoInformation[7])
                crc_r=self.CrcAddSpace(f_r)
                hex_crc_r=crc_r.upper()
                self.Train_list.append(hex_crc_r)
                f_r.close()

    #
    def GetCrc(self):
        for root,dirs,files in os.walk(self.opendir.encode('gbk'),False):
            self.traindatafile=root
            if self.MatchDestinationFolder():
                self.Train_list=[self.MatchTrainData()]
                self.lcofile=files
                self.SearchCrc()
                self.allTrainlist.append(self.Train_list)
        return self.allTrainlist

    def WriteExcel(self):
        workbook=xlsxwriter.Workbook(os.path.join(self.savedir,self.filename+'.xlsx'))
        worksheet=workbook.add_worksheet()
        bold=workbook.add_format({'bold':True})
        worksheet.write('A1',u'车型',bold)
        worksheet.set_column('A:A',25)
        worksheet.set_column('B:C',30)
        worksheet.write('B1',self.lcoInformation[3],bold)
        worksheet.write('C1',self.lcoInformation[4],bold)

        row=1
        col=0
        for trainNumber,master,servant in (self.GetCrc()):
            worksheet.write(row,col,trainNumber)
            worksheet.write(row,col+1,master)
            worksheet.write(row,col+2,servant)
            row+=1
        workbook.close()
        self.control.AppendText(u'保存完毕,请查看!\n')

    def SaveAs(self,event):
        dialog=wx.FileDialog(self,message=u'选择文件', defaultDir=self.opendir,wildcard='(*.xlsx)|*.xlsx*')
        if dialog.ShowModal()==wx.ID_OK:
            self.filename=dialog.GetFilename()
            self.savedir=dialog.GetDirectory()
            self.control.AppendText(u'保存路径:\n %s\n' %self.savedir)
        dialog.Destroy()
        self.WriteExcel()

    def Exit(self,event):
        self.Close()

app=wx.App()
frame=MainWindow()
frame.Show()
app.MainLoop()




你可能感兴趣的:(python)