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
/
fileio.py
124 lines (115 loc) · 4.82 KB
/
fileio.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
from PyQt5.QtCore import pyqtSlot
from settings import Settings
from binaryornot import check
import traceback
import cchardet
import chardet
class FileIO(Settings):
"""All the File I/O stuff of JNote goes Here"""
def __init__(self) -> None:
Settings.__init__(self)
@ pyqtSlot()
def fileNew(self) -> None:
"""Update settings when a new document is created"""
try:
self.setSettingsBool("last-used-file", "untitled", True)
self.setSettingsStr("last-used-file", "encoding", "utf-8")
self.setSettingsStr("last-used-file", "path", "")
self.setSettingsStr("last-used-file", "text", "")
self.newDocumentCreated.emit()
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
@ pyqtSlot(str, result=str)
def fileOpen(self, fPath: str) -> str:
"""Open File"""
fileText: str = ""
try:
if not check.is_binary(fPath):
with open(fPath, "rb") as binaryFile:
binary: str = binaryFile.read()
coding: str = cchardet.detect(binary)["encoding"]
fileText: str = binary.decode(coding)
self.setSettingsBool("last-used-file", "untitled", False)
self.setSettingsStr("last-used-file", "path", fPath)
self.setSettingsStr("last-used-file", "encoding", coding)
self.fileOpenSuccessful.emit()
return fileText
else:
self.fileOpenError.emit()
except (UnicodeDecodeError, LookupError):
try:
with open(fPath, "rb") as binaryFile:
binary: str = binaryFile.read()
coding: str = chardet.detect(binary)["encoding"]
fileText: str = binary.decode(coding)
self.setSettingsBool("last-used-file", "untitled", False)
self.setSettingsStr("last-used-file", "path", fPath)
self.setSettingsStr("last-used-file", "encoding", coding)
self.fileOpenSuccessful.emit()
return fileText
except (UnicodeDecodeError, LookupError):
self.fileOpenError.emit()
except IOError:
self.fileHandleError.emit()
except Exception:
self.fatalError.emit(traceback.format_exc())
except FileNotFoundError:
self.fileNotFound.emit(fPath)
except IOError:
self.fileHandleError.emit()
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
return ""
@ pyqtSlot(str)
def fileSave(self, fText: str) -> None:
"""Save File"""
fPath: str = self.getSettings("last-used-file")["path"]
try:
untitled: bool = self.getSettings("last-used-file")["untitled"]
if not untitled:
fCoding: str = self.getSettings("last-used-file")["encoding"]
with open(fPath, "w", encoding=fCoding) as outFile:
outFile.write(fText)
self.fileSaved.emit()
else:
self.fileUntitled.emit()
except FileNotFoundError:
self.fileNotFound.emit(fPath)
except IOError:
self.fileHandleError.emit()
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
@ pyqtSlot(str, str)
def fileSaveAs(self, fPath: str, fText: str) -> None:
"""Save File As"""
try:
fCoding: str = self.getSettings("last-used-file")["encoding"]
with open(fPath, "w+", encoding=fCoding) as outFile:
outFile.write(fText)
self.fileSavedAs.emit()
except FileNotFoundError:
self.fileNotFound.emit(fPath)
except IOError:
self.fileHandleError.emit()
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
else:
self.setSettingsBool("last-used-file", "untitled", False)
self.setSettingsStr("last-used-file", "path", fPath)
@ pyqtSlot(result=str)
def openLastOpenFile(self) -> str:
"""(re)Open last opened file"""
fPath: str = self.getSettings("last-used-file")["path"]
try:
fCoding: str = self.getSettings("last-used-file")["encoding"]
with open(fPath, "r", encoding=fCoding) as text:
return text.read()
except FileNotFoundError:
self.fileNotFound.emit(fPath)
except UnicodeDecodeError:
self.fileOpenError.emit()
except IOError:
self.fileHandleError.emit()
except Exception as e:
self.fatalError.emit(str(e), traceback.format_exc())
return ""