-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhonq.py
More file actions
228 lines (166 loc) · 6.78 KB
/
honq.py
File metadata and controls
228 lines (166 loc) · 6.78 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''
Hierarchical Operating system Notation Query
Provides really easy to use filtering options for getting the files you want.
Iterate over Files(), optionally chaining additional filters to get what you
want without bothering with os.walk.
IMPORTANT NOTE: For processing speed, it returns (<full path>, <file name>).
End the query with .full() to limit the results to just the full path.
# Prints out all the files in all sub directories of C:/CoolStuff
for f in Files('C:/CoolStuff').full():
print f
# Use .skipFolders(), .like()/.notLike(), .types()/.notTypes() to filter results.
# This finds all .jpg and .jpeg files under C:/CoolStuff, skipping and directories
# with "temp" in the name.
for f in Files('C:/CoolStuff').skipFolders('temp').types('jpg', 'jpeg').full():
print f
# .like()/.unlike() and .skipFolders(), in addtion to several criteria, can be
# followed with exact=True to require an exact match, so .like('temp', exact=True)
# only matches 'temp' and not 'temporal'
for f in Files('C:/CoolStuff').skipFolders('temp', exact=True).full():
print f
# Case is ignore by default, controlled by the module level `ignoreCase`
honq.ignoreCase = False # Now I'm case sensitive!
# The search terms are actually regular expressions, you can get elaborate if needed.
# This only finds files ending with song1, song2 and song3, only top 3 is good
# enough for us.
for f in Files('C:/CoolStuff').like('song[1-3]').full():
print f
'''
import itertools
import os
import re
try:
import pathlib
except ImportError:
pathlib = None
try:
# Python 2
from future_builtins import filter
except ImportError:
# Python 3
pass
try:
from itertools import ifilterfalse as filterfalse
except ImportError:
from itertools import filterfalse
ignoreCase = True
class _Stream(object):
'''
Represents a stream of filenames.
'''
def __init__(self, upstream=None):
self.incoming = upstream or tuple()
def __iter__(self):
return (i for i in self.incoming)
def like(self, *patterns, **kwargs):
'''
Takes one or more patterns to match. Optionally follow with exact=True.
'''
exact = kwargs['exact'] if 'exact' in kwargs else False
return _Like(self, patterns, exact=exact)
def notLike(self, *patterns, **kwargs):
exact = kwargs['exact'] if 'exact' in kwargs else False
return _Like(self, patterns, invert=True, exact=exact)
def types(self, *filetypes):
regexes = ['\.' + ft + '$' for ft in filetypes]
return _Like(self, regexes, testFullPath=True)
def notTypes(self, *filetypes):
regexes = ['\.' + ft + '$' for ft in filetypes]
return _Like(self, regexes, invert=True, testFullPath=True)
#if pathlib:
# def asPathObj(self):
# return _ForEach(self, pathlib.Path)
def fSlash(self):
# Make results use forward slashes
return _ForEach(self, lambda x: (x[0].replace('\\', '/'), x[1]) )
def bSlash(self):
# Make results use backslashes
return _ForEach(self, lambda x: (x[0].replace('/', '\\'), x[1]) )
def lower(self):
# Make results lower case
return _ForEach(self, lambda x: (x[0].lower(), x[1].lower()) )
def full(self):
# Limit results to the full filepath
return _ForEach(self, lambda x: x[0] )
class _ForEach(_Stream):
'''
Runs the given function on the results.
'''
def __init__(self, upstream=tuple(), func=lambda x: x):
super(_ForEach, self).__init__(upstream=upstream)
self.func = func
def __iter__(self):
return itertools.imap(self.func, self.incoming)
class Files(_Stream):
'''
Takes one or more folders, returning all the files beneath it, and sub dirs.
'''
def __init__(self, *folders):
'''
init
'''
self.folders = [folder for folder in folders if os.path.isdir(folder)]
self.ignore = None
def __iter__(self):
for folder in self.folders:
for path, dirs, files in os.walk(folder):
for f in files:
yield path + '/' + f, os.path.splitext(f)[0]
if self.ignore:
toRemove = [d for d in dirs if self.ignore(d)]
for toRem in toRemove:
dirs.remove(toRem)
def skipFolders(self, *folders, **kwargs):
if 'exact' in kwargs and kwargs['exact']:
#regex = '((' + ')|('.join( folders ) + '))'
regex = '((^' + '$)|(^'.join( folders ) + '$))'
else:
regex = '((' + ')|('.join( folders ) + '))'
flags = re.IGNORECASE if ignoreCase else 0
self.ignore = re.compile(regex, flags=flags).search
return self
class Dirs(_Stream):
def __init__(self, *folders):
self.folders = [folder for folder in folders if os.path.isdir(folder)]
self.ignore = None
def __iter__(self):
for folder in self.folders:
for path, dirs, files in os.walk(folder):
if self.ignore:
toRemove = [d for d in dirs if self.ignore(d)]
for toRem in toRemove:
dirs.remove(toRem)
for d in dirs:
yield path + '/' + d, d
def skipFolders(self, *folders, **kwargs):
if 'exact' in kwargs and kwargs['exact']:
#regex = '((' + ')|('.join( folders ) + '))'
regex = '((^' + '$)|(^'.join( folders ) + '$))'
else:
regex = '((' + ')|('.join( folders ) + '))'
flags = re.IGNORECASE if ignoreCase else 0
self.ignore = re.compile(regex, flags=flags).search
return self
class _Like(_Stream):
'''
Only allow files that pass the regexes (unless inverted).
'''
def __init__(self, upstream=tuple(), regexes=('.',), invert=False, exact=False, testFullPath=False):
super(_Like, self).__init__(upstream=upstream)
self.invert = invert
if exact:
regex = '((^' + '$)|(^'.join( regexes ) + '$))'
else:
regex = '((' + ')|('.join( regexes ) + '))'
flags = re.IGNORECASE if ignoreCase else 0
self.regex = re.compile(regex, flags).search
if testFullPath:
self.test = lambda path_name: self.regex(path_name[0])
else:
self.test = lambda path_name: self.regex(path_name[1])
def __iter__(self):
_stream = iter(self.incoming)
if self.invert:
return filterfalse(self.test, _stream)
else:
return filter(self.test, _stream)