import sys from matchKey import * def readKeyconf(filename): try: keystream = open(filename) except Exception: return None keyconf = {} atts = {} key = None for line in keystream: line = line.strip() if len(line) <= 0: continue if line.startswith('#'): key = line.split()[1] atts = {} else: ''' {'KEY_ALA':{'/search/index':{'match':'^ala.*'}}} ''' confs = line.split(' ',1) if len(confs) <= 1: atts[confs[0]] = '' else: atts[confs[0]] = confs[1] keyconf[key] = atts #print(line) return keyconf if __name__ == '__main__': t = {'urlpath':'/search/wiseala','tn':'wiseala','dulisearch':'1'} keyconf = readKeyconf('keyconf.txt') if keyconf is None: print('None') else: print(keyconf) for key in keyconf: if matchKey(keyconf,key,t): print(key)
import sys import re def isMatchAttribute(cl,cmd,value,atts): ok = False if cl not in atts: return ok if cmd == '=': if atts[cl] == value: ok = True elif cmd == 'match': pat = re.compile(value) if pat.match(atts[cl]): ok = True return ok def matchKey(keyconf,keyname,atts): pat = keyconf[keyname] # 匹配所有的属性才算匹配成功 ok = True for cl in pat: vals = pat[cl].split() if len(vals) <= 1: cmd = vals[0] value = "" else: cmd = vals[0] value = vals[1] if not isMatchAttribute(cl,cmd,value,atts): ok = False break return ok