1
1
#ViewMate File Reader
2
- #supports txt,lrc,csv,dat,py,html,css
2
+ #supports txt,lrc,csv,dat,py,html,css,json,log
3
3
import termcolor
4
4
from termcolor import colored
5
5
import os
6
6
import pickle
7
7
import csv
8
8
import bs4
9
9
from bs4 import BeautifulSoup
10
+ import json
10
11
11
12
def load_txt (path ):
12
13
try :
@@ -17,7 +18,7 @@ def load_txt(path):
17
18
except FileNotFoundError :
18
19
print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
19
20
except Exception as e :
20
- print (f"an error occured: { e } " )
21
+ print (colored ( f"an error occured: { e } " , "red" , attrs = [ "bold" ]) )
21
22
22
23
def load_lrc (path ):
23
24
try :
@@ -28,19 +29,19 @@ def load_lrc(path):
28
29
except FileNotFoundError :
29
30
print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
30
31
except Exception as e :
31
- print (f"an error occured: { e } " )
32
+ print (colored ( f"an error occured: { e } " , "red" , attrs = [ "bold" ]) )
32
33
33
34
def load_csv (path ):
34
35
try :
35
- with open (path ,"r" ) as file :
36
+ with open (path ,"r" , encoding = "utf-8" ) as file :
36
37
f = csv .reader (file )
37
38
print (colored ("File Found!" ,"green" ,attrs = ["bold" ]))
38
39
for row in f :
39
40
print (row )
40
41
except FileNotFoundError :
41
42
print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
42
43
except Exception as e :
43
- print (f"an error occured: { e } " )
44
+ print (colored ( f"an error occured: { e } " , "red" , attrs = [ "bold" ]) )
44
45
45
46
def load_py (path ):
46
47
try :
@@ -51,7 +52,7 @@ def load_py(path):
51
52
except FileNotFoundError :
52
53
print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
53
54
except Exception as e :
54
- print (f"an error occured: { e } " )
55
+ print (colored ( f"an error occured: { e } " , "red" , attrs = [ "bold" ]))
55
56
56
57
def load_dat (path ):
57
58
try :
@@ -74,24 +75,60 @@ def load_dat(path):
74
75
print (colored ("trying to decode text..." ,"yellow" ,attrs = ["bold" ]))
75
76
try :
76
77
text = data .decode ("utf-8" )
77
- print (colored (f"decided text: \n { text } " ,"yellow" ))
78
+ print (colored (f"decoded text: \n { text } " ,"yellow" ))
78
79
except UnicodeDecodeError :
79
80
print (colored ("File is not valid utf-8 text!" ,"red" ,attrs = ["bold" ]))
80
81
except FileNotFoundError :
81
82
print (colored ("File Not Found!" ,"red" ,attrs = ["bold" ]))
82
83
except Exception as e :
83
- print (f"an error occured: { e } " )
84
+ print (colored ( f"an error occured: { e } " , "red" , attrs = [ "bold" ]) )
84
85
85
86
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
+
89
96
90
97
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" ]))
94
106
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
+
95
132
def load (path ):
96
133
parts = path .split ("." )
97
134
if len (parts ) > 1 :
@@ -112,6 +149,10 @@ def load(path):
112
149
load_html (path )
113
150
elif format == "css" :
114
151
load_css (path )
152
+ elif format == "json" :
153
+ load_json (path )
154
+ elif format == "log" :
155
+ load_log (path )
115
156
else :
116
157
print (colored ("Unsupported File Format!" ,"red" ,attrs = ["bold" ]))
117
158
0 commit comments