This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jnote.py
202 lines (185 loc) · 7.43 KB
/
jnote.py
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
from mdx_gfm import GithubFlavoredMarkdownExtension
from PyQt5.QtCore import pyqtSlot, pyqtProperty
from version_parser.version import Version
from requests import get, RequestException
from markdown import markdown
from tempfile import mkstemp
from fileio import FileIO
import webbrowser
import subprocess
import traceback
import datetime
import sys
import os
import re
# Typing Imports
from typing import Dict, List, Tuple, Any
class JNote(FileIO):
"""Class Exposed to QML"""
def __init__(self) -> None:
FileIO.__init__(self)
self.__updateInfo: Dict[str, str] = {}
self.__cleanupFiles: List[Tuple[int, str]] = []
@pyqtSlot(bool)
def checkUpdates(self, isStartup: bool) -> None:
"""Check For Updates"""
try:
url: str = (
"https://api.github.com/repos/Dev-I-J/JNote/releases/latest"
)
with get(url) as r:
currentVersionStr: str = "v1.6.13"
currentVersion: Version = Version(currentVersionStr)
newVersionStr: str = r.json()['tag_name']
newVersion: Version = Version(newVersionStr)
if currentVersion < newVersion:
raw_info: str = markdown(r.json()['body'], extensions=[
GithubFlavoredMarkdownExtension()
])
info: str = raw_info.partition(
"<h1>Downloads Table</h1>")[0]
date: str = r.json()['published_at'][0:10]
self.updateInfo["newVersion"] = newVersionStr
self.updateInfo["currentVersion"] = currentVersionStr
self.updateInfo["details"] = info
self.updateInfo["date"] = date
self.updateAvailable.emit()
elif currentVersion > newVersion:
self.apiError.emit()
else:
if not isStartup:
self.updateInfo["currentVersion"] = currentVersionStr
self.upToDate.emit()
except RequestException:
self.apiConnectError.emit()
except KeyError:
self.apiError.emit()
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
@pyqtSlot(str, str, bool, bool, result=list)
def findText(
self, pattern: str, text: str, casesensitive: bool, regex: bool
) -> List[List[int]]:
"""Find Given Text"""
try:
result: List[List[int]] = []
if regex:
if not casesensitive:
for match in re.finditer(pattern, text, re.IGNORECASE):
result.append([match.span()[0], match.span()[1]])
else:
for match in re.finditer(pattern, text):
result.append([match.span()[0], match.span()[1]])
else:
pattern = re.escape(pattern)
if not casesensitive:
for match in re.finditer(pattern, text, re.IGNORECASE):
result.append([match.span()[0], match.span()[1]])
else:
for match in re.finditer(pattern, text):
result.append([match.span()[0], match.span()[1]])
return result
except re.error as e:
self.regexError.emit(pattern, e.msg)
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
return []
@pyqtSlot(bool, str)
def render(self, md: bool, source: str) -> None:
try:
file, name = mkstemp(suffix=".html", text=True)
self.__cleanupFiles.append((file, name))
with open(name, "w") as tmpFile:
tmpFile.write(source if not md else markdown(source))
webbrowser.open_new_tab(name)
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
@pyqtSlot(str)
def shellExec(self, script: str) -> None:
try:
if sys.platform == "win32":
file, name = mkstemp(suffix=".bat", text=True)
self.__cleanupFiles.append((file, name))
with open(name, "w") as tmpFile:
tmpFile.write(script)
subprocess.run(f"start cmd /k {name}", shell=True)
elif sys.platform == "darwin":
file, name = mkstemp(suffix=".sh", text=True)
self.__cleanupFiles.append((file, name))
with open(name, "w") as tmpFile:
tmpFile.write(script)
subprocess.run(f"open -W -a Terminal.app {name}")
else:
self.platformNotSupported.emit(sys.platform)
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
@pyqtSlot()
def clean(self) -> None:
try:
self.__addComments()
for file in self.__cleanupFiles:
try:
os.close(file[0])
os.remove(file[1])
except OSError:
pass
except Exception:
self.fatalError.emit(traceback.format_exc())
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
@staticmethod
def __addComments() -> None:
comments: str = """\
# DO NOT DELETE OR MODIFY THIS FILE! DOING SO WILL DAMAGE JNOTE!!
# This File is Automatically Generated and is not for adding
# user's preferences.
# These are some small settings used to enhance your experience with
# JNote but not to, Edit or DELETE.
# Doing so will do nothing but only ruin your experience!!!
"""
with open("settings.toml", "r") as settings:
settingsString: str = settings.read()
with open("settings.toml", "w") as settings:
settings.write(comments + settingsString)
@pyqtProperty(str, constant=True)
def about(self) -> str:
"""About JNote"""
try:
with open("data/about.html", "r", encoding="utf-8") as aboutfile:
return aboutfile.read()
except FileNotFoundError:
self.readmeFileNotFound.emit()
return "data/about.html Not Found."
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
return ""
@pyqtProperty(str, constant=True)
def gplLicense(self) -> str:
"""GNU GPL License"""
try:
with open(
"data/license.html", "r", encoding="utf-8") as licensefile:
return licensefile.read()
except FileNotFoundError:
self.licenseFileNotFound.emit()
return "data/license.html Not Found."
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
return ""
@pyqtProperty("QVariant", constant=True)
def updateInfo(self) -> Dict[str, str]:
"""Update Information"""
return self.__updateInfo
@pyqtProperty(str)
def dateTime(self) -> str:
try:
dtobject: datetime = datetime.datetime.now()
datetimestr: str = dtobject.strftime("%I:%M %p %d/%m/%Y")
self.dateTimeInserted.emit()
return datetimestr
except Exception:
self.fatalerror.emit()
return ""
@updateInfo.setter
def updateInfo(self, arg: Any) -> None:
self.__updateInfo = arg