def make_app( config_path=USER_CONFIG_PATH, cli_username=None, password=None ): copy_config_to_user_dir_if_not_present() urllib3.disable_warnings() # ssl warning about certs conf = ConfigParser() conf.read(config_path) conf = dict(conf.items('yaws')) usernames = (cli_username, conf.get('username'), getpass.getuser()) conf.update( { 'username': next( username for username in usernames if username is not None ), 'password': password, 'config_path': config_path, } ) c = Container(conf=conf) register(c) return c
def run_app(config_path='yawsm/conf/develop.cfg', logging_config_path='yawsm/conf/logging/develop.cfg'): logging.config.fileConfig(logging_config_path) # txaio.start_logging(level='debug') c = Container(dict(config_path=config_path, )) log = logging.getLogger(__name__) register_all(c) # txaio.use_asyncio() # txaio.config.loop = c('loop') try_to_connect_to_db_and_create_admin_if_not_present( c('conf')['admin']['default_username'], log) log.info('changing unfinished work status to UNKNOWN...') move_unfinished_works_to_unknown_status(c) asyncio.ensure_future(c('consumer').consume_all(), loop=c('loop')) log.info('task queue consumer started..') asyncio.ensure_future(make_http_app(c), loop=c('loop')) log.info('http started...') asyncio.ensure_future(make_ws_app(c), loop=c('loop')) log.info('websocket started..') for signame in ('SIGINT', 'SIGTERM'): c('loop').add_signal_handler(getattr(signal, signame), functools.partial(exit, signame, c)) c('loop').run_forever()
def __get__(self, instance, owner): obj_id = id(instance) if obj_id not in self._vals: self.__set__(instance, Container()) real_obj = self._vals[obj_id] return real_obj if isinstance(real_obj, Container) else real_obj()
def test_uses_alternative_name(self): c = Container() def foobar(container): return 'foobar' c.add_provider(foobar, False, 'alternative') self.assertEqual(c.provide('alternative'), 'foobar')
def test_returns_return_value_provider(self): c = Container() def foo(container): return 'bar' c.add_provider(foo, False) self.assertEqual(c.provide('foo'), 'bar')
def test_registers_service(self): c = Container() c.add_provider = MagicMock() def foo(container): pass c.add_service(foo) c.add_provider.assert_called_with(foo, True, None) c.add_service(foo, 'alternative') c.add_provider.assert_called_with(foo, True, 'alternative')
def make_app(config_path='conf/develop.cfg', username='******', password='******'): logging.basicConfig(level=logging.INFO) c = Container( dict(config_path=config_path, username=username, password=password)) register(c) check_if_credentials_are_ok(c) return WorkerApp(url=c('conf').get('broker', 'url'), reactor=c('reactor'), factory=c('factory'), controller=c('worker_controller'), ssl_context=c('ssl_context'))
def test_registers_factory(self): c = Container() c.add_provider = MagicMock() def foo(container): pass c.add_factory(foo) c.add_provider.assert_called_with(foo, False, None) c.add_factory(foo, 'alternative') c.add_provider.assert_called_with(foo, False, 'alternative')
def test_caches_return_value_provider(self): c = Container() def foobar(container): return {} c.add_provider(foobar, True) dict1 = c.provide('foobar') dict2 = c.provide('foobar') assert isinstance(dict1, dict) assert isinstance(dict2, dict) assert dict1 is dict2
def test_registers_factory_with_decorator(self): c = Container() c.add_factory = MagicMock() @factory(c) def foo(container): pass c.add_factory.assert_called_with(foo, None) @factory(c, 'alternative') def bar(container): pass c.add_factory.assert_called_with(bar, 'alternative')
def test_registers_provider_with_decorator(self): c = Container() c.add_provider = MagicMock() @provider(c, False) def foo(container): pass c.add_provider.assert_called_with(foo, False, None) @provider(c, True, 'alternative') def bar(container): pass c.add_provider.assert_called_with(bar, True, 'alternative')
def test_registers_service_with_decorator(self): c = Container() c.add_service = MagicMock() @service(c) def foo(container): pass c.add_service.assert_called_with(foo, None) @service(c, 'alternative') def bar(container): pass c.add_service.assert_called_with(bar, 'alternative')
def test_returns_whether_return_value_provider_is_cached(self): c = Container() def foobar(container): return {} c.add_provider(foobar, True) self.assertFalse(c.is_cached('foobar')) c.provide('foobar') self.assertTrue(c.is_cached('foobar')) def foobaz(container): return {} c.add_provider(foobaz, False) self.assertFalse(c.is_cached('foobaz')) c.provide('foobaz') self.assertFalse(c.is_cached('foobaz'))
def container(conf_path, event_loop): container = Container(dict( config_path=conf_path )) container.add_service(conf) container.add_service(lambda c: event_loop, 'loop') register_mock_clients(container) register_db(container) register_domain(container) register_ws_services(container) register_usecases(container) container.add_service(ssh) register_auth(container) register_ws(container) connect_to_db_and_create_tables() return container
def test_raises_key_error_when_name_not_found(self): c = Container() self.assertRaises(KeyError, lambda: c.provide('foo'))
def test_returns_value(self): c = Container({'value': 'foobar'}) self.assertEqual(c.provide('value'), 'foobar')
def test_forwards_to_provide(self): c = Container() c.provide = MagicMock() c.provide('foo') c.provide.assert_called_with('foo')