Skip to content

Commit 13f48e9

Browse files
committed
Adds get_files for powerful directory crawling.
The get_files function allows for directory exclusion with the `exclude_dirs` option, file filtering with the `include_regex` option, limitted recursion with the `depth` option, and symlink control with the `follow_symlinks` option.
1 parent decc733 commit 13f48e9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

sc2reader/utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cStringIO
2+
import fnmatch
23
import os
34
import struct
45

@@ -523,3 +524,40 @@ def read_header(file):
523524

524525
#array [unknown,version,major,minor,build,unknown] and frame count
525526
return header_data[1].values(),header_data[3]
527+
528+
529+
def allow(file, include_regex=None):
530+
name, ext = os.path.splitext(file)
531+
if ext.lower() != ".sc2replay":
532+
return False
533+
elif include_regex and not re.match(include_regex,name):
534+
return False
535+
else:
536+
return True
537+
538+
def get_files( location, include_regex=None,
539+
exclude_dirs=[],depth=-1, follow_symlinks=True, **extras):
540+
541+
if not os.path.exists(location):
542+
raise ValueError("Location `{0}` does not exit".format(location))
543+
544+
if not os.path.isdir(location):
545+
if allow(location, include_regex):
546+
return [location]
547+
548+
files = list()
549+
for file in os.listdir(location):
550+
path = os.path.join(location, file)
551+
if os.path.isdir(path):
552+
if file in exclude_dirs or depth == 0:
553+
continue
554+
elif not follow_symlinks and os.path.islink(path):
555+
continue
556+
else:
557+
files.extend(get_files(path, include_regex,
558+
exclude_dirs, depth-1, follow_symlinks))
559+
elif os.path.isfile(path):
560+
if allow(file, include_regex):
561+
files.append(path)
562+
563+
return files

0 commit comments

Comments
 (0)