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
|
# Copyright © 2012 Umang Varma <umang.me@gmail.com>
#
# This file is part of indicator-stickynotes.
#
# indicator-stickynotes is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# indicator-stickynotes is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# indicator-stickynotes. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime
import uuid
import json
from os.path import expanduser
SETTINGS_FILE = "stickynotesrc"
class Note:
def __init__(self, content=None, gui_class=None, noteset=None):
content = content or {}
self.uuid = content.get('uuid')
self.body = content.get('body','')
self.properties = content.get("properties", {})
last_modified = content.get('last_modified')
if last_modified:
self.last_modified = datetime.strptime(last_modified,
"%Y-%m-%dT%H:%M:%S")
else:
self.last_modified = datetime.now()
self.gui_class = gui_class
self.gui = None
self.noteset = noteset
def extract(self):
self.gui.update_note()
if not self.uuid:
self.uuid = str(uuid.uuid4())
self.properties = self.gui.properties()
return {"uuid":self.uuid, "body":self.body,
"last_modified":self.last_modified.strftime(
"%Y-%m-%dT%H:%M:%S"), "properties":self.properties}
def update(self,body=None):
if not body == None:
self.body = body
self.last_modified = datetime.now()
def delete(self):
self.noteset.notes.remove(self)
self.noteset.save()
del self
def show(self, *args):
if not self.gui:
self.gui = self.gui_class(note=self)
self.gui.show(*args)
def hide(self):
self.gui.hide()
class NoteSet:
def __init__(self, gui_class):
self.notes = []
self.gui_class = gui_class
def _loads_updater(self, dnoteset):
"""Parses old versions of the Notes structure and updates them"""
return dnoteset
def loads(self, snoteset):
"""Loads notes into their respective objects"""
notes = self._loads_updater(json.loads(snoteset))
self.notes = [Note(note, gui_class=self.gui_class, noteset=self)
for note in notes.get("notes",[])]
def dumps(self):
return json.dumps({"notes":[x.extract() for x in self.notes]})
def save(self, path=''):
output = self.dumps()
with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)),
mode='w', encoding='utf-8') as fsock:
fsock.write(output)
def open(self, path=''):
with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)),
encoding='utf-8') as fsock:
self.loads(fsock.read())
def new(self):
"""Creates a new note and adds it to the note set"""
note = Note(gui_class=self.gui_class, noteset=self)
self.notes.append(note)
note.show()
return note
def showall(self, *args):
for note in self.notes:
note.show(*args)
def hideall(self, *args):
self.save()
for note in self.notes:
note.hide(*args)
class dGUI:
def __init__(self, *args, **kwargs):
pass
"""Dummy GUI"""
def show(self):
pass
def hide(self):
pass
def update_note(self):
pass
def properties(self):
return None
|