-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiary.py
272 lines (213 loc) · 8.12 KB
/
diary.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Module containing markdown-diary's actual Diary class."""
import os
import re
import datetime
import binascii
import tempfile
class Diary():
"""Class handling all the diary and note manipulation.
The Diary is a data container and manipulation class. It can create and
delete notes and diaries.
"""
def __init__(self, fname):
"""Init method that reads in a diary from a file.
Args:
fname (str): Path to the diary to be loaded.
"""
self.fname = fname
with open(fname) as f:
self.rawData = f.read()
self.checksum = binascii.crc32(bytes(self.rawData, encoding="UTF-8"))
self.data = self.extractData(self.rawData)
def updateDiaryOnDisk(self, newData):
"""Save all changes to the diary to disk.
If the diary's checksum changed in the meantime, abort the save.
Args:
newData (str): The whole diary as a string to be saved to disk.
"""
with open(self.fname) as f:
rawData = f.read()
checksum = binascii.crc32(bytes(rawData, encoding="UTF-8"))
if checksum == self.checksum:
newChecksum = binascii.crc32(bytes(newData, encoding="UTF-8"))
with tempfile.NamedTemporaryFile(
mode="w", prefix=".diary_", suffix=".tmp",
dir=os.path.dirname(self.fname), delete=False) as tmpf:
tmpf.write(newData)
os.replace(tmpf.name, self.fname)
self.rawData = newData
self.checksum = newChecksum
self.data = self.extractData(self.rawData)
else:
print("ERROR: Diary file was changed! Abort save.")
def saveNote(self, note, noteId, noteDate):
"""Save a new note to diary or update an existing one.
Args:
note (str): The note's contents.
noteId (str): UUID of the note.
noteDate (str): Note creation date.
"""
if any(noteId in metaDict["note_id"] for metaDict in self.data):
self.updateNote(note, noteId, noteDate)
else:
newData = self.rawData
newData += self.createNoteHeader(noteId, noteDate)
newData += note
self.updateDiaryOnDisk(newData)
@staticmethod
def createNoteHeader(noteId, noteDate):
"""Create a note metadata header.
Args:
noteId (str): UUID of the note
noteDate (str): Date of the note's creation
Returns:
Note header string.
"""
header = ("\n<!---\n"
"markdown-diary note metadata\n"
"note_id = ")
header += noteId
header += "\n--->\n"
header += noteDate
header += "\n\n"
return header
def updateNote(self, note, noteId, noteDate):
"""Update an existing note.
Args:
note (str): The note's new contents.
noteId (str): UUID of the note.
noteDate (str): Note creation date.
"""
reHeader = re.compile(
r"""^<!---
(?:\n|\r\n)
markdown-diary\ note\ metadata
(?:\n|\r\n)
note_id\ =\ # Hashtag for PEP8 compiance
""" + noteId +
r"""(.*?)
--->
""", re.MULTILINE | re.VERBOSE | re.DOTALL)
reHeaderNext = re.compile(
r'^<!---(?:\n|\r\n)markdown-diary note metadata(?:\n|\r\n)',
re.MULTILINE)
header = reHeader.search(self.rawData)
nextHeader = reHeaderNext.search(self.rawData, header.end())
newData = self.rawData[:header.end()]
newData += "\n"
newData += noteDate
newData += "\n\n"
newData += note
if nextHeader is not None:
# We need a newline separating note text from next header
if newData[-1] is not '\n':
newData += "\n"
newData += self.rawData[nextHeader.start():]
self.updateDiaryOnDisk(newData)
def deleteNote(self, noteId):
"""Delete a note from a diary.
Args:
noteId (str): UUID of the note to be deleted.
"""
reHeader = re.compile(
r"""^<!---
(?:\n|\r\n)
markdown-diary\ note\ metadata
(?:\n|\r\n)
note_id\ =\ # Hashtag for PEP8 compiance
""" + noteId +
r"""(.*?)
--->
""", re.MULTILINE | re.VERBOSE | re.DOTALL)
reHeaderNext = re.compile(
r'^<!---(?:\n|\r\n)markdown-diary note metadata(?:\n|\r\n)',
re.MULTILINE)
header = reHeader.search(self.rawData)
nextHeader = reHeaderNext.search(self.rawData, header.end())
newData = self.rawData[:header.start()]
if nextHeader is not None:
newData += "\n"
newData += self.rawData[nextHeader.start():]
self.updateDiaryOnDisk(newData)
@staticmethod
def extractData(rawData):
"""Get all notes' metadata and text from a diary.
Args:
diaryData (str): The whole diary as a string.
Returns:
A list of data dictionaries.
"""
reHeader = re.compile(
r"""^<!--- # Beggining of Markdown comment
(?:\n|\r\n) # Unix|Windows non-capturing \n
markdown-diary\ note\ metadata # Mandatory first line
(.*?) # Any characters including \n
---> # End of Markdown comment
""", re.MULTILINE | re.VERBOSE | re.DOTALL)
matches = list(reHeader.finditer(rawData))
data = []
for i, match in enumerate(matches):
dataDict = {}
for line in rawData[
match.start():match.end()].splitlines()[2:-1]:
key, val = line.partition("=")[::2]
dataDict[key.strip()] = val.strip()
date = rawData[match.end():].splitlines()[1]
title = rawData[match.end():].splitlines()[3].strip("# ")
text = ""
if i == len(matches) - 1:
text = rawData[matches[i].end():].split("\n", maxsplit=3)[3]
else:
text = rawData[matches[i].end(): matches[i + 1].start()].split("\n", maxsplit=3)[3]
dataDict["date"] = date
dataDict["title"] = title
dataDict["text"] = text
data.append(dataDict)
return data
def getNote(self, noteId):
"""Extract note text from diary.
Args:
noteId (str): UUID of the requested note.
Returns:
A single note's text.
"""
for datum in self.data:
if datum["note_id"] == noteId:
return datum["text"]
return None
def getNoteMetadata(self, noteId):
"""Get metadata of a single note.
Args:
noteId (str): UUID of the requested note.
Returns:
A metadata dictionary. Returns None if noteId not found.
"""
for datum in self.data:
if noteId == datum["note_id"]:
return datum
return None
def searchNotes(self, pattern):
"""Search for all notes containing 'pattern'.
Args:
pattern (string): Text to look for.
Returns:
A list of metadata of all matching notes.
"""
matching = []
for datum in self.data:
if pattern.lower() in datum["text"].lower():
matching.append(datum)
return matching
def changeNoteDate(self, noteId, newDate):
"""Change date of a note."""
self.saveNote(self.getNote(noteId), noteId, newDate)
@staticmethod
def isValidDate(date):
"""Check whether a date is of a valid format."""
try:
datetime.datetime.strptime(date, '%Y-%m-%d')
if len(date) != 10:
return False
return True
except ValueError:
return False