class Server(object): def __init__(self, libraries, opts): log = access_log = None log_size = opts.max_log_size * 1024 * 1024 if opts.log: log = RotatingLog(opts.log, max_size=log_size) if opts.access_log: access_log = RotatingLog(opts.access_log, max_size=log_size) self.handler = Handler(libraries, opts) plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, log=log, access_log=access_log, plugins=plugins) self.handler.set_log(self.loop.log) self.handler.set_jobs_manager(self.loop.jobs_manager) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop if is_running_from_develop: from calibre.utils.rapydscript import compile_srv compile_srv()
def __init__(self, libraries, opts): log = access_log = None log_size = opts.max_log_size * 1024 * 1024 if opts.log: log = RotatingLog(opts.log, max_size=log_size) if opts.access_log: access_log = RotatingLog(opts.access_log, max_size=log_size) self.handler = Handler(libraries, opts) if opts.custom_list_template: with lopen(os.path.expanduser(opts.custom_list_template), 'rb') as f: self.handler.router.ctx.custom_list_template = json.load(f) if opts.search_the_net_urls: with lopen(os.path.expanduser(opts.search_the_net_urls), 'rb') as f: self.handler.router.ctx.search_the_net_urls = json.load(f) plugins = [] if opts.use_bonjour: plugins.append(BonJour(wait_for_stop=max(0, opts.shutdown_timeout - 0.2))) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, log=log, access_log=access_log, plugins=plugins) self.handler.set_log(self.loop.log) self.handler.set_jobs_manager(self.loop.jobs_manager) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop if is_running_from_develop: from calibre.utils.rapydscript import compile_srv compile_srv()
class LibraryServer(TestServer): def __init__(self, library_path, libraries=(), plugins=(), **kwargs): Thread.__init__(self, name='ServerMain') from calibre.srv.opts import Options from calibre.srv.loop import ServerLoop from calibre.srv.handler import Handler from calibre.srv.http_response import create_http_handler self.setup_defaults(kwargs) opts = Options(**kwargs) self.libraries = libraries or (library_path, ) self.handler = Handler(self.libraries, opts, testing=True) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, plugins=plugins, log=ServerLog(level=ServerLog.DEBUG), ) self.log = self.loop.log self.handler.set_log(self.log) def __exit__(self, *args): try: self.loop.stop() except Exception as e: self.log.error('Failed to stop server with error:', e) self.handler.close() self.join(self.loop.opts.shutdown_timeout) self.loop.close_control_connection()
class Server(object): def __init__(self, libraries, opts): log = access_log = None log_size = opts.max_log_size * 1024 * 1024 if opts.log: log = RotatingLog(opts.log, max_size=log_size) if opts.access_log: access_log = RotatingLog(opts.access_log, max_size=log_size) self.handler = Handler(libraries, opts) if opts.custom_list_template: with lopen(opts.custom_list_template, 'rb') as f: self.handler.router.ctx.custom_list_template = json.load(f) plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, log=log, access_log=access_log, plugins=plugins) self.handler.set_log(self.loop.log) self.handler.set_jobs_manager(self.loop.jobs_manager) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop if is_running_from_develop: from calibre.utils.rapydscript import compile_srv compile_srv()
class LibraryServer(TestServer): def __init__(self, library_path, libraries=(), plugins=(), specialize=lambda x:None, **kwargs): Thread.__init__(self, name='ServerMain') from calibre.srv.opts import Options from calibre.srv.loop import ServerLoop from calibre.srv.handler import Handler from calibre.srv.http_response import create_http_handler self.setup_defaults(kwargs) opts = Options(**kwargs) self.libraries = libraries or (library_path,) self.handler = Handler(self.libraries, opts, testing=True) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, plugins=plugins, log=ServerLog(level=ServerLog.WARN), ) self.handler.set_log(self.loop.log) specialize(self) def __exit__(self, *args): self.loop.stop() self.handler.close() self.join(self.loop.opts.shutdown_timeout)
class LibraryServer(TestServer): def __init__(self, library_path, libraries=(), plugins=(), specialize=lambda x: None, **kwargs): Thread.__init__(self, name='ServerMain') from calibre.srv.opts import Options from calibre.srv.loop import ServerLoop from calibre.srv.handler import Handler from calibre.srv.http_response import create_http_handler self.setup_defaults(kwargs) opts = Options(**kwargs) self.libraries = libraries or (library_path, ) self.handler = Handler(self.libraries, opts, testing=True) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, plugins=plugins, log=ServerLog(level=ServerLog.WARN), ) self.handler.set_log(self.loop.log) specialize(self) def __exit__(self, *args): self.loop.stop() self.handler.close() self.join(self.loop.opts.shutdown_timeout)
class Server(object): def __init__(self, libraries, opts): log = access_log = None log_size = opts.max_log_size * 1024 * 1024 if opts.log: log = RotatingLog(opts.log, max_size=log_size) if opts.access_log: access_log = RotatingLog(opts.access_log, max_size=log_size) self.handler = Handler(libraries, opts) plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.loop = ServerLoop(create_http_handler(self.handler.dispatch), opts=opts, log=log, access_log=access_log, plugins=plugins) self.handler.set_log(self.loop.log) self.handler.set_jobs_manager(self.loop.jobs_manager) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop _df = os.environ.get('CALIBRE_DEVELOP_FROM', None) if _df and os.path.exists(_df): from calibre.utils.rapydscript import compile_srv compile_srv()
def __init__(self, libraries, opts): log = None if opts.log: log = RotatingLog(opts.log, max_size=opts.max_log_size) self.handler = Handler(libraries, opts) plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.loop = ServerLoop(create_http_handler(self.handler.dispatch), opts=opts, log=log, plugins=plugins) self.handler.set_log(self.loop.log) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop
class Server(object): def __init__(self, libraries, opts): log = None if opts.log: log = RotatingLog(opts.log, max_size=opts.max_log_size) self.handler = Handler(libraries, opts) plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.loop = ServerLoop(create_http_handler(self.handler.dispatch), opts=opts, log=log, plugins=plugins) self.handler.set_log(self.loop.log) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop
def test_library_restrictions(self): # {{{ from calibre.srv.opts import Options from calibre.srv.handler import Handler from calibre.db.legacy import create_backend opts = Options(userdb=':memory:') Data = namedtuple('Data', 'username') with TemporaryDirectory() as base: l1, l2, l3 = map(lambda x: os.path.join(base, 'l' + x), '123') for l in (l1, l2, l3): create_backend(l).close() ctx = Handler((l1, l2, l3), opts).router.ctx um = ctx.user_manager def get_library(username=None, library_id=None): ans = ctx.get_library(Data(username), library_id=library_id) return os.path.basename(ans.backend.library_path) def library_info(username=None): lmap, defaultlib = ctx.library_info(Data(username)) lmap = {k:os.path.basename(v) for k, v in iteritems(lmap)} return lmap, defaultlib self.assertEqual(get_library(), 'l1') self.assertEqual(library_info()[0], {'l%d'%i:'l%d'%i for i in range(1, 4)}) self.assertEqual(library_info()[1], 'l1') self.assertRaises(HTTPForbidden, get_library, 'xxx') um.add_user('a', 'a') self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4)}) um.update_user_restrictions('a', {'blocked_library_names': ['L2']}) self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4) if i != 2}) um.update_user_restrictions('a', {'allowed_library_names': ['l3']}) self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4) if i == 3}) self.assertEqual(library_info('a')[1], 'l3') self.assertRaises(HTTPForbidden, get_library, 'a', 'l1') self.assertRaises(HTTPForbidden, get_library, 'xxx')
def __init__(self, library_broker, notify_changes): opts = server_config() lp, lap = log_paths() try: os.makedirs(cache_dir()) except EnvironmentError as err: if err.errno != errno.EEXIST: raise log_size = opts.max_log_size * 1024 * 1024 log = RotatingLog(lp, max_size=log_size) access_log = RotatingLog(lap, max_size=log_size) self.handler = Handler(library_broker, opts, notify_changes=notify_changes) plugins = self.plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log)
class Server(object): def __init__(self, libraries, opts): log = None if opts.log: log = RotatingLog(opts.log, max_size=opts.max_log_size) self.handler = Handler(libraries, opts) plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.loop = ServerLoop(create_http_handler(self.handler.dispatch), opts=opts, log=log, plugins=plugins) self.handler.set_log(self.loop.log) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop _df = os.environ.get("CALIBRE_DEVELOP_FROM", None) if _df and os.path.exists(_df): from calibre.utils.rapydscript import compile_srv compile_srv()
def __init__(self, library_path, libraries=(), plugins=(), **kwargs): Thread.__init__(self, name='ServerMain') from calibre.srv.opts import Options from calibre.srv.loop import ServerLoop from calibre.srv.handler import Handler from calibre.srv.http_response import create_http_handler self.setup_defaults(kwargs) opts = Options(**kwargs) self.libraries = libraries or (library_path, ) self.handler = Handler(self.libraries, opts, testing=True) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, plugins=plugins, log=ServerLog(level=ServerLog.DEBUG), ) self.log = self.loop.log self.handler.set_log(self.log)
def __init__(self, library_broker, notify_changes): opts = server_config() lp, lap = log_paths() try: os.makedirs(cache_dir()) except OSError as err: if err.errno != errno.EEXIST: raise log_size = opts.max_log_size * 1024 * 1024 log = RotatingLog(lp, max_size=log_size) access_log = RotatingLog(lap, max_size=log_size) self.handler = Handler(library_broker, opts, notify_changes=notify_changes) plugins = self.plugins = [] if opts.use_bonjour: plugins.append(BonJour(wait_for_stop=max(0, opts.shutdown_timeout - 0.2))) self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log) self.handler.router.ctx.custom_list_template = custom_list_template() self.handler.router.ctx.search_the_net_urls = search_the_net_urls()
class LibraryServer(TestServer): def __init__(self, library_path, libraries=(), plugins=(), specialize=lambda x:None, **kwargs): Thread.__init__(self, name='ServerMain') from calibre.srv.opts import Options from calibre.srv.loop import ServerLoop from calibre.srv.handler import Handler from calibre.srv.http_response import create_http_handler kwargs['shutdown_timeout'] = kwargs.get('shutdown_timeout', 0.1) kwargs['listen_on'] = kwargs.get('listen_on', 'localhost') kwargs['port'] = kwargs.get('port', 0) opts = Options(**kwargs) self.libraries = libraries or (library_path,) self.handler = Handler(self.libraries, opts, testing=True) self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=opts, plugins=plugins, log=ServerLog(level=ServerLog.WARN), ) self.handler.set_log(self.loop.log) specialize(self)
def __init__(self, library_broker, notify_changes): opts = server_config() lp, lap = log_paths() try: os.makedirs(cache_dir()) except EnvironmentError as err: if err.errno != errno.EEXIST: raise log_size = opts.max_log_size * 1024 * 1024 log = RotatingLog(lp, max_size=log_size) access_log = RotatingLog(lap, max_size=log_size) self.handler = Handler(library_broker, opts, notify_changes=notify_changes) plugins = self.plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log) _df = os.environ.get('CALIBRE_DEVELOP_FROM', None) if _df and os.path.exists(_df): from calibre.utils.rapydscript import compile_srv compile_srv()
def __init__(self, library_broker, notify_changes): opts = server_config() lp, lap = log_paths() try: os.makedirs(cache_dir()) except EnvironmentError as err: if err.errno != errno.EEXIST: raise log_size = opts.max_log_size * 1024 * 1024 log = RotatingLog(lp, max_size=log_size) access_log = RotatingLog(lap, max_size=log_size) self.handler = Handler(library_broker, opts, notify_changes=notify_changes) plugins = self.plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log) self.handler.router.ctx.custom_list_template = custom_list_template()
class Server: loop = current_thread = exception = None state_callback = start_failure_callback = None def __init__(self, library_broker, notify_changes): opts = server_config() lp, lap = log_paths() try: os.makedirs(cache_dir()) except OSError as err: if err.errno != errno.EEXIST: raise log_size = opts.max_log_size * 1024 * 1024 log = RotatingLog(lp, max_size=log_size) access_log = RotatingLog(lap, max_size=log_size) self.handler = Handler(library_broker, opts, notify_changes=notify_changes) plugins = self.plugins = [] if opts.use_bonjour: plugins.append(BonJour(wait_for_stop=max(0, opts.shutdown_timeout - 0.2))) self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log) self.handler.router.ctx.custom_list_template = custom_list_template() self.handler.router.ctx.search_the_net_urls = search_the_net_urls() @property def ctx(self): return self.handler.router.ctx @property def user_manager(self): return self.handler.router.ctx.user_manager def start(self): if self.current_thread is None: try: self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=self.opts, log=self.log, access_log=self.access_log, plugins=self.plugins ) self.loop.initialize_socket() except Exception as e: self.loop = None self.exception = e if self.start_failure_callback is not None: try: self.start_failure_callback(as_unicode(e)) except Exception: pass return self.handler.set_jobs_manager(self.loop.jobs_manager) self.current_thread = t = Thread( name='EmbeddedServer', target=self.serve_forever ) t.daemon = True t.start() def serve_forever(self): self.exception = None from calibre.srv.content import reset_caches try: if is_running_from_develop: from calibre.utils.rapydscript import compile_srv compile_srv() except BaseException as e: self.exception = e if self.start_failure_callback is not None: try: self.start_failure_callback(as_unicode(e)) except Exception: pass return if self.state_callback is not None: try: self.state_callback(True) except Exception: pass reset_caches() # we reset the cache as the server tdir has changed try: self.loop.serve_forever() except BaseException as e: self.exception = e if self.state_callback is not None: try: self.state_callback(False) except Exception: pass def stop(self): if self.loop is not None: self.loop.stop() self.loop = None def exit(self): if self.current_thread is not None: self.stop() self.current_thread.join() self.current_thread = None @property def is_running(self): return self.current_thread is not None and self.current_thread.is_alive()
class Server(object): loop = current_thread = exception = None state_callback = start_failure_callback = None def __init__(self, library_broker, notify_changes): opts = server_config() lp, lap = log_paths() try: os.makedirs(cache_dir()) except EnvironmentError as err: if err.errno != errno.EEXIST: raise log_size = opts.max_log_size * 1024 * 1024 log = RotatingLog(lp, max_size=log_size) access_log = RotatingLog(lap, max_size=log_size) self.handler = Handler(library_broker, opts, notify_changes=notify_changes) plugins = self.plugins = [] if opts.use_bonjour: plugins.append(BonJour()) self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log) self.handler.router.ctx.custom_list_template = custom_list_template() @property def ctx(self): return self.handler.router.ctx @property def user_manager(self): return self.handler.router.ctx.user_manager def start(self): if self.current_thread is None: try: self.loop = ServerLoop( create_http_handler(self.handler.dispatch), opts=self.opts, log=self.log, access_log=self.access_log, plugins=self.plugins ) self.loop.initialize_socket() except Exception as e: self.loop = None self.exception = e if self.start_failure_callback is not None: try: self.start_failure_callback(as_unicode(e)) except Exception: pass return self.handler.set_jobs_manager(self.loop.jobs_manager) self.current_thread = t = Thread( name='EmbeddedServer', target=self.serve_forever ) t.daemon = True t.start() def serve_forever(self): self.exception = None from calibre.srv.content import reset_caches try: if is_running_from_develop: from calibre.utils.rapydscript import compile_srv compile_srv() except BaseException as e: self.exception = e if self.start_failure_callback is not None: try: self.start_failure_callback(as_unicode(e)) except Exception: pass return if self.state_callback is not None: try: self.state_callback(True) except Exception: pass reset_caches() # we reset the cache as the server tdir has changed try: self.loop.serve_forever() except BaseException as e: self.exception = e if self.state_callback is not None: try: self.state_callback(False) except Exception: pass def stop(self): if self.loop is not None: self.loop.stop() self.loop = None def exit(self): if self.current_thread is not None: self.stop() self.current_thread.join() self.current_thread = None @property def is_running(self): return self.current_thread is not None and self.current_thread.is_alive()