Created
November 17, 2013 11:50
-
-
Save keimina/7512395 to your computer and use it in GitHub Desktop.
Grep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import re | |
import console | |
import editor | |
from datetime import datetime | |
INTERRUPT_TIME = 5 | |
colors = {'default':(0,0,0), | |
'blue':(0,0,1), | |
'red':(1,0,0) | |
} | |
def get_color(color): | |
return colors.get(color, colors['default']) | |
def print_in_color(color, msg): | |
console.set_color(*get_color(color)) | |
sys.stdout.write(msg) | |
console.set_color(*get_color('default')) | |
def print_line_with_color(search_pattern, line): | |
line = line.strip() | |
while 1: | |
m = re.search(search_pattern, line) | |
if m == None: | |
print line | |
break | |
s, e = m.span() | |
print_in_color('default', line[:s]) | |
print_in_color('blue', line[s:e]) | |
line = line[e:] | |
def main(): | |
home_dir = os.path.dirname(__file__) | |
current_dir = os.path.dirname(editor.get_path()) | |
current_dir = os.path.relpath(current_dir, home_dir) | |
search_pattern = console.input_alert('Input Search pattern').decode('utf-8') | |
if search_pattern == '': | |
return | |
print '----grep start----' | |
l = os.walk(current_dir) | |
file_name_list = (map(lambda y: os.path.join(x[0], y), x[2]) for x in l if x[2] != []) | |
file_name_list = (item for sublist in file_name_list for item in sublist) | |
file_num = len(list(file_name_list)) | |
l = os.walk(current_dir) | |
file_name_list = (map(lambda y: os.path.join(x[0], y), x[2]) for x in l if x[2] != []) | |
file_name_list = (item for sublist in file_name_list for item in sublist) | |
file_cnt = 0 | |
s_time = datetime.now() | |
while 1: | |
try: | |
file_name = file_name_list.next() | |
except: | |
break | |
try: | |
f = open(file_name, 'r') | |
line = f.readline().decode('utf-8') | |
line_number = 1 | |
while line: | |
m = re.search(search_pattern, line) | |
if m: | |
sys.stdout.write("%s(L%d):" % (file_name, line_number)) | |
print_line_with_color(search_pattern, line) | |
line = f.readline().decode('utf-8') | |
line_number = line_number + 1 | |
delta = datetime.now() - s_time | |
if delta.seconds > INTERRUPT_TIME: | |
print_in_color('red','Continue(Enter) or Quit("Any char" + Enter)[%d/%d(%0.f%%) files completed]:'%(file_cnt, file_num, float(file_cnt)/file_num*100)) | |
inp = raw_input() | |
if inp != '': | |
f.close() | |
return | |
s_time = datetime.now() | |
f.close() | |
delta = datetime.now() - s_time | |
if delta.seconds > INTERRUPT_TIME: | |
print_in_color('red','Continue(Enter) or Quit("Any char" + Enter)[%d/%d(%0.f%%) files completed]:'%(file_cnt, file_num, float(file_cnt)/file_num*100)) | |
inp = raw_input() | |
if inp != '': | |
f.close() | |
return | |
s_time = datetime.now() | |
except: | |
#print 'cann\'t read:%s'%file_name | |
pass | |
file_cnt += 1 | |
print '----grep end----' | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment