def test_it(self): from karlserve.instance import get_instances settings = {} instances = get_instances(settings) self.assertEqual(settings['instances'], instances) instances.iam = 'thisone' instances = get_instances(settings) self.assertEqual(instances.iam, 'thisone')
def site_dispatch(request): instances = get_instances(request.registry.settings) path = list(request.matchdict.get('subpath')) host = request.host # Copy request, getting rid of bfg keys from the environ environ = request.environ.copy() for key in list(environ.keys()): if key.startswith('bfg.'): del environ[key] request = request.__class__(environ) # nginx likes to set script name to '/' with screws up everybody # trying to write urls and causes them to add an extra slash if len(request.script_name) == 1: request.script_name = '' # See if we're in a virtual hosting environment name = instances.get_virtual_host(host) if not name and path: # We are not in a virtual hosting environment, so the first element of # the path_info is the name of the instance. name = path.pop(0) # Get the Karl instance to dispatch to instance = instances.get(name) # If we found the instance, rewrite paths for subrequest if instance is not None: script_name = '/'.join((request.script_name, name)) path_info = '/' + '/'.join(path) request.script_name = script_name request.path_info = path_info else: # Get the Karl instance to dispatch to instance = instances.get(name) # If we still don't have an instance, see if one is defined as the root # instance. if instance is None: name = instances.root_instance if name is not None: instance = instances.get(name) if instance is None: raise NotFound # Dispatch set_current_instance(name) return request.get_response(instance.pipeline())
def main(argv=sys.argv, out=None): logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s: %(message)s') # ZEO is a bit too chatty, if you ask me. zeo_logger = logging.getLogger('ZEO') zeo_logger.setLevel(logging.WARN) if out is None: out = codecs.getwriter('UTF-8')(sys.stdout) parser = argparse.ArgumentParser() parser.add_argument('-C', '--config', metavar='FILE', default=None, help='Path to configuration ini file.') parser.add_argument('--pdb', action='store_true', default=False, help='Drop into the debugger if there is an uncaught ' 'exception.') subparsers = parser.add_subparsers( title='command', help='Available commands.') eps = [ep for ep in pkg_resources.iter_entry_points('sixfeetup.karlutils.scripts')] eps.sort(key=lambda ep: ep.name) ep_names = set() for ep in eps: if ep.name in ep_names: raise RuntimeError('script defined more than once: %s' % ep.name) try: script = ep.load() ep_names.add(ep.name) script(ep.name, subparsers, **helpers) except ImportError: pass args = parser.parse_args(argv[1:]) if args.config is None: args.config = get_default_config() app = loadapp('config:%s' % args.config, 'karlserve') if hasattr(args, 'instance'): if not args.instance: args.instances = sorted(get_instances( app.registry.settings).get_names()) else: args.instances = sorted(args.instance) del args.instance args.app = app args.get_instance = instance_factory(args, app) args.get_root = instance_root_factory(args, app) args.get_setting = settings_factory(args, app) args.is_normal_mode = is_normal_mode(args) args.out = out try: func = log_errors(args.func) if getattr(args, 'daemon', False): func = daemon(func, args) if getattr(args, 'only_one', False): func = only_one(func, args) if args.pdb: func = debug(func) func(args) finally: instances = app.registry.settings.get('instances') if instances is not None: instances.close()
def get_instance(app, name): instances = get_instances(app.registry.settings) return instances.get(name)