def __init__(self, configuration_location, **settings): BaseApplication.__init__(self, **settings) self._config_main_path = os.path.join(self._base_path, configuration_location) self._config_base_path = os.path.dirname(self._config_main_path) self._config = load_from_file(self._config_main_path) # Initialize the routing map self._routing_map = RoutingMap() # Default properties self._scope = settings['scope'] if 'scope' in settings else None self._port = 8000 # Register the default services. self._register_default_services() # Add the main configuration to the watch list. watch(self._config_main_path) # Configure the included files first. for inclusion in self._config.children('include'): source_location = inclusion.attribute('src') if source_location[0] != '/': source_location = os.path.join(self._config_base_path, source_location) pre_config = load_from_file(source_location) self._configure(pre_config, source_location) watch(source_location) self._logger.info('Included the configuration from %s' % source_location) self._configure(self._config) # Override the properties with the parameters. if 'port' in settings: self._port = settings['port'] self._logger.info('Changed the listening port: %s' % self._port) # Update the routing map AppServices.get('routing_map').update(self._routing_map) # Normal procedure self._update_routes(self._routing_map.export()) self.listen(self._port) self._activate()
def __init__(self, configuration_location, **settings): BaseApplication.__init__(self, **settings) self._service_assembler = ImaginationAssembler(ImaginationTransformer(AppServices)) self._config_main_path = os.path.join(self._base_path, configuration_location) self._config_base_path = os.path.dirname(self._config_main_path) self._config = load_from_file(self._config_main_path) # Initialize the routing map self._routing_map = RoutingMap() # Default properties self._scope = settings['scope'] if 'scope' in settings else None self._port = 8000 # Register the default services. self._register_default_services() # Add the main configuration to the watch list. watch(self._config_main_path) # Configure with the configuration files self._service_assembler.activate_passive_loading() for inclusion in self._config.children('include'): self._load_inclusion(inclusion) self._configure(self._config) self._prepare_db_connections() self._prepare_session_manager() self._service_assembler.deactivate_passive_loading() # Override the properties with the parameters. if 'port' in settings: self._port = settings['port'] self._logger.info('Changed the listening port: %s' % self._port) # Update the routing map AppServices.get('routing_map').update(self._routing_map) # Normal procedure self._update_routes(self._routing_map.export()) self.listen(self._port) self._activate()
def main(name, args): application = Application('config/dev.xml') if not args: print('USAGE: python {} <base_path>[ <port:8000>]'.format(name)) sys.exit(255) base_path = args[0] services.get('internal.finder').set_base_path(base_path) if len(args) > 1: port = args[1] application.listen(port) application.start()
def component(self, name, fork_component=False): """ Get the (re-usable) component from the initialized Imagination component locator service. :param `name`: the name of the registered re-usable component. :param `fork_component`: the flag to fork the component :return: module, package registered or ``None`` """ if not services.has(name): return None return services.fork(name)\ if fork_component\ else services.get(name)
def _prepare_db_connections(self): db_config = self._settings['db'] manager_config = db_config['managers'] em_factory = AppServices.get('db') for alias in manager_config: url = manager_config[alias]['url'] em_factory.set(alias, url) def callback(em_factory, db_alias): return em_factory.get(db_alias) callback_proxy = CallbackProxy(callback, em_factory, alias) AppServices.set('db.{}'.format(alias), callback_proxy)
def auto_load(): dataset = [(Role, { 'admin': { 'name': 'Administrator', 'alias': 'admin' }, 'user': { 'name': 'User', 'alias': 'user' } }), (Provider, { 'dev': { 'name': 'Developer Mode', 'alias': 'dev' }, 'google': { 'name': 'Google', 'alias': 'google' } })] manager = services.get('entity_manager') session = manager.open_session(supervised=False) for entity_class, fixtures in dataset: repository = session.collection(entity_class) for alias in fixtures: criteria = fixtures[alias] entity = repository.filter_one(criteria) if entity: continue entity = repository.new(**criteria) repository.post(entity)
def auto_load_mongodb(): dataset = { "tori.security.collection.provider": { "google": {"_id": 1, "name": "Google"}, "github": {"_id": 2, "name": "GitHub"}, } } for service_id in dataset: repository = services.get(service_id) """ :type repository: tori.db.odm.collection.Collection """ for alias in dataset[service_id]: criteria = dataset[service_id][alias] entity = repository.filter_one(**criteria) if entity: continue entity = repository.new_document(**criteria) repository.post(entity)
def auto_load(): dataset = [ ( Role, { 'admin': { 'name': 'Administrator', 'alias': 'admin' }, 'user': { 'name': 'User', 'alias': 'user' } } ), ( Provider, { 'dev': { 'name': 'Developer Mode', 'alias': 'dev' }, 'google': { 'name': 'Google', 'alias': 'google' } } ) ] manager = services.get('entity_manager') session = manager.open_session(supervised=False) for entity_class, fixtures in dataset: repository = session.collection(entity_class) for alias in fixtures: criteria = fixtures[alias] entity = repository.filter_one(criteria) if entity: continue entity = repository.new(**criteria) repository.post(entity) # endfor # endfor
''' Server bootstrap ''' from tori.application import Application from tori.centre import services import bootstrap from pints.foundation.initializer import Initializer application = Application('config/server.xml') if not bootstrap.is_production: initializer = Initializer(services.get('db')) initializer.load('%s/config/initial_directory.xml' % bootstrap.app_path) application.start()