~umang/indicator-stickynotes/trunk

« back to all changes in this revision

Viewing changes to stickynotes/backend.py

  • Committer: Umang Varma
  • Date: 2012-06-24 20:43:03 UTC
  • Revision ID: git-v1:7154d9e47e06f4ca05d7f5a2acdd48d6d6efb3a7
Added .desktop and setup.py files.

In addition stick.py was moved to indicator-stickynotes.py so that it looks more
like its final installed name.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright © 2012-2015 Umang Varma <umang.me@gmail.com>
 
1
# Copyright © 2012 Umang Varma <umang.me@gmail.com>
2
2
3
3
# This file is part of indicator-stickynotes.
4
4
20
20
import json
21
21
from os.path import expanduser
22
22
 
23
 
from stickynotes.info import FALLBACK_PROPERTIES
 
23
SETTINGS_FILE = "stickynotesrc"
24
24
 
25
25
class Note:
26
 
    def __init__(self, content=None, gui_class=None, noteset=None,
27
 
            category=None):
28
 
        self.gui_class = gui_class
29
 
        self.noteset = noteset
 
26
    def __init__(self, content=None, gui_class=None, noteset=None):
30
27
        content = content or {}
31
28
        self.uuid = content.get('uuid')
32
29
        self.body = content.get('body','')
33
30
        self.properties = content.get("properties", {})
34
 
        self.category = category or content.get("cat", "")
35
 
        if not self.category in self.noteset.categories:
36
 
            self.category = ""
37
31
        last_modified = content.get('last_modified')
38
32
        if last_modified:
39
33
            self.last_modified = datetime.strptime(last_modified,
40
34
                    "%Y-%m-%dT%H:%M:%S")
41
35
        else:
42
36
            self.last_modified = datetime.now()
43
 
        # Don't create GUI until show is called
 
37
        self.gui_class = gui_class
44
38
        self.gui = None
 
39
        self.noteset = noteset
45
40
 
46
41
    def extract(self):
 
42
        self.gui.update_note()
47
43
        if not self.uuid:
48
44
            self.uuid = str(uuid.uuid4())
49
 
        if self.gui != None:
50
 
            self.gui.update_note()
51
 
            self.properties = self.gui.properties()
 
45
        self.properties = self.gui.properties()
52
46
        return {"uuid":self.uuid, "body":self.body,
53
47
                "last_modified":self.last_modified.strftime(
54
 
                    "%Y-%m-%dT%H:%M:%S"), "properties":self.properties,
55
 
                "cat": self.category}
 
48
                    "%Y-%m-%dT%H:%M:%S"), "properties":self.properties}
56
49
 
57
50
    def update(self,body=None):
58
51
        if not body == None:
64
57
        self.noteset.save()
65
58
        del self
66
59
 
67
 
    def show(self, *args, **kwargs):
68
 
        # If GUI has not been created, create it now
69
 
        if self.gui == None:
 
60
    def show(self, *args):
 
61
        if not self.gui:
70
62
            self.gui = self.gui_class(note=self)
71
 
        else:
72
 
            self.gui.show(*args, **kwargs)
 
63
        self.gui.show(*args)
73
64
 
74
65
    def hide(self):
75
 
        if self.gui != None:
76
 
            self.gui.hide()
77
 
 
78
 
    def set_locked_state(self, locked):
79
 
        # if gui hasn't been initialized, just change the property
80
 
        if self.gui == None:
81
 
            self.properties["locked"] = locked
82
 
        else:
83
 
            self.gui.set_locked_state(locked)
84
 
 
85
 
    def cat_prop(self, prop):
86
 
        """Gets a property of the note's category"""
87
 
        return self.noteset.get_category_property(self.category, prop)
 
66
        self.gui.hide()
88
67
 
89
68
 
90
69
class NoteSet:
91
 
    def __init__(self, gui_class, data_file, indicator):
 
70
    def __init__(self, gui_class):
92
71
        self.notes = []
93
 
        self.properties = {}
94
 
        self.categories = {}
95
72
        self.gui_class = gui_class
96
 
        self.data_file = data_file
97
 
        self.indicator = indicator
98
73
 
99
74
    def _loads_updater(self, dnoteset):
100
75
        """Parses old versions of the Notes structure and updates them"""
103
78
    def loads(self, snoteset):
104
79
        """Loads notes into their respective objects"""
105
80
        notes = self._loads_updater(json.loads(snoteset))
