~umang/indicator-stickynotes/trunk

« back to all changes in this revision

Viewing changes to backend.py

  • Committer: Umang Varma
  • Date: 2015-07-11 20:23:33 UTC
  • Revision ID: git-v1:17b868aeb1031ade9f7d4bb35b628dc3e4f71aa0
Updated Italian translations

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from datetime import datetime
2
 
import uuid
3
 
import json
4
 
from os.path import expanduser
5
 
 
6
 
SETTINGS_FILE = "stickynotesrc"
7
 
 
8
 
class Note:
9
 
    def __init__(self, content=None, gui_class=None, noteset=None):
10
 
        content = content or {}
11
 
        self.uuid = content.get('uuid')
12
 
        self.body = content.get('body','')
13
 
        self.properties = content.get("properties", {})
14
 
        last_modified = content.get('last_modified')
15
 
        if last_modified:
16
 
            self.last_modified = datetime.strptime(last_modified,
17
 
                    "%Y-%m-%dT%H:%M:%S")
18
 
        else:
19
 
            self.last_modified = datetime.now()
20
 
        self.gui_class = gui_class
21
 
        self.gui = None
22
 
        self.noteset = noteset
23
 
 
24
 
    def extract(self):
25
 
        self.gui.update_note()
26
 
        if not self.uuid:
27
 
            self.uuid = str(uuid.uuid4())
28
 
        return {"uuid":self.uuid, "body":self.body,
29
 
                "last_modified":self.last_modified.strftime(
30
 
                    "%Y-%m-%dT%H:%M:%S"), "properties":self.gui.properties()}
31
 
 
32
 
    def update(self,body=None):
33
 
        if not body == None:
34
 
            self.body = body
35
 
            self.last_modified = datetime.now()
36
 
 
37
 
    def delete(self):
38
 
        self.noteset.notes.remove(self)
39
 
        self.noteset.save()
40
 
        del self
41
 
 
42
 
    def show(self):
43
 
        if not self.gui:
44
 
            self.gui = self.gui_class(note=self)
45
 
        self.gui.show()
46
 
 
47
 
    def hide(self):
48
 
        self.gui.hide()
49
 
 
50
 
 
51
 
class NoteSet:
52
 
    def __init__(self, gui_class):
53
 
        self.notes = []
54
 
        self.gui_class = gui_class
55
 
 
56
 
    def _loads_updater(self, dnoteset):
57
 
        """Parses old versions of the Notes structure and updates them"""
58
 
        return dnoteset
59
 
 
60
 
    def loads(self, snoteset):
61
 
        """Loads notes into their respective objects"""
62
 
        notes = self._loads_updater(json.loads(snoteset))
63
 
        self.notes = [Note(note, gui_class=self.gui_class, noteset=self)
64
 
                for note in notes.get("notes",[])]
65
 
 
66
 
    def dumps(self):
67
 
        return json.dumps({"notes":[x.extract() for x in self.notes]})
68
 
 
69
 
    def save(self, path=''):
70
 
        with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)),
71
 
                mode='w', encoding='utf-8') as fsock:
72
 
            fsock.write(self.dumps())
73
 
 
74
 
    def open(self, path=''):
75
 
        with open(path or expanduser("~/.{0}".format(SETTINGS_FILE)), 
76
 
                encoding='utf-8') as fsock:
77
 
            self.loads(fsock.read())
78
 
 
79
 
    def new(self):
80
 
        """Creates a new note and adds it to the note set"""
81
 
        note = Note(gui_class=self.gui_class, noteset=self)
82
 
        self.notes.append(note)
83
 
        note.show()
84
 
        return note
85
 
 
86
 
    def showall(self):
87
 
        for note in self.notes:
88
 
            note.show()
89
 
 
90
 
    def hideall(self):
91
 
        for note in self.notes:
92
 
            note.hide()
93
 
 
94
 
class dGUI:
95
 
    def __init__(self, *args, **kwargs):
96
 
        pass
97
 
    """Dummy GUI"""
98
 
    def show(self):
99
 
        pass
100
 
    def hide(self):
101
 
        pass
102
 
    def update_note(self):
103
 
        pass
104
 
    def properties(self):
105
 
        return None
106