sqs8.py
Go to the documentation of this file.
00001 #!c:/python26/python.exe
00002 import sys,rp
00003 
00004 def process(data,cols,col,ope,val):
00005     ret=False
00006     indexes=cols[col]
00007     data=data[indexes[0]:indexes[1]]
00008     #print data,ope,val
00009     if isinstance(val,int):
00010         exec("ret=("+data+ope+str(val)+")")
00011     else:
00012         if ope.upper()=='LIKE':
00013             if val.startswith('%'):
00014                 ret=data.endswith(val[1:])
00015             elif val.endswith('%'):
00016                 ret=data.startswith(val[:-1])
00017             else:
00018                 ret=(data.find(val)>-1)
00019         elif ope=='=':
00020             ret=(data == val)            
00021     return ret
00022 
00023 rule=['option norun',            #norun, means that lines of code 
00024                                  #will not be interpreted while parsing string
00025       'init sqs_selection=[] ',
00026       'init sqs_cond="" ',
00027       'sqs  ::=  SELECT selection FROM fileid WHERE condition ',
00028       '                                  @sqs_fileid="$fileid"',
00029       'selection::=  col0 cols*      ', 
00030       'col0     ::=  col                 @sqs_selection.append("$col")',  
00031       'cols     ::=  "," col             @sqs_selection.append("$col")',
00032       'col      ::=  r"[A-Za-z0-9]"*  ',
00033 
00034           'fileid::= r"\S"* ',
00035       
00036       'condition ::=  cond1  cond2* ',
00037       'cond2     ::=  op_or  cond1  ',
00038       'op_or     ::=  OR                 @sqs_cond+=" or "',
00039       'cond1     ::=  cond3 cond4*  ',
00040       'cond4     ::=  op_and  cond3 ',
00041       'op_and    ::=  AND                @sqs_cond+=" and "',
00042       'cond3     ::=  column operator value  ',
00043       '@sqs_cond+= "process(data,cols,\'$column\',\'$operator\'," ',
00044       '@if sqs_valtype==1:',
00045       '@   sqs_cond+="\'"+sqs_value+"\'"',
00046       '@else:',
00047       '@   sqs_cond+=str(sqs_value)',
00048       '@sqs_cond+=")"',
00049       '           |   leftpar condition rightpar  ',
00050       '           |   condition ',
00051       'column   ::=  r"[A-Za-z0-9]"*  ',
00052       'leftpar   ::=  "("                @sqs_cond+=" ( " ',
00053       'rightpar  ::=  ")"                @sqs_cond+=" ) " ',
00054       'operator  ::=  LIKE ',
00055       '           | "=" ',
00056       '           | "<=" ',
00057       '           | ">=" ',
00058       '           | "<" ',
00059       '           | ">" ',
00060       'value     ::=  "\'" car* "\'"        @sqs_valtype=1  @sqs_value="$car" ',
00061       '           |   numeric               @sqs_valtype=2  @sqs_value=$numeric ',
00062       'car       ::=  r"[^\']" ',
00063       'numeric   ::=  r"[0-9]"* ' ]
00064 parms=' '.join(sys.argv[1:])
00065 
00066 cmp=rp.match(rule,parms)
00067 if cmp==None:
00068         print "Error in parsing:"
00069 else:
00070     #
00071     #cmp.code contains the complete lines of code of the parsing
00072     #so, we need to exec it in order to have values set.
00073     #After that, to access values, no need to prefix with cmp. 
00074     """
00075     print '----- the code -----'
00076     print cmp.code
00077     print '--------------------'
00078     """
00079     exec(cmp.code)
00080     id=None
00081     try:
00082         id=open(sqs_fileid)
00083         #
00084         #Read the first two lines that contain columns name & length
00085         #and prepare dictionary 'columns'
00086         colnames=[x for x in id.readline()[:-1].split(' ') if x!=""]
00087         collengths=id.readline()[:-1].split(' ')
00088         deb=0
00089         cols={}
00090         for i,name in enumerate(colnames):
00091            colsize=len(collengths[i])
00092            cols[name]=(deb,colsize+deb)
00093            deb+=colsize+1
00094         #
00095         #now loop on lines until end of file
00096         for data in id.readlines():
00097             """
00098             print '..... sqs_cond .....'
00099             print sqs_cond
00100             print '....................'
00101             """
00102             exec("found="+sqs_cond)
00103             if found:
00104                 txt=''
00105                 for col in sqs_selection:
00106                     c=cols[col]
00107                     txt+=data[c[0]:c[1]]+' '
00108                 print txt
00109     except Exception,e:
00110             print e
00111     else:
00112         if id!=None: id.close()
00113         


rocon_ebnf
Author(s): LParis
autogenerated on Fri May 2 2014 10:35:48