📄 pfind.py
字号:
badindstart = lineno #break if indok == 0 and badindstart != None and badindstart != lineno: sys.stderr.write('Didn\'t understand indentation lines %d - %d - ignored\n' % (badindstart, lineno)) badindstart = None else: comment = 1 if matchfn((l, lineno)): if not module_printed: print_boldblue('MODULE %s' % (module)) module_printed = 1 if Class and not class_printed: print_boldgreen('CLASS %s' % (Class)) class_printed = 1 if method and not method_printed: print_boldred('METHOD %s' % (method)) method_printed = 1 if Fn and not fn_printed: print_boldmagenta('FUNCTION %s' % (Fn)) fn_printed = 1 if submethod and not submethod_printed: print_cyan('SUB-METHOD %s' % (submethod)) submethod_printed = 1 if not comment: print '%d: %s' % (lineno, l) else: print '%d:' % (lineno), print_yellow('%s' % (l))##############################################################################def get_regex(fnm, snm): try: refile = open(fnm, 'r') except IOError, s: print 'Couldn\'t read regex file', s usage(snm) #re = [l[:-1] for l in refile.readlines() if len(l) > 1 and len(l[:-1].rstrip().lstrip()) > 0] # This one allows for finding lines containing only spaces re = [l[:-1] for l in refile.readlines() if len(l) > 1] #print re if len(re) == 0: eprint('No RE contained in file ' + fnm) sys.exit(1) if len(re) > 1: # get rid of duplicates rid = {} for r in re: if rid.has_key(r): continue rid[r] = 1 re = rid.keys() re.sort() if len(re) > 1: # use alternation for multiple re's for r in re[1:]: re[0] += '|%s' % (r) #print 're is \'%s\'' % (re[0]) return re[0]##############################################################################def getranges(arg): def get_ranges(lines, arg): rangere = re.compile('(\d+)\s*-\s*(\d+)|(\d+)') ranges = [] for lt in lines: error = 0 l = lt[2] g = rangere.match(l) if g: gg = g.groups() if gg[0] and gg[1]: #print 'range', gg[0], gg[1] ranges.append((int(gg[0]), int(gg[1]))) elif gg[2]: #print 'line', gg[2] ranges.append((int(gg[2]), int(gg[2]))) else: error = 1 else: error = 1 if error: print 'whoops - malformed line specification: \'%s\' line/element %d \'%s\'' % (arg, lt[0], lt[1]) return None return ranges def read_lines(fnm): try: f = open(fnm, 'r') except IOError, s: print s return None lines = [] ln = 1 for lhold in f.readlines(): l = lhold.lstrip().rstrip() if not l: ln += 1 continue lines.append((ln, lhold, l)) ln += 1 return lines def get_lines(arg): asplit = arg.split(',') lines = [] part = 1 for l in asplit: lines.append((part, l, l)) part += 1 return lines # # main fn starts here # try: mode = os.stat(arg)[ST_MODE] if S_ISREG(mode): txtranges = read_lines(arg) except OSError, s: if str(s).find('No such file or directory') >= 0: txtranges = get_lines(arg) else: return None if txtranges: return get_ranges(txtranges, arg)##############################################################################def has_line(lineno, lines): #print lineno, lines for ln in lines: if ln[0] <= lineno <= ln[1]: return 1 return 0############################################################################## def main(): recurse = None ferr = [] scriptname = os.path.basename(argv[0]) regex = None remeth = 'match' refile = None txt = None lines = [] try: optlist, args = getopt.getopt(argv[1:], 'hrsf:D:dl:') except getopt.error, s: usage(scriptname, msg=str(s)) for opt in optlist: if opt[0] == '-h': usage(scriptname) if opt[0] == '-r': try: regex = args[0] args = args[1:] except IndexError: usage(scriptname, 'No RE argument given') if opt[0] == '-s': remeth = 'search' if opt[0] == '-f': regex = get_regex(opt[1], scriptname) if opt[0] == '-d': recurse = 100000 if opt[0] == '-l': lines = getranges(opt[1]) if opt[0] == '-D': try: recurse = int(opt[1]) except ValueError: usage(scriptname, 'Invalid recursion depth \'%s\'' % (opt[1])) if (not regex) and (not lines): if not len(args): usage(scriptname, 'No search expression given') txt = args[0] args = args[1:] if not args: args = [os.getcwd()] if not recurse: recurse = 1 elif len(args) == 1 and isdir(args[0]) and not recurse: recurse = 1 if regex: RE = re.compile(regex) #matchfn = getattr(RE, remeth) refn = getattr(RE, remeth) def mch(mfn): return lambda arg: mfn(arg[0]) matchfn = mch(refn) elif lines: def mch(lines): return lambda arg: has_line(arg[1], lines) matchfn = mch(lines) else: def mch(txt): return lambda arg: (arg[0].find(txt) >= 0) matchfn = mch(txt) idre = re.compile('(\s*).*') classre = re.compile('class[ ]*(.*):.*') fnre = re.compile('([ ]*)def[ ]*(.*\(.*\)):.*') cmmtre = re.compile('\s*#+.*') get_files(args, recurse, ferr, matchfn, idre, classre, fnre, cmmtre, lines) if ferr: print ferr.sort() print_bold('Couldn\'t search the following files or directories:') for fe in ferr: print fe[0], ' - ', fe[2][1], '(' + fe[1] + ')' ############################################################################### Call main when run as scriptif __name__ == '__main__': main()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -