def __init__(self, libraries, opts, testing=False, notify_changes=None): ctx = Context(libraries, opts, testing=testing, notify_changes=notify_changes) self.auth_controller = None if opts.auth: has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None prefer_basic_auth = { 'auto': has_ssl, 'basic': True }.get(opts.auth_mode, False) self.auth_controller = AuthController( user_credentials=ctx.user_manager, prefer_basic_auth=prefer_basic_auth, ban_time_in_minutes=opts.ban_for, ban_after=opts.ban_after) self.router = Router(ctx=ctx, url_prefix=opts.url_prefix, auth_controller=self.auth_controller) for module in SRV_MODULES: module = import_module('calibre.srv.' + module) self.router.load_routes(itervalues(vars(module))) self.router.finalize() self.router.ctx.url_for = self.router.url_for self.dispatch = self.router.dispatch
def test_route_finding(self): 'Test route finding' from calibre.srv.routes import Router, endpoint, HTTPNotFound router = Router() def find(path): path = filter(None, path.split('/')) ep, args = router.find_route(path) args = list(args) return ep, args @endpoint('/') def root(ctx, data): pass @endpoint('/defval/{a=1}') def defval(ctx, data, a): pass @endpoint('/varpath/{a}/{b}') def varpath(ctx, data, a, b): pass @endpoint('/soak/{+rest}') def soak(ctx, dest, rest): pass @endpoint('/soak_opt/{+rest="xxx"}') def soak_opt(ctx, dest, rest): pass for x in locals().itervalues(): if getattr(x, 'is_endpoint', False): router.add(x) router.finalize() ep, args = find('/') self.ae(ep, root), self.assertFalse(args) ep, args = find('/defval') self.ae(ep, defval), self.ae(args, [1]) ep, args = find('/defval/2') self.ae(ep, defval), self.ae(args, [2]) self.assertRaises(HTTPNotFound, find, '/defval/a') # a must be an integer self.assertRaises(HTTPNotFound, find, '/varpath') self.assertRaises(HTTPNotFound, find, '/varpath/x') self.assertRaises(HTTPNotFound, find, '/varpath/x/y/z') self.assertRaises(HTTPNotFound, find, '/soak') ep, args = find('/varpath/x/y') self.ae(ep, varpath), self.ae(args, ['x', 'y']) ep, args = find('/soak/x') self.ae(ep, soak), self.ae(args, ['x']) self.ae(router.routes['/soak'].soak_up_extra, 'rest') ep, args = find('/soak/x/y/z') self.ae(ep, soak), self.ae(args, ['x/y/z']) ep, args = find('/soak_opt') self.ae(ep, soak_opt), self.ae(args, ['xxx']) ep, args = find('/soak_opt/a/b') self.ae(ep, soak_opt), self.ae(args, ['a/b'])
def __init__(self, libraries, opts, testing=False): self.router = Router(ctx=Context(libraries, opts, testing=testing), url_prefix=opts.url_prefix) for module in ('content', 'ajax', 'code'): module = import_module('calibre.srv.' + module) self.router.load_routes(vars(module).itervalues()) self.router.finalize() self.router.ctx.url_for = self.router.url_for self.dispatch = self.router.dispatch
def router(prefer_basic_auth=False): from calibre.srv.auth import AuthController return Router(globals().itervalues(), auth_controller=AuthController( { 'testuser': '******', '!@#$%^&*()-=_+': '!@#$%^&*()-=_+' }, prefer_basic_auth=prefer_basic_auth, realm=REALM, max_age_seconds=1))
def router(prefer_basic_auth=False, ban_for=0, ban_after=5): from calibre.srv.auth import AuthController return Router(itervalues(globals()), auth_controller=AuthController( { 'testuser': '******', '!@#$%^&*()-=_+': '!@#$%^&*()-=_+' }, ban_time_in_minutes=ban_for, ban_after=ban_after, prefer_basic_auth=prefer_basic_auth, realm=REALM, max_age_seconds=1))
def __init__(self, libraries, opts, testing=False): ctx = Context(libraries, opts, testing=testing) self.auth_controller = None if opts.auth: has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None prefer_basic_auth = { 'auto': has_ssl, 'basic': True }.get(opts.auth_mode, 'digest') self.auth_controller = AuthController( user_credentials=ctx.user_manager, prefer_basic_auth=prefer_basic_auth) self.router = Router(ctx=ctx, url_prefix=opts.url_prefix, auth_controller=self.auth_controller) for module in ('content', 'ajax', 'code'): module = import_module('calibre.srv.' + module) self.router.load_routes(vars(module).itervalues()) self.router.finalize() self.router.ctx.url_for = self.router.url_for self.dispatch = self.router.dispatch
def __init__(self, libraries, opts): self.router = Router(ctx=Context(libraries, opts), url_prefix=opts.url_prefix) self.router.ctx.url_for = self.router.url_for self.dispatch = self.router.dispatch