~stewart/mythremote/quickly_trunk

« back to all changes in this revision

Viewing changes to mythremote_lib/preferences.py

  • Committer: Stewart Smith
  • Date: 2012-02-05 04:40:51 UTC
  • Revision ID: stewart@flamingspork.com-20120205044051-b86m7edpcxosnziq
Initial project creation with Quickly!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
3
# This file is in the public domain
 
4
### END LICENSE
 
5
 
 
6
"""Provides a shared preferences dictionary"""
 
7
 
 
8
from desktopcouch.records.server import CouchDatabase
 
9
from desktopcouch.records.record import Record
 
10
import gtk
 
11
import gobject
 
12
 
 
13
class User_dict(dict):
 
14
    ''' a dictionary with extra methods:
 
15
 
 
16
    persistence: load, save and db_connect
 
17
    gobject signals: connect and emit.
 
18
    
 
19
    Don't use this directly. Please use the preferences instance.'''
 
20
    
 
21
    def __init__(self, *args, **kwds):
 
22
        dict.__init__(self, *args, **kwds)
 
23
        # Set up couchdb.
 
24
        self._db_name = "mythremote"
 
25
        self._key = None
 
26
        self._database = None
 
27
        
 
28
        self._record_type = (
 
29
            "http://wiki.ubuntu.com/Quickly/RecordTypes/Mythremote/"
 
30
            "Preferences")
 
31
        
 
32
        class Publisher(gtk.Invisible): # pylint: disable=R0904
 
33
            '''set up signals in a separate class
 
34
            
 
35
            gtk.Invisible has 230 public methods'''
 
36
            __gsignals__ = {'changed' : (gobject.SIGNAL_RUN_LAST,
 
37
                 gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
 
38
                 'loaded' : (gobject.SIGNAL_RUN_LAST,
 
39
                 gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))}
 
40
        
 
41
        publisher = Publisher()
 
42
        self.emit  = publisher.emit
 
43
        self.connect  = publisher.connect
 
44
 
 
45
    def db_connect(self):
 
46
        '''connect to couchdb
 
47
        
 
48
        create if necessary'''
 
49
        # logging.basicConfig will be called now
 
50
        self._database = CouchDatabase(self._db_name, create=True)
 
51
 
 
52
    def save(self):
 
53
        'save to couchdb'
 
54
        self._database.update_fields(self._key, self)
 
55
 
 
56
 
 
57
    def load(self):
 
58
        'load from couchdb'
 
59
        self.update({"record_type": self._record_type})
 
60
 
 
61
        results = self._database.get_records(
 
62
            record_type=self._record_type, create_view=True)
 
63
 
 
64
        if len(results.rows) == 0:
 
65
            # No preferences have ever been saved
 
66
            # save them before returning.
 
67
            self._key = self._database.put_record(Record(self))
 
68
        else:
 
69
            self.update(results.rows[0].value)
 
70
            del self['_rev']
 
71
            self._key = results.rows[0].value["_id"]
 
72
        self.emit('loaded', None)
 
73
 
 
74
    def update(self, *args, **kwds):
 
75
        ''' interface for dictionary
 
76
        
 
77
        send changed signal when appropriate '''
 
78
        
 
79
        # parse args
 
80
        new_data = {}
 
81
        new_data.update(*args, **kwds)
 
82
 
 
83
        changed_keys = []
 
84
        for key in new_data.keys():
 
85
            if new_data.get(key) != dict.get(self, key):
 
86
                changed_keys.append(key)
 
87
        dict.update(self, new_data)
 
88
        if changed_keys:
 
89
            self.emit('changed', tuple(changed_keys))
 
90
 
 
91
    def __setitem__(self, key, value):
 
92
        ''' interface for dictionary
 
93
        
 
94
        send changed signal when appropriate '''
 
95
        if value != dict.get(self, key):
 
96
            dict.__setitem__(self, key, value)
 
97
            self.emit('changed', (key,))
 
98
 
 
99
preferences = User_dict()
 
100