-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalker.py
88 lines (75 loc) · 2.25 KB
/
walker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
#-*- coding: utf-8 -*-
"""
File Walker
"""
import os
import re
class Walker:
def __init__(self, dir):
self.dir = dir
self.cache = []
self.patterns = []
def set_patterns(self, patterns, ignore=True):
p = _Pattern(patterns, ignore)
self.patterns.append(p)
def start(self, iscache=False):
for dirpath, dirs, files in os.walk(self.dir):
for f in files:
fpath = os.path.join(dirpath, f)
if not self.__is_notice(fpath):
continue
if iscache:
self.cache.append(fpath)
print "chche"
yield fpath
def __is_notice(self, dirpath):
for p in self.patterns:
if not self.__is_target_pattern(p, dirpath):
return False
return True
def __is_target_pattern(self, pattern, dirpath):
" Pattern is so complex..."
if not self.patterns:
return True
for t in pattern.patterns:
if t.search(dirpath):
if pattern.is_ignore:
return False
return True
if pattern.is_ignore:
return True
return False
def back(self, num):
if not num:
raise ValueError("Should over 0")
if not self.cache:
raise KeyError("You do not set chache")
if num > 0:
num = 0 - num
return self.cache[num]
class _Pattern:
def __init__(self, patterns, ignore=True):
self.patterns = [re.compile(i) for i in patterns]
self.is_ignore = ignore
if __name__ == '__main__':
print __file__
full_path = os.path.abspath(__file__)
paths = full_path.split("/")
home = "/".join(paths[:6])
walker = Walker(home)
walker.set_patterns([".*py$"], False)
walker.set_patterns([".*__init__.py"])
#walker.set_patterns([".*pyc$",".*cgi$", "/\.git"])
n = walker.start(iscache=True)
print "---- sart ------"
while True:
try:
i = 1
print n.next()
print "#####",walker.back(i)
i +=1
except StopIteration:
print "end"
break
print "----- end from: %s ------" % home