106
 
        self.properties = notes.get("properties", {})
107
 
        self.categories = notes.get("categories", {})
108
81
        self.notes = [Note(note, gui_class=self.gui_class, noteset=self)
109
82
                for note in notes.get("notes",[])]
110
83
 
111
84
    def dumps(self):
112
 
        return json.dumps({"notes":[x.extract() for x in self.notes],
113
 
            "properties": self.properties, "categories": self.categories})
 
85
        return json.dumps({"notes":[x.extract() for x in self.notes]})
114
86
 
115
87
    def save(self, path=''):
116
88
        output = self.dumps()
117
 
        with open(path or expanduser(self.data_file),
 
89
        with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)),
118
90
                mode='w', encoding='utf-8') as fsock:
119
91
            fsock.write(output)
120
92
 
121
93
    def open(self, path=''):
122
 
        with open(path or expanduser(self.data_file), 
 
94
        with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)), 
123
95
                encoding='utf-8') as fsock:
124
96
            self.loads(fsock.read())
125
97
 
126
 
    def load_fresh(self):
127
 
        """Load empty data"""
128
 
        self.loads('{}')
129
 
        self.new()
130
 
 
131
 
    def merge(self, data):
132
 
        """Update notes based on new data"""
133
 
        jdata = self._loads_updater(json.loads(data))
134
 
        self.hideall()
135
 
        # update categories
136
 
        if "categories" in jdata:
137
 
            self.categories.update(jdata["categories"])
138
 
        # make a dictionary of notes so we can modify existing notes
139
 
        dnotes = {n.uuid : n for n in self.notes}
140
 
        for newnote in jdata.get("notes", []):
141
 
            if "uuid" in newnote and newnote["uuid"] in dnotes:
142
 
                # Update notes that are already in the noteset
143
 
                orignote = dnotes[newnote["uuid"]]
144
 
                if "body" in newnote:
145
 
                    orignote.body = newnote["body"]
146
 
                if "properties" in newnote:
147
 
                    orignote.properties = newnote["properties"]
148
 
                if "cat" in newnote:
149
 
                    orignote.category = newnote["cat"]
150
 
            else:
151
 
                # otherwise create a new note
152
 
                if "uuid" in newnote:
153
 
                    uuid = newnote["uuid"]
154
 
                else:
155
 
                    uuid = str(uuid.uuid4())
156
 
                dnotes[uuid] = Note(newnote, gui_class=self.gui_class,
157
 
                        noteset=self)
158
 
        # copy notes over from dictionary to list
159
 
        self.notes = list(dnotes.values())
160
 
        self.showall(reload_from_backend=True)
161
 
 
162
98
    def new(self):
163
99
        """Creates a new note and adds it to the note set"""
164
 
        note = Note(gui_class=self.gui_class, noteset=self,
165
 
                category=self.properties.get("default_cat", ""))
 
100
        note = Note(gui_class=self.gui_class, noteset=self)
166
101
        self.notes.append(note)
167
102
        note.show()
168
103
        return note
169
104
 
170
 
    def showall(self, *args, **kwargs):
 
105
    def showall(self, *args):
171
106
        for note in self.notes:
172
 
            note.show(*args, **kwargs)
173
 
        self.properties["all_visible"] = True
 
107
            note.show(*args)
174
108
 
175
109
    def hideall(self, *args):
176
110
        self.save()
177
111
        for note in self.notes:
178
112
            note.hide(*args)
179
 
        self.properties["all_visible"] = False
180
 
 
181
 
    def get_category_property(self, cat, prop):
182
 
        """Get a property of a category or the default"""
183
 
        if ((not cat) or (not cat in self.categories)) and \
184
 
                self.properties.get("default_cat", None):
185
 
            cat = self.properties["default_cat"]
186
 
        cat_data = self.categories.get(cat, {})
187
 
        if prop in cat_data:
188
 
            return cat_data[prop]
189
 
        # Otherwise, use fallback categories
190
 
        if prop in FALLBACK_PROPERTIES:
191
 
            return FALLBACK_PROPERTIES[prop]
192
 
        else:
193
 
            raise ValueError("Unknown property")
194
113
 
195
114
class dGUI:
 
115
    def __init__(self, *args, **kwargs):
 
116
        pass
196
117
    """Dummy GUI"""
197
 
    def __init__(self, *args, **kwargs):
198
 
        pass
199
118
    def show(self):
200
119
        pass
201
120
    def hide(self):