Framework for cdb change notifications

This commit is contained in:
Kovid Goyal 2017-04-29 17:34:23 +05:30
parent 4df54f92f9
commit 530cb0d00a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 14 additions and 6 deletions

View File

@ -34,8 +34,9 @@ def cover(db, book_id):
def implementation( def implementation(
db, is_remote, fields, sort_by, ascending, search_text, limit db, notify_changes, fields, sort_by, ascending, search_text, limit
): ):
is_remote = notify_changes is not None
with db.safe_read_lock: with db.safe_read_lock:
fm = db.field_metadata fm = db.field_metadata
afields = set(FIELDS) | {'id'} afields = set(FIELDS) | {'id'}

View File

@ -45,10 +45,10 @@ def send_message(msg=''):
t.conn.close() t.conn.close()
def run_cmd(cmd, opts, args, db_ctx): def run_cmd(cmd, opts, args, dbctx):
m = module_for_cmd(cmd) m = module_for_cmd(cmd)
ret = m.main(opts, args, db_ctx) ret = m.main(opts, args, dbctx)
if not db_ctx.is_remote and not opts.dont_notify_gui and not getattr(m, 'readonly', False): if not dbctx.is_remote and not opts.dont_notify_gui and not getattr(m, 'readonly', False):
send_message() send_message()
return ret return ret
@ -171,6 +171,12 @@ class DBCtx(object):
self._db = LibraryDatabase(self.library_path).new_api self._db = LibraryDatabase(self.library_path).new_api
return self._db return self._db
def path(self, path):
if self.is_remote:
with lopen(path, 'rb') as f:
return f.read()
return path
def run(self, name, *args): def run(self, name, *args):
m = module_for_cmd(name) m = module_for_cmd(name)
if self.is_remote: if self.is_remote:

View File

@ -1,4 +1,4 @@
Rewrite calibredb to connect to running server Wire up cdb notify_changes for the embeded server
Prevent standalone and embedded servers from running simultaneously Prevent standalone and embedded servers from running simultaneously
Prevent more than a single instance of the standalone server from running Prevent more than a single instance of the standalone server from running

View File

@ -33,7 +33,7 @@ def cdb_run(ctx, rd, which):
raise HTTPBadRequest('args are not valid encoded data') raise HTTPBadRequest('args are not valid encoded data')
db = get_library_data(ctx, rd, strict_library_id=True)[0] db = get_library_data(ctx, rd, strict_library_id=True)[0]
try: try:
result = m.implementation(db, True, *args) result = m.implementation(db, ctx.notify_changes, *args)
except Exception as err: except Exception as err:
import traceback import traceback
return {'err': as_unicode(err), 'tb':traceback.format_stack()} return {'err': as_unicode(err), 'tb':traceback.format_stack()}

View File

@ -32,6 +32,7 @@ class Context(object):
self.user_manager = UserManager(opts.userdb) self.user_manager = UserManager(opts.userdb)
self.ignored_fields = frozenset(filter(None, (x.strip() for x in (opts.ignored_fields or '').split(',')))) self.ignored_fields = frozenset(filter(None, (x.strip() for x in (opts.ignored_fields or '').split(','))))
self.displayed_fields = frozenset(filter(None, (x.strip() for x in (opts.displayed_fields or '').split(',')))) self.displayed_fields = frozenset(filter(None, (x.strip() for x in (opts.displayed_fields or '').split(','))))
self.notify_changes = lambda *a: None
def start_job(self, name, module, func, args=(), kwargs=None, job_done_callback=None, job_data=None): def start_job(self, name, module, func, args=(), kwargs=None, job_done_callback=None, job_data=None):
return self.jobs_manager.start_job(name, module, func, args, kwargs, job_done_callback, job_data) return self.jobs_manager.start_job(name, module, func, args, kwargs, job_done_callback, job_data)