#!/usr/bin/python # -*- coding: UTF-8 -*- import getconn_util from logutil import LogUtil # get_con = getconn_util.get_conn('hive') # print 'jdbc : '+get_con.jdbc_link() # print 'cxoracle : '+get_con.cxoracle_link() # print 'orauser: '******'E:/work_files/logutil_test.log' log = LogUtil(logfile, level='warn') log.info('***** test info ******') log.info('***** test sads ******') log.info('***** test fdfdf ******') log.warn('***** test sdad ******') log.error('***** test error ******')
class File2QRCode(object): D_CONST_MAXSIZE=2331 D_CONST_PREFERSIZE=560 def __init__(self): self._log=LogUtil().logger('core') self._qr=qrcode.QRCode() self._progfunc=None self.clearLastData() def clearLastData(self): self._lastwidth=0 self._lasttotalpack=0 self._lastprefix='' self._lastextname='' self._lastoutpath='' def regProgressFunc(self,func): self._log.info('callback(%s) register'%(func.__name__,)) if callable(func): self._progfunc=func else: self._log.error('callback function error') def quickSplit(self,infile,splitsize=D_CONST_PREFERSIZE,outpath='',prefix='quick',method='png'): if self._fileCheck(infile): if outpath=='': outpath=dirname(infile) self.splitFile(infile, splitsize, outpath, prefix, method) return True return False def splitFile(self,infile,splitsize,outpath,outprefix,outmethod): self._log.info('InFile(%s) SplitSize(%d) OutPath(%s) Prefix(%s) Format(%s)'%(infile,splitsize,outpath,outprefix,outmethod)) if self._fileCheck(infile): self.clearLastData() if outpath=='': self._log.debug('use current dir as output dir') outpath='.' if exists(outpath) and isdir(outpath): pass else: self._log.debug('make output dir') makedirs(outpath) self._lastoutpath=outpath self._lastprefix=outprefix if splitsize<=0 or splitsize>File2QRCode.D_CONST_MAXSIZE: splitsize=File2QRCode.D_CONST_MAXSIZE self._log.warning('SplitSize out of range, use %d'%(splitsize,)) om=str(outmethod).upper() if om=='PNG': image_factory=qrcode.image.pil.PilImage self._lastextname='png' else: image_factory=qrcode.image.svg.SvgImage self._lastextname='svg' totalsize=getsize(infile) self._log.debug('InFile Size:%d'%(totalsize,)) currentcnt=1 self._lasttotalpack=(totalsize+splitsize-1)/splitsize self._log.debug('Package Count:%d'%(self._lasttotalpack,)) self._lastwidth=len(str(self._lasttotalpack)) with open(infile,'rb') as fh: while currentcnt<=self._lasttotalpack: data=fh.read(splitsize) self._qr.add_data(data) img = self._qr.make_image(image_factory=image_factory) outfile=self.getLastFilename(currentcnt) img.save(outfile) self._log.debug('%d/%d InSize:%d OutFile:%s QRVersion:%d'%(currentcnt,self._lasttotalpack,len(data),outfile,self._qr.version)) self._qr.clear() self._qr.version=0 self._fireCallback(currentcnt,self._lasttotalpack) currentcnt+=1 return True return False def getLastFilename(self,idx): self._log.info('Index:%d Width:%d Prefix:%s'%(idx,self._lastwidth,self._lastprefix)) filename=self._genOutfile(self._lastprefix, self._lastextname, idx, self._lastwidth) return join(self._lastoutpath,filename) def _genOutfile(self,prefix,extname,curcnt,width): outstr=prefix strfmt="%0"+str(width)+"d" outstr+=strfmt%(curcnt) outstr+='.'+extname return outstr def _fileCheck(self,infile): if exists(infile) and isfile(infile): return True return False def _fireCallback(self,cur,total): self._log.info('Current:%d Total:%d'%(cur,total)) if self._progfunc: self._progfunc(cur,total)
class XmlReader(object): def __init__(self): self._log = LogUtil().logger("XmlReader") def loadXml(self, filename): self._log.info("Reading XML File:%s" % (filename,)) self._initdata() tagstack = [] for action, elem in etree.iterparse(filename, events=("start", "end")): if action == "start": tag = elem.tag tagstack.append(tag) self.starttag(":".join(tagstack), elem) else: self.endtag(":".join(tagstack), elem) tagstack.pop(-1) def getInfo(self, infoobj): if not isinstance(infoobj, ManageC): return False for func in self._funcs: infoobj.addFunction(func) for var in self._vars: infoobj.addVariable(var) infoobj.setMatrix(self._invars, self._outvars, self._incond, self._outcond) return True def _initdata(self): self._funcs = [] self._funcs.append([]) self._vars = [] self._invars = [] self._outvars = [] self._incond = [] self._outcond = [] def starttag(self, path, elem): if path.startswith("root:function:param"): self._log.debug(path) self._funcs[0].append(elem.attrib) elif path == "root:ex-function": self._log.debug(path) self._funcs.append([]) elif path.startswith("root:ex-function:param"): self._log.debug(path) self._funcs[-1].append(elem.attrib) elif path.startswith("root:ex-var:var"): self._log.debug(path) self._vars.append(elem.attrib) elif path.startswith("root:auto-var:var"): self._log.debug(path) self._vars.append(elem.attrib) elif path.startswith("root:matrix:in-var:var"): self._log.debug(path) self._invars.append(elem.attrib) elif path.startswith("root:matrix:out-var:var"): self._log.debug(path) self._outvars.append(elem.attrib) elif path.startswith("root:matrix:rule"): if path.endswith("in-rule"): self._incond.append([]) elif path.endswith("out-rule"): self._outcond.append([]) if ":in-rule:value" in path: self._log.debug(path) self._incond[-1].append(elem.attrib) elif ":out-rule:value" in path: self._log.debug(path) self._outcond[-1].append(elem.attrib) def endtag(self, path, elem): if path == "root:function": self._log.debug(path) elif path == "root:ex-function": self._log.debug(path) elif path == "root:ex-var": self._log.debug(path) elif path == "root:auto-var": self._log.debug(path) elif path == "root:matrix:in-var": self._log.debug(path) elif path == "root:matrix:out-var": self._log.debug(path) elif path.startswith("root:matrix:rule"): if ":in-rule:value" in path: self._log.debug(path) elif ":out-rule:value" in path: self._log.debug(path)
class Txt2Xml(object): D_FIELDSEP='\t' D_CASTCHAR='(U1*)' D_FLAGSTR1=u'■関数宣言'.encode('utf-8') D_FLAGSTR21=u'■自動変数'.encode('utf-8') D_FLAGSTR22=u'■完了'.encode('utf-8') D_FLAGSTR31=u'■入出力表'.encode('utf-8') D_FLAGSTR32=u'■完了'.encode('utf-8') D_FLAGSTR33=u'入力'.encode('utf-8') D_FLAGSTR34=u'出力'.encode('utf-8') D_FLAGSTR41=u'■外部関数'.encode('utf-8') D_FLAGSTR42=u'■完了'.encode('utf-8') D_FLAGSTR51=u'■外部変数'.encode('utf-8') D_FLAGSTR52=u'■完了'.encode('utf-8') D_MARK1=u'戻り値'.encode('utf-8') D_MARK2=u'○'.encode('utf-8') D_MARK3=u'×'.encode('utf-8') D_MARK4=u'△'.encode('utf-8') D_MARK5=u'-'.encode('utf-8') D_MARK6=u'上記以外'.encode('utf-8') def __init__(self): self._log=LogUtil().logger('Txt2Xml') def loadTxt(self,filename,fileencode): self._log.info('File:%s Encode:%s'%(filename,fileencode)) fh=open(filename,'rU') data=fh.read() fh.close() data8=data.decode(fileencode).encode('utf-8') self.initData() self.splitTxt(data8) self.parseTxt() def toXml(self,filename): root=etree.Element('root') func=etree.SubElement(root,'function') self._xmlFunc(func,self._funcvars[0]) for i in range(len(self._funcvars)-1): func=etree.SubElement(root,'ex-function') self._xmlFunc(func,self._funcvars[i+1]) if len(self._exvardecl)>0: exvar=etree.SubElement(root,'ex-var') for data in self._exvardecl: self._xmlVar(exvar,data) if len(self._vardecl)>0: autovar=etree.SubElement(root,'auto-var') for data in self._vardecl: self._xmlVar(autovar,data) matrix=etree.SubElement(root,'matrix') self._xmlMatrix(matrix) with open(filename,'w') as fh: fh.write(etree.tostring(root, method='xml', xml_declaration=True, pretty_print=True, encoding='utf-8')) def _xmlFunc(self,element,funcdata): for idx,data in enumerate(funcdata): sub=etree.SubElement(element,'param%d'%(idx,)) sub.set('type',data[0]) sub.set('name',data[1]) def _xmlVar(self,element,vardata): subvar=etree.SubElement(element,'var') subvar.set('type',vardata[0]) subvar.set('name',vardata[1]) if len(vardata)==3: subvar.set('value',vardata[2]) elif len(vardata)==5: subvar.set('method',vardata[2]) subvar.set('value',vardata[3]) subvar.set('size',vardata[4]) def _xmlMatrix(self,element): if len(self._invars)>0: invar=etree.SubElement(element,'in-var') for idx,v in enumerate(self._invars): inv=etree.SubElement(invar,'var%d'%(idx,)) inv.set('value',v) if len(self._outvars)>0: outvar=etree.SubElement(element,'out-var') for idx,v in enumerate(self._outvars): outv=etree.SubElement(outvar,'var%d'%(idx,)) outv.set('value',v) for i in range(len(self._incond)): item=etree.SubElement(element,'rule%d'%(i,)) if len(self._incond[i])>0: irule=etree.SubElement(item,'in-rule') for idx,value in enumerate(self._incond[i]): ivalue=etree.SubElement(irule,'value%d'%(idx,)) ivalue.set('value',value) if len(self._outcond[i])>0: orule=etree.SubElement(item,'out-rule') for idx,value in enumerate(self._outcond[i]): ovalue=etree.SubElement(orule,'value%d'%(idx,)) ovalue.set('value',value) def initData(self): # output for splitTxt self._funcs=() self._extvars=() self._autovars=() self._iomatrix=() # output for parseTxt self._funcvars=[] self._exvardecl=[] self._vardecl=[] self._invars=[] self._outvars=[] self._incond=[] self._outcond=[] def splitTxt(self,data8): flag=0 #0:init step=0 flagtable=((self.D_FLAGSTR1,1), (self.D_FLAGSTR21,2), (self.D_FLAGSTR31,3), (self.D_FLAGSTR41,4), (self.D_FLAGSTR51,5)) donetable=[] funcdeclare='' autovars=[] iomatrix=[] extfunc=[] extvars=[] for lineno,line in enumerate(data8.splitlines()): if flag==0: for item in flagtable: if line.startswith(item[0]): self._log.debug('Found Start-tag:%d at line:%d'%(item[1],lineno)) if item[1] not in donetable: flag=item[1] donetable.append(flag) step=0 break else: self._log.warning('Found Duplicated Start-tag:%d'%(item[1],)) if flag==1: # function declare if step==0: step=1 elif step>=1: funcdeclare=line.strip() self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 elif flag==2: # auto vars if step<2: step+=1 elif step>=2: if line.startswith(self.D_FLAGSTR22): self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 else: autovars.append(line.strip()) elif flag==3: # i/o matrix if step==0: step+=1 else: iomatrix.append(line) if self.D_FLAGSTR32 in line: self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 elif flag==4: # extern function if step==0: step+=1 else: if line.startswith(self.D_FLAGSTR42): self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 else: extfunc.append(line.strip()) elif flag==5: # extern vars if step<2: step+=1 else: if line.startswith(self.D_FLAGSTR52): self._log.debug('Found End-tag:%d at line:%d'%(flag,lineno)) flag=0 else: extvars.append(line.strip()) self._autovars=tuple(autovars) self._iomatrix=tuple(iomatrix) funcs=[] funcs.append(funcdeclare) funcs.extend(extfunc) self._funcs=tuple(funcs) self._extvars=tuple(extvars) def parseTxt(self): for func in self._funcs: self._funcvars.append(self.parseFuncDecl(func)) self.parseExtVars() self.parseAutoVars() self.parseIOMatrix() def parseFuncDecl(self,fdec): self._log.info('Parse Function Declaration:%s'%(fdec,)) pos_l=fdec.find('(') pos_r=fdec.rfind(')') outdata=[] if pos_l<0 or pos_r!=len(fdec)-1: self._log.warning('cannot find proper "(" and ")"') else: outdata.append(self._parseFuncParam(fdec[:pos_l])) for param in fdec[pos_l+1:pos_r].split(','): outdata.append(self._parseFuncParam(param)) return tuple(outdata) def _parseFuncParam(self,data): self._log.debug('Parse Function Parameter:%s'%(data,)) ptype='' pname='' sdata=data.strip() rpat=re.compile(r'(?P<type>.+[^a-zA-Z0-9_])(?P<name>[a-zA-Z0-9_]+)') rret=rpat.search(sdata) if rret: ptype=rret.group('type').strip() pname=rret.group('name').strip() else: ptype=sdata.strip() pname=ptype return tuple((ptype,pname)) def parseExtVars(self): for line in self._extvars: self._log.debug('Parse External Parameter:%s'%(line,)) parts=line.split(self.D_FIELDSEP) if len(parts)<2: self._log.warning('Input Check Error, Skip') else: self._exvardecl.append((parts[0],parts[1])) def parseAutoVars(self): for line in self._autovars: self._log.debug('Parse Internal Parameter:%s'%(line,)) parts=line.split(self.D_FIELDSEP) if len(parts)<2: self._log.warning('Input Check Error, Skip') else: if len(parts)==2: parts.append('0') if parts[2]=='memset': if len(parts)<4: parts.append('') if parts[3]=='': parts[3]='0' if len(parts)<5: parts.append('') if parts[4]=='': parts[4]='sizeof(%s)'%(parts[0],) self._vardecl.append(tuple(parts[0:5])) elif parts[2]=='memcpy': if len(parts)<4: parts.append('') if parts[3]=='': self._log.warning('No Source Buffer Found, Change To memset') parts[2]='memset' parts[3]='0' if len(parts)<5: parts.append('') if parts[4]=='': parts[4]='sizeof(%s)'%(parts[0],) self._vardecl.append(tuple(parts[0:5])) else: self._vardecl.append(tuple(parts[0:3])) def parseIOMatrix(self): inidx,outidx=0,0 parts=self._iomatrix[0].split(self.D_FIELDSEP) if self.D_FLAGSTR34 in parts: inidx=parts.index(self.D_FLAGSTR34) else: self._log.error('No Output Found') return parts=self._iomatrix[-1].split(self.D_FIELDSEP) if self.D_FLAGSTR32 in parts: outidx=parts.index(self.D_FLAGSTR32) else: self._log.error('Cannot Figure Out Count Of In/Out Vars') return parts=self._iomatrix[1].split(self.D_FIELDSEP) if len(parts)<=outidx: self._log.error('Not Enough In/Out Vars') return else: if self.D_MARK1 in parts: parts[parts.index(self.D_MARK1)]='__RET__' for i in range(inidx): self._invars.append(parts[i].strip()) for i in range(outidx-inidx+1): self._outvars.append(parts[i+inidx].strip()) for line in self._iomatrix[2:-1]: self._log.debug('Parse I/O Matrix:%s'%(line.decode('utf-8'),)) parts=line.split(self.D_FIELDSEP) for markfrom,markto in ((self.D_MARK2,'__CALL__'), (self.D_MARK3,'__NOCALL__'), (self.D_MARK4,'__NOTCARE__'), (self.D_MARK5,'__NOACT__'), (self.D_MARK6,'__ELSE__')): while markfrom in parts: parts[parts.index(markfrom)]=markto self._incond.append([]) self._outcond.append([]) for i in range(inidx): self._incond[-1].append(parts[i].strip()) for i in range(outidx-inidx+1): self._outcond[-1].append(parts[i+inidx].strip())
class F2QToolPanel(wx.Panel): def __init__(self,parent,id=wx.ID_ANY,pos=wx.DefaultPosition,size=wx.DefaultSize,style=wx.TAB_TRAVERSAL,name=wx.PanelNameStr): super(F2QToolPanel,self).__init__(parent,id,pos,size,style,name) self._log=LogUtil().logger('ui') self._tool=f2q.File2QRCode() self._tool.regProgressFunc(self.OnProgress) self.SetupUi() def SetupUi(self): sizer=wx.GridBagSizer() label1=wx.StaticText(self,label='Input File') self._txt1=wx.TextCtrl(self,value='') btn1=wx.Button(self,label='...') label2=wx.StaticText(self,label='Output Path') self._txt2=wx.TextCtrl(self,value='') btn2=wx.Button(self,label='...') label31=wx.StaticText(self,label='Split Size') label32=wx.StaticText(self,label='Format') self._combo31=wx.ComboBox(self,choices=('14','26','42','62', '84','106','122','152', '180','213','251','287', '331','362','412','450', '504','560','624','666', '711','779','857','911', '997','1059','1125','1190', '1264','1370','1452','1538', '1628','1722','1809','1911', '1989','2099','2213','2331') ,value='560',style=wx.CB_READONLY) self._combo32=wx.ComboBox(self,choices=('png','svg'),value='png',style=wx.CB_READONLY) label4=wx.StaticText(self,label='Prefix') self._txt4=wx.TextCtrl(self,value='quick') btn4=wx.Button(self,label='Split') sizer.Add(label1,(0,0),flag=wx.EXPAND) sizer.Add(self._txt1,(0,1),(1,3),flag=wx.EXPAND) sizer.Add(btn1,(0,4),flag=wx.EXPAND) sizer.Add(label2,(1,0),flag=wx.EXPAND) sizer.Add(self._txt2,(1,1),(1,3),flag=wx.EXPAND) sizer.Add(btn2,(1,4),flag=wx.EXPAND) sizer.Add(label31,(2,0),flag=wx.EXPAND) sizer.Add(self._combo31,(2,1),flag=wx.EXPAND) sizer.Add(label32,(2,3),flag=wx.EXPAND) sizer.Add(self._combo32,(2,4),flag=wx.EXPAND) sizer.Add(label4,(3,0),flag=wx.EXPAND) sizer.Add(self._txt4,(3,1),(1,3),flag=wx.EXPAND) sizer.Add(btn4,(3,4),flag=wx.EXPAND) sizer.SetFlexibleDirection(wx.HORIZONTAL) sizer.AddGrowableCol(2) self.SetSizer(sizer) self.Bind(wx.EVT_BUTTON,self.OnInBtn,btn1) self.Bind(wx.EVT_BUTTON,self.OnOutBtn,btn2) self.Bind(wx.EVT_BUTTON,self.OnActBtn,btn4) def OnProgress(self,curcnt,totalcnt): self._log.info('Callback(%d/%d)'%(curcnt,totalcnt)) self.GetParent().SetStatusText("%d/%d"%(curcnt,totalcnt),2) if curcnt>=totalcnt: msgbox=wx.MessageDialog(self,message='Split Done!',style=wx.OK|wx.CENTER) msgbox.ShowModal() def OnInBtn(self,event): self._log.info('ButtonPressed') infile=self._txt1.GetValue() if infile: pathname,filename=os.path.split(infile) fd=wx.FileDialog(self,message='Select file to split...',defaultDir=pathname,defaultFile=filename) else: fd=wx.FileDialog(self,message='Select file to split...') if fd.ShowModal()==wx.ID_OK: fullname=fd.GetPath() self._log.debug('FileDialog:%s'%(fullname,)) pathname,filename=os.path.split(fullname) filesize=os.path.getsize(fullname) self._txt1.SetValue(fullname) self._txt2.SetValue(pathname) self.GetParent().SetStatusText(filename,0) self.GetParent().SetStatusText("%d Bytes"%(filesize,),1) self.GetParent().SetStatusText("",2) def OnOutBtn(self,event): self._log.info('ButtonPressed') outpath=self._txt2.GetValue() or '.' fd=wx.DirDialog(self,message='Select output path...',defaultPath=outpath) if fd.ShowModal()==wx.ID_OK: pathname=fd.GetPath() self._log.debug('DirDialog:%s'%(pathname,)) self._txt2.SetValue(pathname) def OnActBtn(self,event): self._log.info('ButtonPressed') infile=self._txt1.GetValue() outpath=self._txt2.GetValue() prefix=self._txt4.GetValue() splitsize=int(self._combo31.GetValue()) method=self._combo32.GetValue() self._log.debug('InFile:%s SplitSize:%d OutPath:%s Prefix:%s Format:%s'%(infile,splitsize,outpath,prefix,method)) self._tool.quickSplit(infile,splitsize,outpath,prefix,method)