-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_processing.py
More file actions
258 lines (194 loc) · 7.4 KB
/
file_processing.py
File metadata and controls
258 lines (194 loc) · 7.4 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# src/file_processing.py
import os
import tempfile
import pandas as pd
def get_file_hierarchy(path, prefix='', ignore_patterns=None):
"""
Get a list of files and directories in a directory, recursively.
Args:
path (str): The path to the directory.
prefix (str): The prefix to add to each file and directory.
ignore_patterns (list): A list of patterns to ignore.
Returns:
list: A list of files and directories in the directory.
"""
if ignore_patterns is None:
raise ValueError("ignore_patterns must be provided")
output = []
if os.path.isfile(path):
if not check_ignore_patterns(path, ignore_patterns):
output.append(f"{prefix}{os.path.basename(path)}")
elif os.path.isdir(path):
dirname = os.path.basename(path)
if not check_ignore_patterns(dirname, ignore_patterns):
output.append(f"{prefix}{dirname}/")
prefix += ' '
for entry in sorted(os.listdir(path)):
entry_path = os.path.join(path, entry)
output += get_file_hierarchy(entry_path, prefix, ignore_patterns)
return output
def format_file_hierarchy(path, ignore_patterns):
"""
Format the output of get_file_hierarchy().
Args:
path (str): The path to the directory.
ignore_patterns (list): A list of patterns to ignore.
Returns:
str: The formatted output.
"""
prefix = ''
file_hierarchy = get_file_hierarchy(path, prefix, ignore_patterns)
# Replace the temporary directory with './'
path_absolute = os.path.abspath(path)
if os.path.commonpath(
[path_absolute, tempfile.gettempdir()]
) == tempfile.gettempdir():
file_hierarchy[0] = './'
return '\n'.join(file_hierarchy)
def get_ignore_patterns(input_path, ignore_patterns=None):
"""
Get a list of patterns to ignore.
Args:
input_path (str): The path to the input file or directory.
ignore_patterns (list): A list of patterns to ignore.
Returns:
list: A list of patterns to ignore.
"""
default_ignore_patterns = [
'__pycache__',
'.DS_Store',
'egg-info',
'.env',
'.git',
'.ipynb_checkpoints',
'.pkl',
'.pyc',
'.pytest_cache',
'.vscode',
'dist',
'LICENSE',
'venv',
]
git_ignore_patterns = []
if os.path.isdir(input_path):
gitignore_path = os.path.join(input_path, '.gitignore')
if os.path.isfile(gitignore_path):
with open(gitignore_path, 'r') as f:
gitignore_contents = f.read()
git_ignore_patterns += gitignore_contents.splitlines()
if ignore_patterns is None:
ignore_patterns = []
elif isinstance(ignore_patterns, str):
ignore_patterns = ignore_patterns.split(',')
elif len(ignore_patterns) == 1 and isinstance(ignore_patterns[0], str):
ignore_patterns = ignore_patterns[0].split(',')
ignore_patterns += default_ignore_patterns
ignore_patterns += git_ignore_patterns
# Remove duplicates
ignore_patterns = list(set(ignore_patterns))
# Remove empty strings
ignore_patterns = [pattern for pattern in ignore_patterns if pattern]
# Remove comments
ignore_patterns = [pattern for pattern in ignore_patterns if not pattern.startswith('#')]
print(f"Ignoring: {ignore_patterns}")
return ignore_patterns
def check_ignore_patterns(path, ignore_patterns):
"""
Check if a path matches any of the ignore patterns.
Args:
path (str): The path to the file or directory.
ignore_patterns (list): A list of patterns to ignore.
Returns:
bool: True if the path matches any of the ignore patterns, False otherwise.
Examples:
>>> check_ignore_patterns('.git', ['.git'])
True
"""
for pattern in ignore_patterns:
if pattern.lower() in path.lower():
return True
# If the pattern contains a wildcard, check if it matches the path
if '*' in pattern:
pattern_parts = pattern.split('*')
if pattern_parts[0] and pattern_parts[1] and pattern_parts[0] in path and pattern_parts[-1] in path:
return True
return False
def remove_matching_patterns_from_list(list, ignore_patterns):
"""
Remove files or directories from a list if they match any of the ignore patterns.
Args:
list (list): A list of files or directories.
ignore_patterns (list): A list of patterns to ignore.
Returns:
list: A list of files or directories.
"""
return [item for item in list if not check_ignore_patterns(item, ignore_patterns)]
def get_all_code(dir_path, ignore_patterns):
"""
Get all code in a directory, recursively.
Args:
dir_path (str): The path to the directory.
ignore_patterns (list): A list of patterns to ignore.
Returns:
dict: A dictionary of file paths and code.
"""
summary = {}
for root, dirs, files in os.walk(dir_path):
if check_ignore_patterns(root, ignore_patterns):
continue
dirs = remove_matching_patterns_from_list(dirs, ignore_patterns)
files = remove_matching_patterns_from_list(files, ignore_patterns)
for file in files:
file_path = os.path.join(root, file)
code = []
# check the file extension for csv, json, txt, or xml
if file_path.endswith(('.csv')):
# First 3 lines
number_of_lines = 3
# print(f"Reading {number_of_lines*2} lines from {file_path}")
with open(file_path, 'r') as f:
first_lines = [next(f) for x in range(number_of_lines)]
with open(file_path, 'r') as f:
last_lines = f.readlines()[-number_of_lines:]
code += first_lines
code += "\n...\n"
code += last_lines
summary[file_path] = code
continue
else:
try:
with open(file_path, 'r') as f:
code.append(f.read())
except UnicodeDecodeError:
continue
summary[file_path] = code
return summary
def get_code_for_matching_patterns(dir_path, patterns, ignore_patterns):
"""
Get all code in a directory, recursively.
Args:
dir_path (str): The path to the directory.
patterns (list): A list of patterns to match.
ignore_patterns (list): A list of patterns to ignore.
Returns:
dict: A dictionary of file paths and code.
"""
summary = {}
for root, dirs, files in os.walk(dir_path):
if check_ignore_patterns(root, ignore_patterns):
continue
dirs = remove_matching_patterns_from_list(dirs, ignore_patterns)
files = remove_matching_patterns_from_list(files, ignore_patterns)
for file in files:
file_path = os.path.join(root, file)
# Check if the file matches any of the patterns
if not check_ignore_patterns(file_path, patterns):
continue
code = []
try:
with open(file_path, 'r') as f:
code.append(f.read())
except UnicodeDecodeError:
continue
summary[file_path] = code
return summary