pathflow-toolkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- path_file/__init__.py +223 -0
- path_file/__main__.py +6 -0
- path_file/_internal/__init__.py +87 -0
- path_file/_internal/_iterators.py +349 -0
- path_file/_internal/_log.py +153 -0
- path_file/_internal/_parsers.py +234 -0
- path_file/_internal/_safety.py +268 -0
- path_file/_version.py +1 -0
- path_file/actions.py +855 -0
- path_file/archive.py +447 -0
- path_file/chain.py +1028 -0
- path_file/cli.py +582 -0
- path_file/entries.py +459 -0
- path_file/exceptions.py +174 -0
- path_file/filters.py +654 -0
- path_file/hash.py +338 -0
- path_file/progress.py +321 -0
- path_file/py.typed +0 -0
- path_file/report.py +348 -0
- path_file/rules.py +481 -0
- path_file/search.py +472 -0
- path_file/sync.py +384 -0
- path_file/units.py +420 -0
- path_file/utils.py +509 -0
- path_file/watch.py +310 -0
- pathflow_toolkit-0.1.0.dist-info/METADATA +219 -0
- pathflow_toolkit-0.1.0.dist-info/RECORD +30 -0
- pathflow_toolkit-0.1.0.dist-info/WHEEL +4 -0
- pathflow_toolkit-0.1.0.dist-info/entry_points.txt +2 -0
- pathflow_toolkit-0.1.0.dist-info/licenses/LICENSE +21 -0
path_file/report.py
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"""Report building and formatting utilities."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import csv
|
|
6
|
+
import io
|
|
7
|
+
import json
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from path_file.entries import Entry, Report
|
|
12
|
+
from path_file.units import human_size
|
|
13
|
+
|
|
14
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
15
|
+
# REPORT BUILDER
|
|
16
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build_report(entries: list[Entry]) -> Report:
|
|
20
|
+
"""
|
|
21
|
+
Build aggregate report from list of entries.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
entries: List of Entry objects
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
Report object with aggregated statistics
|
|
28
|
+
"""
|
|
29
|
+
report = Report()
|
|
30
|
+
report.chain_size = len(entries)
|
|
31
|
+
|
|
32
|
+
if not entries:
|
|
33
|
+
return report
|
|
34
|
+
|
|
35
|
+
report.total_size = sum(e.size for e in entries)
|
|
36
|
+
report.file_count = sum(1 for e in entries if e.is_file)
|
|
37
|
+
report.dir_count = sum(1 for e in entries if e.is_dir)
|
|
38
|
+
report.symlink_count = sum(1 for e in entries if e.is_symlink)
|
|
39
|
+
report.hidden_count = sum(1 for e in entries if e.is_hidden)
|
|
40
|
+
report.empty_count = sum(1 for e in entries if e.is_empty)
|
|
41
|
+
|
|
42
|
+
# By extension
|
|
43
|
+
for e in entries:
|
|
44
|
+
ext = e.suffix or "(none)"
|
|
45
|
+
report.by_extension[ext] = report.by_extension.get(ext, 0) + 1
|
|
46
|
+
|
|
47
|
+
# Size buckets
|
|
48
|
+
buckets = {
|
|
49
|
+
"0 B": (0, 0),
|
|
50
|
+
"1-10 KB": (1, 10 * 1024),
|
|
51
|
+
"10-100 KB": (10 * 1024, 100 * 1024),
|
|
52
|
+
"100 KB - 1 MB": (100 * 1024, 1024 * 1024),
|
|
53
|
+
"1-10 MB": (1024 * 1024, 10 * 1024 * 1024),
|
|
54
|
+
"10-100 MB": (10 * 1024 * 1024, 100 * 1024 * 1024),
|
|
55
|
+
"100 MB - 1 GB": (100 * 1024 * 1024, 1024 * 1024 * 1024),
|
|
56
|
+
"1-10 GB": (1024 * 1024 * 1024, 10 * 1024 * 1024 * 1024),
|
|
57
|
+
"10 GB+": (10 * 1024 * 1024 * 1024, float("inf")),
|
|
58
|
+
}
|
|
59
|
+
for e in entries:
|
|
60
|
+
for bucket, (min_s, max_s) in buckets.items():
|
|
61
|
+
if min_s <= e.size <= max_s:
|
|
62
|
+
report.by_size_bucket[bucket] = report.by_size_bucket.get(bucket, 0) + 1
|
|
63
|
+
break
|
|
64
|
+
|
|
65
|
+
# Extremes
|
|
66
|
+
file_entries = [e for e in entries if e.is_file]
|
|
67
|
+
if file_entries:
|
|
68
|
+
report.largest = max(file_entries, key=lambda e: e.size)
|
|
69
|
+
report.smallest = min(file_entries, key=lambda e: e.size)
|
|
70
|
+
|
|
71
|
+
report.newest = max(entries, key=lambda e: e.modified)
|
|
72
|
+
report.oldest = min(entries, key=lambda e: e.modified)
|
|
73
|
+
|
|
74
|
+
return report
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
78
|
+
# TABLE FORMATTER
|
|
79
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def format_table(
|
|
83
|
+
headers: list[str],
|
|
84
|
+
rows: list[list[str]],
|
|
85
|
+
alignments: list[str] | None = None,
|
|
86
|
+
) -> str:
|
|
87
|
+
"""
|
|
88
|
+
Format data as pretty ASCII table.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
headers: Column headers
|
|
92
|
+
rows: Data rows (list of lists of strings)
|
|
93
|
+
alignments: List of 'left', 'right', 'center' for each column
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
Formatted table string
|
|
97
|
+
"""
|
|
98
|
+
if not rows:
|
|
99
|
+
return "No data."
|
|
100
|
+
|
|
101
|
+
# Calculate column widths
|
|
102
|
+
col_widths = [len(h) for h in headers]
|
|
103
|
+
for row in rows:
|
|
104
|
+
for i, cell in enumerate(row):
|
|
105
|
+
if i < len(col_widths):
|
|
106
|
+
col_widths[i] = max(col_widths[i], len(str(cell)))
|
|
107
|
+
|
|
108
|
+
# Default alignments
|
|
109
|
+
if alignments is None:
|
|
110
|
+
alignments = ["left"] * len(headers)
|
|
111
|
+
|
|
112
|
+
# Build format strings
|
|
113
|
+
def align_cell(cell: str, width: int, align: str) -> str:
|
|
114
|
+
cell = str(cell)
|
|
115
|
+
if align == "right":
|
|
116
|
+
return cell.rjust(width)
|
|
117
|
+
elif align == "center":
|
|
118
|
+
return cell.center(width)
|
|
119
|
+
else:
|
|
120
|
+
return cell.ljust(width)
|
|
121
|
+
|
|
122
|
+
lines = []
|
|
123
|
+
# Top border
|
|
124
|
+
top_parts = ["┌" + "─" * (w + 2) + "┐" for w in col_widths]
|
|
125
|
+
lines.append("".join(top_parts[0].replace("┌", "┌").replace("┐", "┬")[:-1] + "┐"))
|
|
126
|
+
|
|
127
|
+
# Headers
|
|
128
|
+
header_cells = [
|
|
129
|
+
f" {align_cell(h, col_widths[i], alignments[i])} " for i, h in enumerate(headers)
|
|
130
|
+
]
|
|
131
|
+
lines.append("│" + "│".join(header_cells) + "│")
|
|
132
|
+
|
|
133
|
+
# Header separator
|
|
134
|
+
sep_cells = ["─" * (col_widths[i] + 2) for i in range(len(headers))]
|
|
135
|
+
lines.append("├" + "┼".join(sep_cells) + "┤")
|
|
136
|
+
|
|
137
|
+
# Data rows
|
|
138
|
+
for row in rows:
|
|
139
|
+
row_cells = [
|
|
140
|
+
f" {align_cell(row[i] if i < len(row) else '', col_widths[i], alignments[i])} "
|
|
141
|
+
for i in range(len(headers))
|
|
142
|
+
]
|
|
143
|
+
lines.append("│" + "│".join(row_cells) + "│")
|
|
144
|
+
|
|
145
|
+
# Bottom border
|
|
146
|
+
bot_parts = ["└" + "─" * (w + 2) + "┘" for w in col_widths]
|
|
147
|
+
lines.append("".join(bot_parts[0].replace("└", "└").replace("┘", "┴")[:-1] + "┘"))
|
|
148
|
+
|
|
149
|
+
return "\n".join(lines)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def format_entry_table(entries: list[Entry]) -> str:
|
|
153
|
+
"""Format entries as a table."""
|
|
154
|
+
if not entries:
|
|
155
|
+
return "No entries found."
|
|
156
|
+
|
|
157
|
+
headers = ["Name", "Size", "Type", "Modified"]
|
|
158
|
+
alignments = ["left", "right", "left", "left"]
|
|
159
|
+
rows = []
|
|
160
|
+
|
|
161
|
+
for e in entries:
|
|
162
|
+
name = e.name[:40] + ".." if len(e.name) > 42 else e.name
|
|
163
|
+
size = e.size_human if e.is_file else "-"
|
|
164
|
+
typ = "FILE" if e.is_file else "DIR" if e.is_dir else "LINK"
|
|
165
|
+
mod = e.modified.strftime("%Y-%m-%d %H:%M")
|
|
166
|
+
rows.append([name, size, typ, mod])
|
|
167
|
+
|
|
168
|
+
return format_table(headers, rows, alignments)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def format_extension_table(report: Report) -> str:
|
|
172
|
+
"""Format extension breakdown as table."""
|
|
173
|
+
if not report.by_extension:
|
|
174
|
+
return "No files."
|
|
175
|
+
|
|
176
|
+
headers = ["Extension", "Count", "Percentage"]
|
|
177
|
+
alignments = ["left", "right", "right"]
|
|
178
|
+
rows = []
|
|
179
|
+
|
|
180
|
+
total = report.chain_size
|
|
181
|
+
for ext, count in sorted(report.by_extension.items(), key=lambda x: -x[1]):
|
|
182
|
+
ext_display = ext if ext else "(none)"
|
|
183
|
+
pct = f"{count / total * 100:.1f}%"
|
|
184
|
+
rows.append([ext_display, str(count), pct])
|
|
185
|
+
|
|
186
|
+
return format_table(headers, rows, alignments)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def format_size_bucket_table(report: Report) -> str:
|
|
190
|
+
"""Format size bucket breakdown as table."""
|
|
191
|
+
if not report.by_size_bucket:
|
|
192
|
+
return "No files."
|
|
193
|
+
|
|
194
|
+
headers = ["Size Range", "Count", "Percentage"]
|
|
195
|
+
alignments = ["left", "right", "right"]
|
|
196
|
+
rows = []
|
|
197
|
+
|
|
198
|
+
total = report.chain_size
|
|
199
|
+
for bucket, count in sorted(report.by_size_bucket.items(), key=lambda x: x[0]):
|
|
200
|
+
pct = f"{count / total * 100:.1f}%"
|
|
201
|
+
rows.append([bucket, str(count), pct])
|
|
202
|
+
|
|
203
|
+
return format_table(headers, rows, alignments)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
207
|
+
# JSON ENCODER
|
|
208
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class PathflowJSONEncoder(json.JSONEncoder):
|
|
212
|
+
"""Custom JSON encoder for path_file types."""
|
|
213
|
+
|
|
214
|
+
def default(self, obj: Any) -> Any:
|
|
215
|
+
if isinstance(obj, Path):
|
|
216
|
+
return str(obj)
|
|
217
|
+
if isinstance(obj, Entry):
|
|
218
|
+
return obj.as_dict()
|
|
219
|
+
if hasattr(obj, "__dict__"):
|
|
220
|
+
return obj.__dict__
|
|
221
|
+
return super().default(obj)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def to_json(data: Any, indent: int = 2) -> str:
|
|
225
|
+
"""Convert to JSON string with custom encoder."""
|
|
226
|
+
return json.dumps(data, indent=indent, ensure_ascii=False, cls=PathflowJSONEncoder)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
230
|
+
# CSV FORMATTER
|
|
231
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def to_csv(entries: list[Entry], include_header: bool = True) -> str:
|
|
235
|
+
"""Convert entries to CSV string."""
|
|
236
|
+
output = io.StringIO()
|
|
237
|
+
writer = csv.writer(output)
|
|
238
|
+
|
|
239
|
+
if include_header:
|
|
240
|
+
writer.writerow(
|
|
241
|
+
[
|
|
242
|
+
"path",
|
|
243
|
+
"name",
|
|
244
|
+
"stem",
|
|
245
|
+
"suffix",
|
|
246
|
+
"extensions",
|
|
247
|
+
"size",
|
|
248
|
+
"size_human",
|
|
249
|
+
"created",
|
|
250
|
+
"modified",
|
|
251
|
+
"accessed",
|
|
252
|
+
"is_file",
|
|
253
|
+
"is_dir",
|
|
254
|
+
"is_symlink",
|
|
255
|
+
"is_hidden",
|
|
256
|
+
"is_empty",
|
|
257
|
+
"hash_sha256",
|
|
258
|
+
]
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
for e in entries:
|
|
262
|
+
writer.writerow(
|
|
263
|
+
[
|
|
264
|
+
str(e.path),
|
|
265
|
+
e.name,
|
|
266
|
+
e.stem,
|
|
267
|
+
e.suffix,
|
|
268
|
+
";".join(e.extensions),
|
|
269
|
+
e.size,
|
|
270
|
+
e.size_human,
|
|
271
|
+
e.created.isoformat(),
|
|
272
|
+
e.modified.isoformat(),
|
|
273
|
+
e.accessed.isoformat(),
|
|
274
|
+
e.is_file,
|
|
275
|
+
e.is_dir,
|
|
276
|
+
e.is_symlink,
|
|
277
|
+
e.is_hidden,
|
|
278
|
+
e.is_empty,
|
|
279
|
+
e.hash_sha256 if e.is_file else "",
|
|
280
|
+
]
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
return output.getvalue()
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
287
|
+
# REPORT FORMATTING
|
|
288
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def format_report(report: Report, include_tables: bool = True) -> str:
|
|
292
|
+
"""Format full report as human-readable string."""
|
|
293
|
+
lines = []
|
|
294
|
+
|
|
295
|
+
lines.append("╔════════════════════════════════════════════════════════════════╗")
|
|
296
|
+
lines.append("║ PATHFLOW REPORT ║")
|
|
297
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
298
|
+
|
|
299
|
+
lines.append(
|
|
300
|
+
f"║ Total Entries: {report.chain_size:<6} Total Size: "
|
|
301
|
+
f"{human_size(report.total_size):>12} ║"
|
|
302
|
+
)
|
|
303
|
+
lines.append(
|
|
304
|
+
f"║ Files: {report.file_count:<6} Dirs: {report.dir_count:<6} "
|
|
305
|
+
f"Symlinks: {report.symlink_count:<6} Hidden: {report.hidden_count:<6} "
|
|
306
|
+
f"Empty: {report.empty_count:<6} ║"
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
if include_tables and report.by_extension:
|
|
310
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
311
|
+
lines.append("║ BY EXTENSION ║")
|
|
312
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
313
|
+
ext_table = format_extension_table(report)
|
|
314
|
+
for line in ext_table.split("\n"):
|
|
315
|
+
lines.append(f"║ {line:<58} ║")
|
|
316
|
+
|
|
317
|
+
if include_tables and report.by_size_bucket:
|
|
318
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
319
|
+
lines.append("║ BY SIZE ║")
|
|
320
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
321
|
+
size_table = format_size_bucket_table(report)
|
|
322
|
+
for line in size_table.split("\n"):
|
|
323
|
+
lines.append(f"║ {line:<58} ║")
|
|
324
|
+
|
|
325
|
+
if report.largest or report.newest:
|
|
326
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
327
|
+
lines.append("║ EXTREMES ║")
|
|
328
|
+
lines.append("╠════════════════════════════════════════════════════════════════╣")
|
|
329
|
+
if report.largest:
|
|
330
|
+
lines.append(
|
|
331
|
+
f"║ Largest: {report.largest.path!s:<50} ({report.largest.size_human}) ║"
|
|
332
|
+
)
|
|
333
|
+
if report.smallest and report.smallest.size > 0:
|
|
334
|
+
lines.append(
|
|
335
|
+
f"║ Smallest: {report.smallest.path!s:<50} ({report.smallest.size_human}) ║"
|
|
336
|
+
)
|
|
337
|
+
if report.newest:
|
|
338
|
+
lines.append(
|
|
339
|
+
f"║ Newest: {report.newest.path!s:<50} ({report.newest.modified_ago}) ║"
|
|
340
|
+
)
|
|
341
|
+
if report.oldest:
|
|
342
|
+
lines.append(
|
|
343
|
+
f"║ Oldest: {report.oldest.path!s:<50} ({report.oldest.modified_ago}) ║"
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
lines.append("╚════════════════════════════════════════════════════════════════╝")
|
|
347
|
+
|
|
348
|
+
return "\n".join(lines)
|