Repository navigation
Expand file tree
/
Copy pathFiles.py
More file actions
344 lines (300 loc) · 11.7 KB
/
Copy pathFiles.py
File metadata and controls
344 lines (300 loc) · 11.7 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
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
from PIL import Image, ImageTk
from customtkinter import CTkFont, CTkTextbox
import os, io, csv, cv2, json, rembg, pandas as pd
from tkinter import Tk, Frame, mainloop, PhotoImage, END, ttk , filedialog
class File_Handler:
"""initializer
-----
an easy and short way to deal with files\n
Get_file_path()
-----
Get the path of a file with filedaialog without importing any thing
-----
Edit_Text_File()
-----
opening the file in a built in Text Editor
make sure that the file is `.txt`\n
otherwise it might looks ugly\n
`filename` the path of the file to Edit, you can get it from
`Get_file_path()` easily
-----
Get_contents_from_json()
-----
Get the contents of a `JSON` file
-----
Get_csv_content_as_list()
-----
Get the contents of a `CSV` file as a `list` of `dict`
-----
Get_csv_content_as_table()
-----
Get the contents of a `CSV` file as a table
-----
Get_text_from_txt()
-----
Get the contents of a `txt` file
-----
Add_to_JSON_file()
-----
adding some data to `JSON` file
-----
Add_to_txt_file()
-----
adding some data to `txt` file\n
`replace: bool` if replace then replace all the content of `txt` file and add the new text
-----
bg_remover()
-----
removing the Background of the image\n
not supported in python 3.12
"""
def __init__(self):
self.__content: str
self.__file: str
self._Version = "Version: 1.5"
self._Publisher = "NooR MaseR"
def Get_file_path(self) -> str | None:
"Get the file path"
file = filedialog.askopenfilename(
title="Pick a file",
filetypes=[
("All files", "*.*"),
("Text file", "*.txt"),
("JSON file", "*.json"),
("CSV files", "*.csv"),
("Excel files", "*.xlsx"),
("PNG files", "*.png"),
("jpeg files", "*.jpeg"),
("jpg files", "*.jpg"),
],
)
if file:
self.__file = file
return f"{file}"
class ask_to_save_as:
"Get the path for saving the file"
def __str__(self) -> str:
return self.__file
def __init__(self,content: str) -> None:
assert content, "content can not be empty"
self.__content = content
self.__file = filedialog.SaveAs(
defaultextension=".*txt",
filetypes=[
("Text file", "*.txt"),
("JSON file", "*.json"),
("CSV files", "*.csv"),
("Excel files", "*.xlsx"),
("PNG files", "*.png"),
("jpeg files", "*.jpeg"),
("jpg files", "*.jpg"),
("All files", "*.*"),
]
).show()
if self.__file:
with io.open(self.__file,"w") as f:
f.write(self.__content)
def remove_file(self,path: str) -> None:
os.remove(path)
def Edit_Text_File(self, filename: str) -> None:
"""
opening the file in a built in Text Editor
make sure that the file is `.txt`\n
otherwise it might looks ugly\n
----
`filename: str` the path of the file to Edit, you can get it from `Get_file()` easily
"""
def Save(text):
with io.open(self.__file, "w") as f:
f.write(text)
if filename.endswith(".txt"):
with io.open(filename, "r") as f:
self.__content = f.read()
app = Tk()
app.geometry("600x500")
app.title("Text Editor")
fonte = CTkFont(family="Arial", size=18)
textframe = ttk.Frame(app)
textframe.pack(fill="both", expand=1)
text_box = CTkTextbox(textframe, font=fonte, undo=True, corner_radius=0)
text_box.insert("end", self.__content)
text_box.pack(fill="both", expand=1)
buttons_frame = Frame(app, height=30)
buttons_frame.pack(fill="x")
cancel_button = ttk.Button(buttons_frame, text="Cancel", command=quit)
cancel_button.place(x=5)
save_button = ttk.Button(
buttons_frame,
text="Save",
command=lambda: Save(text_box.get("1.0", END)),
)
save_button.place(x=125)
mainloop()
else:
raise TypeError("file extention is not txt")
def Get_contents_from_json(self, file_path: str) -> dict:
"Get the contents of a `JSON` file"
if file_path.endswith(".json"):
with io.open(file_path, "r") as f:
self.__content = json.load(f)
return self.__content
else:
raise TypeError("this is not a json file!!!!!!!!!!!!!!!")
def Get_csv_content_as_list(self, file_path: str) -> list:
"Get the contents of a `CSV` file as a `list` of `dict`"
if file_path.endswith(".csv"):
with io.open(file_path, "r", encoding="utf-8") as f:
reader = list(csv.DictReader(f))
return reader
else:
raise TypeError("this is not a csv file!!!!!!!!!!!!!!!")
def Get_csv_content_as_table(self, file_path: str) -> pd.DataFrame:
"""
Get the contents of a `CSV` file as a table\n
>>> csv_file = File_Handler()
>>> file = csv_file.Get_file_path()
>>> content = csv_file.Get_csv_content_as_table(file)
>>> print(content)\n
---
you can rename any row using `rename()`
>>> renamed_row = content.rename(columns= {"row to replace":"replace with"})
>>> print(renamed_raw)\n
---
you can get any raw by slicing it\n
for examble
>>> print(renamed_raw['Email'])
"""
if file_path.endswith(".csv"):
reader = pd.read_csv(file_path, encoding="utf-8")
return reader
else:
raise TypeError("this is not a csv file!!!!!!!!!!!!!!!")
def Get_web_csv_file(self, url: str, encoding: str = "utf-8") -> pd.DataFrame:
"""
Get the contents of a `CSV` file from internet\n
>>> db = File_Handler()
>>> content = db.Get_web_csv_file(file)
>>> print(content)\n
---
you can rename any row using `rename()`
>>> renamed_row = content.rename(columns= {"row to replace":"replace with"})
>>> print(renamed_raw)\n
---
you can get any raw by slicing it\n
for examble
>>> print(renamed_raw['ID'])
"""
try:
reader = pd.read_csv(url, encoding=encoding)
return reader
except:
raise Exception("failed to get the csv file online")
def Get_tables_from_web(self, url: str, encode: str | None = None) -> list[pd.DataFrame] | str:
"Get the tables or csv files online"
if url:
try:
reader = pd.read_html(url, encoding=encode)
if reader:
return reader
else:
return f"No Tables has been Found in {url}"
except:
raise TypeError(
"This url is Not currect please make sure to copy the same url that contain the tables."
)
else:
raise Exception("url cannot be empty!!!!!")
def Get_excel_content(self, file_path: str) -> pd.DataFrame:
"""Get the contents of `Excel` file\n
>>> db = File_Handler()
>>> file = db.Get_file_path()
>>> content = db.Get_excel_content(file)
>>> print(content)\n
---
you can rename any row using `rename()`
>>> renamed_row = content.rename(columns= {"row to replace":"replace with"})
>>> print(renamed_raw)\n
---
you can get any raw by slicing it\n
for examble
>>> print(renamed_raw['ID'])
"""
if file_path.endswith(".xlsx" or "xls"):
reader = pd.read_excel(file_path)
return reader
else:
raise TypeError("this is not a Excel file!!!!!!!!!!!!!!!")
def Get_text_from_txt(self, file_path: str) -> str:
"Get the contents of a `txt` file"
if file_path.endswith(".txt"):
with io.open(file_path, "r") as f:
self.__content = f.read()
return self.__content
else:
raise TypeError("this is not a text file!!!!!!!!!!!!!!!")
def Add_to_JSON_file(self, filename: str, add: str | int | float, indentation: int = 4):
"""adding some data to `JSON` file
>>> edit = File_Handler()
>>> text = edit.Get_contents_from_json('path/to/json/file')
>>> text["Key"] = "value"
>>> edit.Add_to_JSON_file(file, text)
"""
assert indentation >= 0, "can not add negative indentation"
if filename.endswith(".json"):
with io.open(filename, "w") as f:
json.dump(add, f, indent=indentation)
else:
raise TypeError("this is not a json file !!!!!!!!!!!!!!!")
def Add_to_txt_file(self, file_path: str, text_to_add: str, replace: bool = True):
"""
adding some data to `txt` file
`replace: bool` if replace then replace all the content of `txt` file and add the new text
"""
if file_path.endswith(".text"):
with io.open(file_path, "w" if replace else 'a') as f:
f.write(text_to_add)
else:
raise TypeError("this is not a text file !!!!!!!!!!!!!!!")
class bg_remover:
"""
removes the Background from the image\n
`image_path` the path to the image to remove\n
`image_output_path` the path followed by the file name without Extention for saving
-----
Examble
-----
>>> from TOOLS.File_Handler import bg_remover
path = 'path/to/image'
image_remover = bg_remover(image_path = path, image_output_path = "path/for/saving/removed/image/image name")\n
if you want to return the image as PIL you can use `return_image_as_pil()`\n
>>> photo = image_remover.return_image_as_pil()\n
if you want the path of the output or input you can use:
>>> print(image_remover._image_output_path)
>>> print(image_remover._image_output_name)
>>> print(image_remover._image_path)
"""
def __init__(self, image_path: str, image_output_path: str) -> None:
self._image_path = image_path
self._image_output_path = image_output_path + ".png"
self._image_types: tuple = (
".jpg",
".jpeg",
".png",
".gif",
".bmp",
".tif",
".tiff",
)
_, extention = os.path.splitext(image_path)
if extention in self._image_types:
photo = cv2.imread(self._image_path)
print("Please wait while removing the Background")
self.__removed = rembg.remove(photo)
cv2.imwrite(self._image_output_path, self.__removed) # type: ignore
else:
raise TypeError(
f"Extention {extention} is unrecognized, this is not an image"
)
def return_image_as_pil(self, size: tuple[int, int] = (200, 200)) -> PhotoImage:
"return the removed image as PIL for UI like for using it on tkinter"
return ImageTk.PhotoImage(Image.open(self._image_output_path).resize(size)) # type: ignore