~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/library/sqlite.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
from threading import RLock
15
15
from datetime import tzinfo, datetime, timedelta
16
16
 
17
 
from calibre.library import title_sort
 
17
from calibre.ebooks.metadata import title_sort
18
18
 
19
19
global_lock = RLock()
20
20
 
40
40
    if tz is not None:
41
41
        h, m = map(int, tz.split(':'))
42
42
        delta = timedelta(minutes=mult*(60*h + m))
43
 
        tz = type('CustomTZ', (tzinfo,), {'utcoffset':lambda self, dt:delta, 
 
43
        tz = type('CustomTZ', (tzinfo,), {'utcoffset':lambda self, dt:delta,
44
44
                                          'dst':lambda self,dt:timedelta(0)})()
45
45
 
46
 
    val = datetime(year, month, day, hours, minutes, seconds, microseconds, 
 
46
    val = datetime(year, month, day, hours, minutes, seconds, microseconds,
47
47
                   tzinfo=tz)
48
48
    if tz is not None:
49
49
        val = datetime(*(val.utctimetuple()[:6]))
61
61
    def __init__(self, sep=','):
62
62
        self.sep = sep
63
63
        self.ans = ''
64
 
        
 
64
 
65
65
    def step(self, value):
66
66
        if value is not None:
67
67
            self.ans += value + self.sep
68
 
    
 
68
 
69
69
    def finalize(self):
70
70
        if not self.ans:
71
71
            return None
73
73
            return self.ans[:-len(self.sep)]
74
74
        return self.ans
75
75
 
 
76
class SortedConcatenate(object):
 
77
    '''String concatenation aggregator for sqlite, sorted by supplied index'''
 
78
    def __init__(self, sep=','):
 
79
        self.sep = sep
 
80
        self.ans = {}
 
81
 
 
82
    def step(self, ndx, value):
 
83
        if value is not None:
 
84
            self.ans[ndx] = value
 
85
 
 
86
    def finalize(self):
 
87
        if len(self.ans) == 0:
 
88
            return None
 
89
        return self.sep.join(map(self.ans.get, sorted(self.ans.keys())))
 
90
 
76
91
class Connection(sqlite.Connection):
77
 
    
 
92
 
78
93
    def get(self, *args, **kw):
79
94
        ans = self.execute(*args)
80
95
        if not kw.get('all', True):
83
98
                ans = [None]
84
99
            return ans[0]
85
100
        return ans.fetchall()
86
 
 
 
101
 
87
102
 
88
103
class DBThread(Thread):
89
 
    
 
104
 
90
105
    CLOSE = '-------close---------'
91
 
    
 
106
 
92
107
    def __init__(self, path, row_factory):
93
108
        Thread.__init__(self)
94
109
        self.setDaemon(True)
98
113
        self.requests = Queue(1)
99
114
        self.results  = Queue(1)
100
115
        self.conn = None
101
 
        
 
116
 
102
117
    def connect(self):
103
 
        self.conn = sqlite.connect(self.path, factory=Connection, 
 
118
        self.conn = sqlite.connect(self.path, factory=Connection,
104
119
                                   detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
105
120
        self.conn.row_factory = sqlite.Row if self.row_factory else  lambda cursor, row : list(row)
106
121
        self.conn.create_aggregate('concat', 1, Concatenate)
 
122
        self.conn.create_aggregate('sortconcat', 2, SortedConcatenate)
107
123
        self.conn.create_function('title_sort', 1, title_sort)
108
 
    
 
124
 
109
125
    def run(self):
110
126
        try:
111
127
            self.connect()
124
140
            self.unhandled_error = (err, traceback.format_exc())
125
141
 
126
142
class DatabaseException(Exception):
127
 
    
 
143
 
128
144
    def __init__(self, err, tb):
129
145
        tb = '\n\t'.join(('\tRemote'+tb).splitlines())
130
146
        msg = unicode(err) +'\n' + tb
146
162
                raise DatabaseException(*res)
147
163
            return res
148
164
    return run
149
 
            
 
165
 
150
166
 
151
167
class ConnectionProxy(object):
152
 
    
 
168
 
153
169
    def __init__(self, proxy):
154
170
        self.proxy = proxy
155
 
    
 
171
 
156
172
    def close(self):
157
173
        if self.proxy.unhandled_error is None:
158
174
            self.proxy.requests.put((self.proxy.CLOSE, [], {}))
159
 
    
 
175
 
160
176
    @proxy
161
177
    def get(self, query, all=True): pass
162
 
    
163
 
    @proxy        
 
178
 
 
179
    @proxy
164
180
    def commit(self): pass
165
 
    
 
181
 
166
182
    @proxy
167
183
    def execute(self): pass
168
 
    
 
184
 
169
185
    @proxy
170
186
    def executemany(self): pass
171
 
    
 
187
 
172
188
    @proxy
173
189
    def executescript(self): pass
174
 
    
 
190
 
175
191
    @proxy
176
192
    def create_aggregate(self): pass
177
 
    
 
193
 
178
194
    @proxy
179
195
    def create_function(self): pass
180
 
    
 
196
 
181
197
    @proxy
182
198
    def cursor(self): pass
183
 
    
 
199
 
184
200
def connect(dbpath, row_factory=None):
185
201
    conn = ConnectionProxy(DBThread(dbpath, row_factory))
186
202
    conn.proxy.start()
188
204
        time.sleep(0.01)
189
205
    if conn.proxy.unhandled_error[0] is not None:
190
206
        raise DatabaseException(*conn.proxy.unhandled_error)
191
 
    return conn
 
 
b'\\ No newline at end of file'
 
207
    return conn