-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecs.py
528 lines (402 loc) · 14.9 KB
/
specs.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# specs.py
import io
import os
import tokenize
from typing import Iterable, Any
from pathlib import Path
import json
__all__ = [
"read_file",
"strip_code",
"strip_code_file",
"FilesCollection",
"CodeFileSpecs",
"ContentFileSpecs",
"ProjectTree",
"ProjectInspection",
"ProjectSpecs",
"inspect_project",
"project_tree",
"project_specs"
]
def read_file(path: str) -> str:
"""
Reads the content inside the file.
:param path: The file path.
:return: The content of the file
"""
with open(path, "r", encoding="utf-8") as file:
return file.read()
def strip_code(source: str) -> str:
"""
Strips the code string from any docstring, comments and blank lines.
:param source: The source code.
:return: The stripped code.
"""
out = ""
last_lineno = -1
last_col = 0
previous_token_type = tokenize.INDENT
io_obj = io.StringIO(source)
for tok in tokenize.generate_tokens(io_obj.readline):
token_type = tok[0]
token_string = tok[1]
start_line, start_col = tok[2]
end_line, end_col = tok[3]
if start_line > last_lineno:
last_col = 0
if start_col > last_col:
out += (" " * (start_col - last_col))
if token_type == tokenize.COMMENT:
pass
elif token_type == tokenize.STRING:
if (
(previous_token_type != tokenize.INDENT) and
(previous_token_type != tokenize.NEWLINE) and
(start_col > 0)
):
out += token_string
else:
out += token_string
previous_token_type = token_type
last_col = end_col
last_lineno = end_line
return '\n'.join(
line for line in out.splitlines() if line.strip()
)
def strip_code_file(path: str) -> str:
"""
Strips the code string from any docstring, comments and blank lines.
:param path: The file path.
:return: The stripped code.
"""
return strip_code(read_file(path))
FilesCollection = dict[str, list[str]]
def collect_files(
location: str,
extensions: Iterable[str] = None,
excluded_names: Iterable[str] = None,
levels: int = None
) -> FilesCollection:
"""
Collects all the file paths from the location with the extension.
:param location: The location of the files.
:param extensions: The file extensions.
:param levels: The search levels.
:param excluded_names: The excluded file and directory names.
:return: A list of file paths.
"""
if excluded_names is None:
excluded_names = ()
base_extensions = (".",)
if extensions is None:
extensions = base_extensions
paths = {extension: [] for extension in extensions}
if levels == 0:
return paths
if not any(
part in excluded_names
for part in Path(location).parts
):
for name in os.listdir(location):
path = Path(location) / Path(name)
if path.is_file():
for extension in extensions:
if (
(
(extensions != base_extensions) and
(str(path).endswith(extension))
) or (extensions == base_extensions)
):
paths[extension].append(str(path))
else:
new_paths = collect_files(
str(path), extensions=extensions,
levels=(levels - 1 if levels is not None else levels)
)
for extension in paths:
paths[extension].extend(new_paths[extension])
return paths
class ContentFileSpecs:
"""A class for file specs."""
def __init__(self, path: str, extension: str) -> None:
"""
Defines the class attributes.
:param path: The file path.
:param extension: The file extension.
"""
self.path = path
self.extension = extension
self.content = None
self.lines_count = None
self.words_count = None
self.characters_count = None
def process(self) -> None:
"""Processes the file data."""
self.content = read_file(self.path)
self.lines_count = len(self.content.split("\n"))
self.words_count = len(self.content.replace("\n", "").split())
self.characters_count = len(self.content.replace(" ", ""))
class CodeFileSpecs:
"""A class for file specs."""
def __init__(self, path: str, extension: str) -> None:
"""
Defines the class attributes.
:param path: The file path.
:param extension: The file extension.
"""
self.path = path
self.extension = extension
self.content = None
self.code = None
self.code_lines_count = None
self.content_lines_count = None
self.comment_lines_count = None
self.words_count = None
self.characters_count = None
def process(self) -> None:
"""Processes the file data."""
self.content = read_file(self.path)
self.code = strip_code(self.content)
self.code_lines_count = len(self.code.split("\n"))
self.content_lines_count = len(self.content.split("\n"))
self.comment_lines_count = self.content_lines_count - self.code_lines_count
self.words_count = len(self.code.replace("\n", "").split())
self.characters_count = len(self.code.replace(" ", ""))
class ProjectSpecs:
"""A class for project specs."""
def __init__(
self, content_files_collection: FilesCollection,
code_files_collection: FilesCollection, location: str
) -> None:
"""
Defines the class attributes.
:param location: The project location.
:param content_files_collection: The collection of file paths.
:param code_files_collection: The collection of file paths.
"""
self.location = location
self.content_files_collection = content_files_collection
self.code_files_collection = code_files_collection
self.content_files_count = sum(
len(value) for value in self.content_files_collection.values()
)
self.code_files_count = sum(
len(value) for value in self.code_files_collection.values()
)
self.content_file_extensions = list(self.content_files_collection.keys())
self.code_file_extensions = list(self.code_files_collection.keys())
class ProjectTree:
"""A class to represent the project tree."""
def __init__(self, tree: dict[str, Any]) -> None:
"""
Defines the class attributes.
:param tree: The project tree.
"""
self.tree = tree
def set_project_leaf(
tree: dict[str, Any], branches: list[str], leaf: dict[str, Any]
) -> None:
"""
Set a terminal element to a leaf within nested dictionaries.
:param tree: The project tree object.
:param branches: The project tree branches.
:param leaf: The leaf to add to the tree.
"""
if len(branches) == 1:
tree[branches[0]] = leaf
return
if branches[0] not in tree:
tree[branches[0]] = {}
set_project_leaf(
tree=tree[branches[0]], branches=branches[1:],
leaf=leaf
)
def project_tree(
location: str,
excluded_extensions: Iterable[str] = None,
excluded_names: Iterable[str] = None
) -> ProjectTree:
"""
Gets the project file structure tree.
:param location: The project location.
:param excluded_extensions: The excluded file types.
:param excluded_names: The excluded file and directory names.
:return: The project tree.
"""
tree = {}
for root, dirs, files in os.walk(location):
branches = [location]
if (
(root != location) and
(not any(part in excluded_names for part in Path(root).parts))
):
branches.extend(
Path(os.path.relpath(root, location)).parts
)
files_data = []
for file in files:
valid = True
for extension in excluded_extensions:
if valid and (
file.endswith(extension) or
any(part in excluded_extensions for part in Path(file).parts)
):
valid = False
if valid:
files_data.append((file, None))
directories_data = [
(d, {}) for d in dirs
if any(part in excluded_names for part in Path(d).parts)
]
# noinspection PyTypeChecker
set_project_leaf(
tree=tree, branches=branches, leaf=dict(
directories_data + files_data
)
)
return ProjectTree(tree)
class ProjectInspection:
"""A class for project inspection."""
def __init__(self, specs: ProjectSpecs, tree: ProjectTree) -> None:
"""
Defines the class attributes.
:param specs: The project specs object
:param tree: The tree of the project.
"""
self.specs = specs
self.tree = tree
self.location = self.specs.location
self.content_files_collection = {}
self.code_lines_counters = {}
self.comment_lines_counters = {}
self.content_lines_counters = {}
self.code_files_collection = {}
self.content_file_extensions = self.specs.content_file_extensions
self.code_file_extensions = self.specs.code_file_extensions
self.content_files_count = self.specs.content_files_count
self.code_files_count = self.specs.code_files_count
self.total_code_lines_count = None
self.total_comment_lines_count = None
self.total_content_lines_count = None
self.total_lines_count = None
def process(self) -> None:
"""Processes the project."""
for extension, paths in self.specs.content_files_collection.items():
self.content_files_collection[extension] = {}
self.content_lines_counters[extension] = 0
for path in paths:
content_file_specs = ContentFileSpecs(
path=path, extension=extension
)
content_file_specs.process()
(
self.content_files_collection[extension][path]
) = content_file_specs
self.content_lines_counters[extension] += content_file_specs.lines_count
for extension, paths in self.specs.code_files_collection.items():
self.code_files_collection[extension] = {}
self.code_lines_counters[extension] = 0
self.comment_lines_counters[extension] = 0
for path in paths:
code_file_specs = CodeFileSpecs(
path=path, extension=extension
)
code_file_specs.process()
(
self.code_files_collection[extension][path]
) = code_file_specs
self.code_lines_counters[extension] += code_file_specs.code_lines_count
self.comment_lines_counters[extension] += code_file_specs.comment_lines_count
self.content_file_extensions = self.specs.content_file_extensions
self.code_file_extensions = self.specs.code_file_extensions
self.total_code_lines_count = sum(self.code_lines_counters.values())
self.total_comment_lines_count = sum(self.comment_lines_counters.values())
self.total_content_lines_count = sum(self.content_lines_counters.values())
self.total_lines_count = (
self.total_code_lines_count +
self.total_comment_lines_count +
self.total_content_lines_count
)
def inspect_project(
location: str,
content_file_extensions: Iterable[str] = None,
code_file_extensions: Iterable[str] = None,
excluded_extensions: Iterable[str] = None,
excluded_names: Iterable[str] = None
) -> ProjectInspection:
"""
Defines the class attributes.
:param location: The project location.
:param content_file_extensions: The extensions of file paths.
:param code_file_extensions: The extensions of file paths.
:param excluded_extensions: The excluded file types.
:param excluded_names: The excluded file and directory names.
:returns: The inspection object.
"""
return ProjectInspection(
ProjectSpecs(
content_files_collection=collect_files(
location=location, extensions=content_file_extensions,
excluded_names=excluded_names
),
code_files_collection=collect_files(
location=location, extensions=code_file_extensions,
excluded_names=excluded_names
),
location=location
),
tree=project_tree(
location=location, excluded_extensions=excluded_extensions,
excluded_names=excluded_names,
)
)
class ModelEncoder(json.JSONEncoder):
"""A class to represent a json encoder."""
excluded: Iterable[str] = []
def default(self, obj: Any) -> dict[str, Any]:
"""
Returns the data to encode to json format.
:param obj: The object to encode.
:return: The internal data state of the object.
"""
data = obj.__dict__.copy()
for key in data.copy():
if key in self.excluded:
data.pop(key)
return data
def project_specs(
location: str, save: bool = True,
excluded_extensions: Iterable[str] = None,
excluded_names: Iterable[str] = None,
content_file_extensions: Iterable[str] = None,
code_file_extensions: Iterable[str] = None
) -> ProjectInspection:
"""
Gets the project file structure tree.
:param location: The project location.
:param save: The value to save the objects.
:param excluded_extensions: The excluded file types.
:param excluded_names: The excluded file and directory names.
:param content_file_extensions: The extensions of file paths.
:param code_file_extensions: The extensions of file paths.
:return: The project specs object.
"""
if isinstance(save, bool):
save = "specs"
specs_path = Path(save) / Path(location).parts[-1]
if not specs_path.exists():
os.makedirs(str(specs_path), exist_ok=True)
inspection = inspect_project(
location=location,
code_file_extensions=code_file_extensions,
content_file_extensions=content_file_extensions,
excluded_extensions=excluded_extensions,
excluded_names=excluded_names
)
inspection.process()
ModelEncoder.excluded = excluded_names
if save:
with open(str(specs_path / Path("inspection.json")), "w") as file:
json.dump(inspection, file, cls=ModelEncoder, indent=4)
return inspection