This repository has been archived by the owner on May 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanners.py
82 lines (65 loc) · 3.32 KB
/
scanners.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
# import traceback
import os
class FileScanner:
def __init__(self, settings):
self.show_warning_on_open_fail = settings.get('find_in_project_show_warning_on_open_failure', False)
self.encodings = settings.get('find_in_project_encodings', ["utf-8"])
self.skip_binary = settings.get('find_in_project_skip_binary_files', True)
self.show_warning_binary_skip = settings.get('find_in_project_show_warning_on_binary_skip', False)
self.max_file_size = settings.get('find_in_project_max_file_size_mb', 20)*1000000
self.show_warning_size_skip = settings.get('find_in_project_show_warning_on_size_skip', False)
exts_to_ignore = settings.get('find_in_project_ignore_extensions', [])
self.exts_to_ignore = [x.lower() for x in exts_to_ignore]
self.warnings = []
def read_lines(self, filename):
if self._should_include_file(filename):
for enc in self.encodings:
try:
# Read file line by line
with open(filename, "r", encoding=enc) as f:
line_no = 0
while True:
line_no += 1
line = f.readline()
if not line:
break
if self.skip_binary and '\0' in line:
if self.show_warning_binary_skip:
self.warnings.append(
"Skipped binary file.")
return
else:
yield (line_no, line)
return
except UnicodeDecodeError:
# Probably using wrong encoding
# traceback.print_exc()
continue
print("Unable to read file:", filename)
if self.show_warning_on_open_fail:
self.warnings.append(
"Failed to open file. This could be due to unknown/unspecified encoding.")
def _should_include_file(self, filename):
file_extension = os.path.splitext(filename)[1][1:]
if file_extension.lower() in self.exts_to_ignore:
print('Skipping file with ignored extension: ', filename)
return False
file_size = os.path.getsize(filename)
if file_size > self.max_file_size:
if self.show_warning_size_skip:
self.warnings.append(
"Skipped file due to size (%.2f MB)." % (file_size / 1000000.,))
print('Skipping file with size larger than maximum: ', filename)
return False
return True
class DirScanner:
def __init__(self, settings):
self.follow_symlinks = settings.get('find_in_project_follow_sym_links', False)
dirs_to_ignore = settings.get('find_in_project_ignore_dirs', [])
self.dirs_to_ignore = [x.lower() for x in dirs_to_ignore]
def list_tree(self, directory):
for root, dirs, files in os.walk(directory, followlinks=self.follow_symlinks):
dirs[:] = [d for d in dirs if self._should_include_dir(d)]
yield root, dirs, files
def _should_include_dir(self, directory):
return directory.lower() not in self.dirs_to_ignore