def test_get_class_from_config(self): """ Getting classes from configs.""" default_config = {"module": "firenado.session", "class": "SessionEngine"} custom_config = {"module": "firenado.session", "my_class": "SessionEngine"} result = get_class_from_config(default_config) result_custom = get_class_from_config(custom_config, index="my_class") self.assertTrue(result == SessionEngine) self.assertTrue(result_custom == SessionEngine)
def test_get_class_from_config(self): """ Getting classes from configs.""" default_config = {"module": "firenado.session", "class": "SessionEngine"} custom_config = {"module": "firenado.session", "my_class": "SessionEngine"} result = get_class_from_config(default_config) result_custom = get_class_from_config(custom_config, index="my_class") self.assertTrue(result == SessionEngine) self.assertTrue(result_custom == SessionEngine)
def test_session_type_file(self): """ Checks if test component was loaded correctly by the application __init__ method. """ application = TornadoApplication() session_handler_config = firenado.conf.session['handlers'][ firenado.conf.session['type']] session_handler_class = get_class_from_config(session_handler_config) self.assertEqual(application.session_engine.session_handler.__class__, session_handler_class)
def __init__(self, session_aware_instance): self.session_aware_instance = None self.session_handler = None self.session_callback = None self.callback_time = None self.callback_hiccup = firenado.conf.session['callback_hiccup'] * 1000 # TODO: By the way session could be disabled. How do we # handle that? # TODO: check if session type exists. Maybe disable it if type is not # defined. We need to inform the error here if firenado.conf.session['enabled']: # Transforming session callback time to milliseconds. self.callback_time = firenado.conf.session['callback_time'] * 1000 logging.debug("Session periodic callback will be set with time of " "%sms." % self.callback_time) session_handler_class = get_class_from_config( firenado.conf.session['handlers'][ firenado.conf.session['type']] ) logging.debug("Setting session handler %s.%s." % (session_handler_class.__module__, session_handler_class.__name__) ) self.session_aware_instance = session_aware_instance self.session_handler = session_handler_class(self) self.session_callback = tornado.ioloop.PeriodicCallback( self.session_handler.purge_expired_sessions, self.callback_time ) self.session_handler.set_settings({}) self.session_handler.configure() # Starting session periodic callback self.session_callback.start() logging.debug("Session periodic callback started by the engine.") encoder_class = get_class_from_config( firenado.conf.session['encoders'][ firenado.conf.session['encoder'] ] ) self.session_encoder = encoder_class()
def test_session_type_file(self): """ Checks if test component was loaded correctly by the application __init__ method. """ application = TornadoApplication() session_handler_config = firenado.conf.session[ 'handlers'][firenado.conf.session['type']] session_handler_class = get_class_from_config(session_handler_config) app_session_handler_class = \ application.session_engine.session_handler.__class__ self.assertEqual(app_session_handler_class, session_handler_class)
def __init__(self, session_aware_instance): self.session_aware_instance = None self.session_handler = None # TODO: By the way session could be disabled. How do we # handle that? # TODO: check if session type exists. Maybe disable it if type is not # defined. We need to inform the error here if firenado.conf.session['enabled']: session_handler_class = get_class_from_config( firenado.conf.session['handlers'][ firenado.conf.session['type']]) self.session_aware_instance = session_aware_instance self.session_handler = session_handler_class(self) self.session_handler.set_settings({}) self.session_handler.configure() encoder_class = get_class_from_config( firenado.conf.session['encoders'][ firenado.conf.session['encoder']]) self.session_encoder = encoder_class()
def setUp(self): """ Application configuration file will be read and components will be loaded. """ chdir_app('file', 'session') self.application = TornadoApplication() self.session_handler_config = firenado.conf.session[ 'handlers'][firenado.conf.session['type']] self.session_handler_config = firenado.conf.session[ 'handlers'][firenado.conf.session['type']] self.session_handler_class = get_class_from_config( self.session_handler_config)
def setUp(self): """ Application configuration file will be read and components will be loaded. """ chdir_app('file', 'session') self.application = TornadoApplication() self.session_handler_config = firenado.conf.session['handlers'][ firenado.conf.session['type']] self.session_handler_config = firenado.conf.session['handlers'][ firenado.conf.session['type']] self.session_handler_class = get_class_from_config( self.session_handler_config)
def __init__(self, session_aware_instance): self.session_aware_instance = None self.session_handler = None self.session_callback = None # TODO: By the way session could be disabled. How do we # handle that? # TODO: check if session type exists. Maybe disable it if type is not # defined. We need to inform the error here if firenado.conf.session['enabled']: # Transforming session scan interval to milliseconds. scan_interval = firenado.conf.session['scan_interval'] * 1000 logging.debug("Session periodic call back will be set with scan " "interval of %sms." % scan_interval) session_handler_class = get_class_from_config( firenado.conf.session['handlers'][ firenado.conf.session['type']] ) logging.debug("Setting session handler %s.%s." % (session_handler_class.__module__, session_handler_class.__name__) ) self.session_aware_instance = session_aware_instance self.session_handler = session_handler_class(self) self.session_callback = tornado.ioloop.PeriodicCallback( self.session_handler.purge_expired_sessions, scan_interval ) self.session_handler.set_settings({}) self.session_handler.configure() # Starting session periodic callback self.session_callback.start() logging.debug("Session periodic callback started by the engine.") encoder_class = get_class_from_config( firenado.conf.session['encoders'][ firenado.conf.session['encoder'] ] ) self.session_encoder = encoder_class()
def __init__(self, session_aware_instance): self.session_aware_instance = None self.session_handler = None # TODO: By the way session could be disabled. How do we # handle that? # TODO: check if session type exists. Maybe disable it if type is not # defined. We need to inform the error here if firenado.conf.session['enabled']: session_handler_class = get_class_from_config( firenado.conf.session['handlers'][firenado.conf.session['type']] ) self.session_aware_instance = session_aware_instance self.session_handler = session_handler_class(self) self.session_handler.set_settings({}) self.session_handler.configure() encoder_class = get_class_from_config( firenado.conf.session['encoders'][ firenado.conf.session['encoder'] ] ) self.session_encoder = encoder_class()
def __generate_session_id(): """ Retrieves the session id generator function from the configuration. It is possible for a developer create a new session id generator function and and use it here. Returns: A probably unique session id. """ session_id_generator = get_class_from_config( firenado.conf.session['id_generators'][ firenado.conf.app['session']['id_generator']], "function") return session_id_generator()
def __generate_session_id(): """ Retrieves the session id generator function from the configuration. It is possible for a developer create a new session id generator function and and use it here. Returns: A probably unique session id. """ session_id_generator = get_class_from_config( firenado.conf.session['id_generators'][ firenado.conf.app['session']['id_generator'] ], "function" ) return session_id_generator()
def run(self, namespace): #TODO throw a custom error when type is not found from firenado.config import get_class_from_config parameters = {} if namespace.dir is not None: parameters['dir'] = namespace.dir if namespace.socket is None: if namespace.addresses is not None: parameters['addresses'] = namespace.addresses.split(",") if namespace.port is not None: parameters['port'] = namespace.port else: parameters['socket'] = namespace.socket app_type = firenado.conf.app['types'][firenado.conf.app['type']] launcher = get_class_from_config(app_type['launcher'])(**parameters) launcher.load() launcher.launch()
def test_session_type_file(self): """ Checks if test component was loaded correctly by the application __init__ method. """ chdir_app('file', 'session') application = TornadoApplication() session_handler_config = firenado.conf.session[ 'handlers'][firenado.conf.session['type']] session_handler_class = get_class_from_config(session_handler_config) app_session_handler_class = \ application.session_engine.session_handler.__class__ self.assertEquals(app_session_handler_class, session_handler_class) print(str(session_handler_class)) print(application.session_engine.session_handler.__class__) sess_handler_path = application.session_engine.session_handler.path sess_handler_path
def run(self, namespace): #TODO throw a custom error when type is not found from firenado.config import get_class_from_config parameters = {} if namespace.app is not None: parameters['app'] = namespace.app if namespace.dir is not None: parameters['dir'] = namespace.dir if namespace.path is not None: parameters['path'] = namespace.path if namespace.socket is None: if namespace.addresses is not None: parameters['addresses'] = namespace.addresses.split(",") if namespace.port is not None: parameters['port'] = namespace.port else: parameters['socket'] = namespace.socket app_type = firenado.conf.app['types'][firenado.conf.app['type']] launcher = get_class_from_config(app_type['launcher'])(**parameters) launcher.load() launcher.launch()
def __load_components(self): """ Loads all enabled components registered into the components conf. """ for key, value in iteritems(firenado.conf.components): if value['enabled']: component_class = get_class_from_config(value) self.components[key] = component_class(key, self) if self.components[key].get_config_file(): from firenado.util.file import file_has_extension filename = self.components[key].get_config_file() comp_config_file = None if file_has_extension(filename): if os.path.isfile( os.path.join(firenado.conf.APP_CONFIG_PATH, filename)): comp_config_file = os.path.join( firenado.conf.APP_CONFIG_PATH, filename) else: config_file_extensions = ['yml', 'yaml'] for extension in config_file_extensions: candidate_filename = os.path.join( firenado.conf.APP_CONFIG_PATH, '%s.%s' % (filename, extension)) if os.path.isfile(candidate_filename): comp_config_file = candidate_filename break if comp_config_file is not None: self.components[key].conf = load_yaml_config_file( comp_config_file) self.components[key].process_config() else: logger.warn('Failed to find the file for the ' 'component %s at %s. Component filename ' 'returned is %s.' % (key, firenado.conf.APP_CONFIG_PATH, self.components[key].get_config_file())) self.components[key].initialize()
def __load_components(self): """ Loads all enabled components registered into the components conf. """ for key, value in iteritems(firenado.conf.components): if value['enabled']: component_class = get_class_from_config(value) self.components[key] = component_class(key, self) if self.components[key].get_config_file(): from firenado.util.file import file_has_extension filename = self.components[key].get_config_file() comp_config_file = None if file_has_extension(filename): if os.path.isfile(os.path.join( firenado.conf.APP_CONFIG_PATH, filename)): comp_config_file = os.path.join( firenado.conf.APP_CONFIG_PATH, filename) else: config_file_extensions = ['yml', 'yaml'] for extension in config_file_extensions: candidate_filename = os.path.join( firenado.conf.APP_CONFIG_PATH, '%s.%s' % (filename, extension)) if os.path.isfile(candidate_filename): comp_config_file = candidate_filename break if comp_config_file is not None: self.components[key].conf = load_yaml_config_file( comp_config_file) self.components[key].process_config() else: logger.warn('Failed to find the file for the ' 'component %s at %s. Component filename ' 'returned is %s.' % ( key, firenado.conf.APP_CONFIG_PATH, self.components[key].get_config_file()) ) self.components[key].initialize()
def run(self, namespace): #TODO throw a custom error when type is not found from firenado.config import get_class_from_config app_type = firenado.conf.app['types'][firenado.conf.app['type']] launcher = get_class_from_config(app_type['launcher'])() launcher.launch()