-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.py
More file actions
250 lines (188 loc) · 5.66 KB
/
Copy pathmetadata.py
File metadata and controls
250 lines (188 loc) · 5.66 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
"""
=========================================================
Universal File Converter
Metadata Utilities
=========================================================
"""
from __future__ import annotations
from pathlib import Path
import fitz
import pandas as pd
from PIL import Image
from docx import Document
class FileMetadata:
"""
Extract metadata from supported files.
"""
@staticmethod
def basic(file_path: str | Path) -> dict:
file = Path(file_path)
return {
"Name": file.name,
"Extension": file.suffix.upper(),
"Size (KB)": round(
file.stat().st_size / 1024,
2,
),
}
# -------------------------------------------------
# Images
# -------------------------------------------------
@staticmethod
def image(file_path: str | Path) -> dict:
image = Image.open(file_path)
width, height = image.size
mode = image.mode
image.close()
return {
**FileMetadata.basic(file_path),
"Width": width,
"Height": height,
"Mode": mode,
}
# -------------------------------------------------
# PDF
# -------------------------------------------------
@staticmethod
def pdf(file_path: str | Path) -> dict:
document = fitz.open(file_path)
pages = len(document)
document.close()
return {
**FileMetadata.basic(file_path),
"Pages": pages,
}
# -------------------------------------------------
# TXT
# -------------------------------------------------
@staticmethod
def text(file_path: str | Path) -> dict:
text = Path(file_path).read_text(
encoding="utf-8",
errors="ignore",
)
return {
**FileMetadata.basic(file_path),
"Characters": len(text),
"Lines": len(text.splitlines()),
}
# -------------------------------------------------
# DOCX
# -------------------------------------------------
@staticmethod
def docx(file_path: str | Path) -> dict:
"""
Return DOCX metadata.
"""
document = Document(file_path)
paragraphs = [
paragraph.text
for paragraph in document.paragraphs
if paragraph.text.strip()
]
return {
**FileMetadata.basic(file_path),
"Paragraphs": len(paragraphs),
}
# -------------------------------------------------
# CSV
# -------------------------------------------------
@staticmethod
def csv(file_path: str | Path) -> dict:
"""
Return CSV metadata.
"""
dataframe = pd.read_csv(file_path)
return {
**FileMetadata.basic(file_path),
"Rows": len(dataframe),
"Columns": len(dataframe.columns),
}
# -------------------------------------------------
# XLSX
# -------------------------------------------------
@staticmethod
def xlsx(file_path: str | Path) -> dict:
"""
Return XLSX metadata.
"""
dataframe = pd.read_excel(file_path)
return {
**FileMetadata.basic(file_path),
"Rows": len(dataframe),
"Columns": len(dataframe.columns),
}
# -------------------------------------------------
# JSON
# -------------------------------------------------
@staticmethod
def json(file_path: str | Path) -> dict:
"""
Return JSON metadata.
"""
import json
with open(
file_path,
"r",
encoding="utf-8",
) as file:
data = json.load(file)
if isinstance(data, list):
records = len(data)
elif isinstance(data, dict):
records = len(data.keys())
else:
records = 1
return {
**FileMetadata.basic(file_path),
"Records": records,
}
# -------------------------------------------------
# XML
# -------------------------------------------------
@staticmethod
def xml(file_path: str | Path) -> dict:
"""
Return XML metadata.
"""
import xml.etree.ElementTree as ET
tree = ET.parse(file_path)
root = tree.getroot()
return {
**FileMetadata.basic(file_path),
"Root": root.tag,
"Children": len(root),
}
# -------------------------------------------------
# Automatic Detection
# -------------------------------------------------
@staticmethod
def get_metadata(file_path: str | Path) -> dict:
"""
Automatically return metadata based on file type.
"""
extension = Path(file_path).suffix.lower()
if extension in {
".jpg",
".jpeg",
".png",
".bmp",
".webp",
".tiff",
}:
return FileMetadata.image(file_path)
if extension == ".pdf":
return FileMetadata.pdf(file_path)
if extension == ".txt":
return FileMetadata.text(file_path)
if extension == ".docx":
return FileMetadata.docx(file_path)
if extension == ".csv":
return FileMetadata.csv(file_path)
if extension == ".xlsx":
return FileMetadata.xlsx(file_path)
if extension == ".json":
return FileMetadata.json(file_path)
if extension == ".xml":
return FileMetadata.xml(file_path)
return FileMetadata.basic(file_path)