sqs7.py
Go to the documentation of this file.
00001 #!c:/python26/python.exe
00002 import sys,rp
00003 
00004 rule=['init sqs_selection=[] ',
00005       'sqs  ::=  SELECT selection FROM fileid WHERE column operator value ',
00006       '                                 @sqs_fileid="$fileid"',
00007       '                                 @sqs_column="$column"',
00008       '                                 @sqs_ope="$operator"',
00009       
00010       'selection::=  col0 cols*      ', 
00011       'col0     ::=  col                 @sqs_selection.append("$col")',  
00012       'cols     ::=  "," col             @sqs_selection.append("$col")',
00013       'col      ::=  r"[A-Za-z0-9]"*  ',
00014       'column   ::=  r"[A-Za-z0-9]"*  ',
00015       'value::=  "\'" car* "\'"         @sqs_valtype=1  @sqs_value="$car" ',
00016       '      |   numeric                @sqs_valtype=2  @sqs_value=numeric ',
00017       'operator::=  LIKE ',
00018       '         |   "="  ',
00019       '         |   ">"  ',
00020       '         |   "<"  ',
00021           'car  ::=  r"[^\']" ',
00022       'numeric::= r"[0-9]"* ',
00023           'fileid::= r"\S"* ']
00024 parms=' '.join(sys.argv[1:])
00025 
00026 cmp=rp.match(rule,parms)
00027 if cmp==None:
00028         print "Error in parsing:"
00029 else:
00030     id=None
00031     try:
00032         id=open(cmp.sqs_fileid)
00033         #
00034         #Read the first two lines that contain columns name & length
00035         #and prepare dictionary 'columns'
00036         colnames=[x for x in id.readline()[:-1].split(' ') if x!=""]
00037         collengths=id.readline()[:-1].split(' ')
00038         deb=0
00039         columns={}
00040         for i,name in enumerate(colnames):
00041            colsize=len(collengths[i])
00042            columns[name]=(deb,colsize+deb)
00043            deb+=colsize+1
00044         #
00045         #now loop on lines until end of file
00046         for l in id.readlines():
00047             indexes=columns[cmp.sqs_column]     #get indexes associated to column
00048             data=l[indexes[0]:indexes[1]]       #get the data of where condition
00049             found=False
00050             if cmp.sqs_valtype==1:
00051                 if cmp.sqs_ope.upper()=='LIKE':
00052                     if cmp.sqs_value.startswith('%'):
00053                         found=data.endswith(cmp.sqs_value[1:])
00054                     elif cmp.sqs_value.endswith('%'):
00055                         found=data.startswith(cmp.sqs_value[:-1])
00056                     else:
00057                         found=(data.find(cmp.sqs_value)>-1)
00058                 elif cmp.sqs_ope=='=':
00059                     found=(data == cmp.sqs_value)
00060                 else:
00061                     raise Exception('Operator not allowed with alpha')
00062             else:
00063                 try:
00064                     v1=int(data.strip())
00065                     v2=int(cmp.sqs_value.strip())
00066                     if cmp.sqs_ope=='=': found=(v1==v2)
00067                     elif cmp.sqs_ope=='>': found=(v1>v2)
00068                     elif cmp.sqs_ope=='<': found=(v1<v2)
00069                 except:
00070                     pass
00071             if found:
00072                 txt=''
00073                 for col in cmp.sqs_selection:
00074                     c=columns[col]
00075                     txt+=l[c[0]:c[1]]+' '
00076                 print txt
00077     except Exception,e:
00078             print e
00079     else:
00080         if id!=None: id.close()
00081         


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