Skip to content

Commit f21d951

Browse files
authored
Update ViewMate.py
added .json, .log support error handling corrected fixed typo's
1 parent 0e31bc1 commit f21d951

File tree

1 file changed

+55
-14
lines changed

1 file changed

+55
-14
lines changed

ViewMate.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#ViewMate File Reader
2-
#supports txt,lrc,csv,dat,py,html,css
2+
#supports txt,lrc,csv,dat,py,html,css,json,log
33
import termcolor
44
from termcolor import colored
55
import os
66
import pickle
77
import csv
88
import bs4
99
from bs4 import BeautifulSoup
10+
import json
1011

1112
def load_txt(path):
1213
try:
@@ -17,7 +18,7 @@ def load_txt(path):
1718
except FileNotFoundError:
1819
print(colored("File Not Found!","red",attrs=["bold"]))
1920
except Exception as e:
20-
print(f"an error occured: {e}")
21+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
2122

2223
def load_lrc(path):
2324
try:
@@ -28,19 +29,19 @@ def load_lrc(path):
2829
except FileNotFoundError:
2930
print(colored("File Not Found!","red",attrs=["bold"]))
3031
except Exception as e:
31-
print(f"an error occured: {e}")
32+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
3233

3334
def load_csv(path):
3435
try:
35-
with open(path,"r") as file:
36+
with open(path,"r",encoding="utf-8") as file:
3637
f = csv.reader(file)
3738
print(colored("File Found!","green",attrs=["bold"]))
3839
for row in f:
3940
print(row)
4041
except FileNotFoundError:
4142
print(colored("File Not Found!","red",attrs=["bold"]))
4243
except Exception as e:
43-
print(f"an error occured: {e}")
44+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
4445

4546
def load_py(path):
4647
try:
@@ -51,7 +52,7 @@ def load_py(path):
5152
except FileNotFoundError:
5253
print(colored("File Not Found!","red",attrs=["bold"]))
5354
except Exception as e:
54-
print(f"an error occured: {e}")
55+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
5556

5657
def load_dat(path):
5758
try:
@@ -74,24 +75,60 @@ def load_dat(path):
7475
print(colored("trying to decode text...","yellow",attrs=["bold"]))
7576
try:
7677
text = data.decode("utf-8")
77-
print(colored(f"decided text: \n{text}","yellow"))
78+
print(colored(f"decoded text: \n{text}","yellow"))
7879
except UnicodeDecodeError:
7980
print(colored("File is not valid utf-8 text!","red",attrs=["bold"]))
8081
except FileNotFoundError:
8182
print(colored("File Not Found!","red",attrs=["bold"]))
8283
except Exception as e:
83-
print(f"an error occured: {e}")
84+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
8485

8586
def load_html(path):
86-
with open(path,"r",encoding="utf-8") as file:
87-
soup = BeautifulSoup(file,"html.parser")
88-
print(soup.prettify())
87+
try:
88+
with open(path,"r",encoding="utf-8") as file:
89+
soup = BeautifulSoup(file,"html.parser")
90+
print(soup.prettify())
91+
except FileNotFoundError:
92+
print(colored("File Not Found!","red",attrs=["bold"]))
93+
except Exception as e:
94+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
95+
8996

9097
def load_css(path):
91-
with open(path,"r",encoding="utf-8") as file:
92-
content=file.read()
93-
print(content)
98+
try:
99+
with open(path,"r",encoding="utf-8") as file:
100+
content=file.read()
101+
print(content)
102+
except FileNotFoundError:
103+
print(colored("File Not Found!","red",attrs=["bold"]))
104+
except Exception as e:
105+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
94106

107+
def load_json(path):
108+
try:
109+
with open(path,"r",encoding="utf-8") as file:
110+
data = json.load(file)
111+
print(data)
112+
except FileNotFoundError:
113+
print(colored("File Not Found!","red",attrs=["bold"]))
114+
except json.JSONDecodeError:
115+
print(colored("invalid JSON Format!","red",attrs=["bold"]))
116+
except Exception as e:
117+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
118+
119+
def load_log(path):
120+
try:
121+
with open(path,"r",encoding="utf-8") as file:
122+
print(colored("File Found!","green",attrs=["bold"]))
123+
for line in file:
124+
print(line.strip())
125+
except FileNotFoundError:
126+
print(colored("File Not Found!","red",attrs=["bold"]))
127+
except UnicodeDecodeError:
128+
print(colored("Error: File encoding is not UTF-8!","red",attrs=["bold"]))
129+
except Exception as e:
130+
print(colored(f"an error occured: {e}","red",attrs=["bold"]))
131+
95132
def load(path):
96133
parts = path.split(".")
97134
if len(parts) > 1:
@@ -112,6 +149,10 @@ def load(path):
112149
load_html(path)
113150
elif format == "css":
114151
load_css(path)
152+
elif format == "json":
153+
load_json(path)
154+
elif format == "log":
155+
load_log(path)
115156
else:
116157
print(colored("Unsupported File Format!","red",attrs=["bold"]))
117158

0 commit comments

Comments
 (0)