This repository was archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
190 lines (165 loc) · 5.73 KB
/
index.py
File metadata and controls
190 lines (165 loc) · 5.73 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
import sys
import eel
import tkinter
import tkinter.filedialog as TkFile
import json
import ctypes
import mammoth
def hideConsole():
whnd = ctypes.windll.kernel32.GetConsoleWindow()
if whnd != 0:
ctypes.windll.user32.ShowWindow(whnd, 0)
from python_src.constants.config_names import *
from python_src.helpers.database.document_helper import DBHelper
tk = tkinter.Tk()
tk.geometry('0x0+0+0')
tk.withdraw()
tk.lift()
config_data={
"name":"TextEd",
"folder_name":"TextEd",
"db_file_name":"texted_db.json"
}
# Imports path config
database_folder_path=get_db_path(config_data)
print(database_folder_path)
# Init tiny db object
tinydb_helper=DBHelper(database_folder_path)
@eel.expose
def save_file(data: str, path: str,doc_filename:str, force: bool):
''' The data passed here is suppose to be of type string '''
try:
tk.lift()
print(path)
if(path == "empty"):
extenion = [("TextEd File", "*.texted")]
file_to_write_in = TkFile.asksaveasfile(
filetypes=extenion, defaultextension='.texted',initialfile=doc_filename)
print(data)
if(file_to_write_in!=None):
#Saving data
file_to_write_in.write(data)
file_to_write_in.close()
filename_name = file_to_write_in.name.split('/')[-1]
print(file_to_write_in.name.split('/'))
dict_obj={
"filename": filename_name,
"path": file_to_write_in.name,
}
tinydb_helper.save_or_update_document(dict_obj)
return dict_obj
return None
elif (force == True):
extenion = [("TextEd File", "*.texted")]
file_to_write_in = TkFile.asksaveasfile(
filetypes=extenion, defaultextension='.texted', initialfile=path.split('/')[-1])
if(file_to_write_in!=None):
#Saving data
file_to_write_in.write(data)
file_to_write_in.close()
filename_name = file_to_write_in.name.split('/')[-1]
dict_obj={
"filename": filename_name,
"path": file_to_write_in.name,
}
tinydb_helper.save_or_update_document(dict_obj)
return dict_obj
return None
else:
file_to_write_in = open(path, "w")
file_to_write_in.write(data)
file_to_write_in.close()
dict_obj= {
"filename": path.split('/')[-1],
"path": path,
}
tinydb_helper.save_or_update_document(dict_obj)
return dict_obj
except:
return "AN ERROR OCCURED"
@eel.expose
def get_recents_documents():
return tinydb_helper.get_documents(5)
@eel.expose
def open_file_from_path(path:str):
file_open=open(path,"r")
if(file_open.readable()):
return json.dumps({
"filename": path.split('/')[-1],
"path": path,
"content": file_open.read()
})
return None
@eel.expose
def open_file():
''' This method is to open file '''
try:
tk.lift()
extenion = [("TextEd File", "*.texted"),("Word document",".docx")]
#Open file
file_open = TkFile.askopenfile(
filetypes=extenion, defaultextension='.texted', mode="r")
if(file_open != None):
filename_name =file_open.name.split('/')[-1]
print(filename_name.split('.')[-1])
if(filename_name.split('.')[-1]=="docx"):
print('Is a document word')
docx_file=open(file_open.name, "rb")
result = mammoth.convert_to_html(docx_file)
return json.dumps({
"filename": filename_name,
"path": None,
"content": result.value
})
# json_file_content=json.loads(file_open.read())
# print(json_file_content)
else :
return json.dumps({
"filename": filename_name,
"path": file_open.name,
"content": file_open.read()
})
except:
return None
@eel.expose
def open_new_intance():
init()
def init():
if(len(sys.argv)>1):
if(sys.argv[1] == "--develop"):
# Init the project
eel.init("public")
# Need to define also the start launcher
eel.start({
'port': 3000,
'host': 'localhost'
}, options={
'port': 8888,
'host': 'localhost'
}, suppress_error=True, size=(int(tk.winfo_screenwidth()), int(tk.winfo_screenheight())))
else:
try:
eel_kwargs = dict(
host='localhost',
port=8888,
chromeFlags=["--start-fullscreen", "--browser-startup-dialog"]
)
eel.init('build',[".js",".html",".tsx",".ts",".jsx"])
eel.start({
'port': 8888,
'host': 'localhost'
},size=(int(tk.winfo_screenwidth()), int(
tk.winfo_screenheight())), suppress_error=True, **eel_kwargs)
except EnvironmentError:
# If Chrome isn't found, fallback to Microsoft Edge on Win10 or greater
if sys.platform in ['win32', 'win64'] and int(platform.release()) >= 10:
eel.start("index.html", mode='edge',size=(int(tk.winfo_screenwidth()), int(
tk.winfo_screenheight())), suppress_error=True,**eel_kwargs)
else:
raise
@eel.expose
def exit_eel():
sys.exit(0)
if __name__ == "__main__":
# hideConsole()
init()