Esempio n. 1
0
 def _generate_settings(self, files=None):
     files = files or []
     factory = Factory(self.module)
     settings, paths = factory.make_settings(
         settings=self.settings,
         additional_modules=files,
     )
     settings['paths'] = paths
     return settings, paths
Esempio n. 2
0
 def get_settings(self, module, settings={}, additional_modules=None):
     additional_modules = additional_modules or [
         ('local', False),
     ]
     self.factory = Factory('%s.application' % (module))
     settings, paths = self.factory.make_settings(
         settings=settings,
         additional_modules=additional_modules,)
     settings['paths'] = paths
     return settings, paths
Esempio n. 3
0
 def _generate_settings(self, files=None):
     files = files or []
     factory = Factory(self.module)
     settings, paths = factory.make_settings(
         settings=self.settings,
         paths=self.paths,
         additional_modules=files,
     )
     settings['paths'] = paths
     return settings, paths
Esempio n. 4
0
    def setUp(self):
        super(FactoryTest, self).setUp()
        self.factory = Factory('main_modulepath', 'settings_modulepath')

        self.patchers = {}
        self.mocks = {}

        self._init_patchers()
        self._start_patchers()
Esempio n. 5
0
 def get_settings(self, module, settings={}, additional_modules=None):
     additional_modules = additional_modules or [
         ('local', False),
     ]
     self.factory = Factory('%s.application' % (module))
     settings, paths = self.factory.make_settings(
         settings=settings,
         additional_modules=additional_modules,)
     settings['paths'] = paths
     return settings, paths
Esempio n. 6
0
class Application(object):

    """Prepering and configuring project."""

    route_class = Route

    def __init__(self, module, make_routes):
        self.make_routes = make_routes
        self.module = module
        self.initialize_unpacker()
        self.plugins = []
        self.plugin_types = []
        self.controller_plugins = []
        self.commands = None

    def initialize_unpacker(self):
        self.unpacker = UnpackRequest()
        self.unpacker.add('POST', lambda req: req.POST)
        self.unpacker.add('GET', lambda req: req.GET)
        self.unpacker.add('matchdict', lambda req: req.matchdict)
        self.unpacker.add('settings', lambda req: req.registry['settings'])
        self.unpacker.add('paths', lambda req: req.registry['paths'])
        self.unpacker.add('registry', lambda req: req.registry)
        self.unpacker.add('route', lambda req: req.route_path)

    def add_plugin(self, plugin):
        if type(plugin) in self.plugin_types:
            # add plugin only once
            # TODO: this should raise an error
            return
        plugin.init(self)
        plugin.add_unpackers()
        plugin.add_controller_plugins()
        self.plugin_types.append(type(plugin))
        self.plugins.append(plugin)

    def __call__(self, settings={}):
        self.generate_settings(settings)
        self.append_plugin_settings()
        self.walk_thru_plugins()
        self.make_before_config()
        self.create_config()
        self.make_after_config()
        self.make_pyramid_includes()
        self.init_routes()
        self.make_registry(self.config.registry)

        return self.config.make_wsgi_app()

    def init_routes(self):
        self.route = Route(self)
        self.make_routes(self, self.route)
        for plugin in self.plugins:
            plugin.append_routes()

    def run_commands(self, settings={}):
        self.generate_settings(settings)
        self.append_plugin_settings()
        self.commands = CommandsApplication(self)
        self.commands()

    def generate_settings(self, settings):
        self.settings, self.paths = self.get_settings(self.module, settings)
        self.settings['paths'] = self.paths
        return self.settings

    def get_settings(self, module, settings={}, additional_modules=None):
        additional_modules = additional_modules or [
            ('local', False),
        ]
        self.factory = Factory('%s.application' % (module))
        settings, paths = self.factory.make_settings(
            settings=settings,
            additional_modules=additional_modules,)
        settings['paths'] = paths
        return settings, paths

    def create_config(self):
        self.config = Configurator(
            settings=self.settings.to_dict(),
        )

    def append_plugin_settings(self):
        for plugin in self.plugins:
            plugin.append_settings()

    def walk_thru_plugins(self):
        for plugin in self.plugins:
            plugin.walk_thru_plugins()

    def make_before_config(self):
        for plugin in self.plugins:
            plugin.before_config()

    def make_after_config(self):
        for plugin in self.plugins:
            plugin.add_request_plugins()
        for plugin in self.plugins:
            plugin.after_config()

    def make_pyramid_includes(self):
        for plugin in self.plugins:
            plugin.make_config_include_if_able()
        self.config.commit()

    def make_registry(self, registry):
        registry['unpacker'] = self.unpacker
        registry['settings'] = self.settings
        registry['paths'] = self.paths
        registry['controller_plugins'] = self.controller_plugins
        for plugin in self.plugins:
            plugin.add_to_registry()

    def add_controller_plugin(self, plugin):
        self.controller_plugins.append(plugin)

    def _validate_dependency_plugin(self, plugin):
        if not plugin in self.plugin_types:
            raise PluginNotFound(plugin)

    def start_pytest_session(self):
        self.generate_settings({})
        self.append_plugin_settings()
        self.factory.run_module_without_errors('tests')
        hatak._test_cache['app'] = self

    def _import_from_string(self, url):
        url = url.split(':')
        module = import_module(url[0])
        return getattr(module, url[1])

    def get_plugin(self, cls):
        for plugin in self.plugins:
            if cls == type(plugin):
                return plugin
Esempio n. 7
0
class Application(object):

    """Prepering and configuring project."""

    route_class = Route

    def __init__(self, module, make_routes):
        self.make_routes = make_routes
        self.module = module
        self.initialize_unpacker()
        self.plugins = []
        self.plugin_types = []
        self.controller_plugins = []
        self.commands = None

    def initialize_unpacker(self):
        self.unpacker = UnpackRequest()
        self.unpacker.add('POST', lambda req: req.POST)
        self.unpacker.add('GET', lambda req: req.GET)
        self.unpacker.add('matchdict', lambda req: req.matchdict)
        self.unpacker.add('settings', lambda req: req.registry['settings'])
        self.unpacker.add('paths', lambda req: req.registry['paths'])
        self.unpacker.add('registry', lambda req: req.registry)
        self.unpacker.add('route', lambda req: req.route_path)

    def add_plugin(self, plugin):
        if type(plugin) in self.plugin_types:
            # add plugin only once
            # TODO: this should raise an error
            return
        plugin.init(self)
        plugin.add_unpackers()
        plugin.add_controller_plugins()
        self.plugin_types.append(type(plugin))
        self.plugins.append(plugin)

    def __call__(self, settings={}):
        self.generate_settings(settings)
        self.append_plugin_settings()
        self.make_before_config()
        self.create_config()
        self.make_after_config()
        self.make_pyramid_includes()
        self.init_routes()
        self.make_registry(self.config.registry)

        return self.config.make_wsgi_app()

    def init_routes(self):
        self.route = Route(self)
        self.make_routes(self, self.route)
        for plugin in self.plugins:
            plugin.append_routes()

    def run_commands(self, settings={}):
        self.generate_settings(settings)
        self.append_plugin_settings()
        self.commands = CommandsApplication(self)
        self.commands()

    def generate_settings(self, settings):
        self.settings, self.paths = self.get_settings(self.module, settings)
        self.settings['paths'] = self.paths
        return self.settings

    def get_settings(self, module, settings={}, additional_modules=None):
        additional_modules = additional_modules or [
            ('local', False),
        ]
        self.factory = Factory('%s.application' % (module))
        settings, paths = self.factory.make_settings(
            settings=settings,
            additional_modules=additional_modules,)
        settings['paths'] = paths
        return settings, paths

    def create_config(self):
        self.config = Configurator(
            settings=self.settings.to_dict(),
        )

    def append_plugin_settings(self):
        for plugin in self.plugins:
            plugin.append_settings()

    def make_before_config(self):
        for plugin in self.plugins:
            plugin.before_config()

    def make_after_config(self):
        for plugin in self.plugins:
            plugin.add_request_plugins()
        for plugin in self.plugins:
            plugin.after_config()

    def make_pyramid_includes(self):
        for plugin in self.plugins:
            plugin.make_config_include_if_able()
        self.config.commit()

    def make_registry(self, registry):
        registry['unpacker'] = self.unpacker
        registry['settings'] = self.settings
        registry['paths'] = self.paths
        registry['controller_plugins'] = self.controller_plugins
        for plugin in self.plugins:
            plugin.add_to_registry()

    def add_controller_plugin(self, plugin):
        self.controller_plugins.append(plugin)

    def _validate_dependency_plugin(self, plugin):
        if not plugin in self.plugin_types:
            raise PluginNotFound(plugin)

    def start_pytest_session(self):
        self.generate_settings({})
        self.append_plugin_settings()
        self.factory.run_module_without_errors('tests')
        hatak._test_cache['app'] = self
Esempio n. 8
0
class FactoryTest(TestCase):

    def setUp(self):
        super(FactoryTest, self).setUp()
        self.factory = Factory('main_modulepath', 'settings_modulepath')

        self.patchers = {}
        self.mocks = {}

        self._init_patchers()
        self._start_patchers()

    def tearDown(self):
        for name, patcher in self.patchers.items():
            try:
                patcher.stop()
            except RuntimeError:
                pass

    def _start_patchers(self):
        for name, patcher in self.patchers.items():
            self.mocks[name] = patcher.start()

    def _init_patchers(self):
        self.patchers['import'] = patch.object(self.factory, '_import_wrapper')

    def test_init(self):
        self.assertEqual('main_modulepath', self.factory.main_modulepath)
        self.assertEqual(
            'settings_modulepath', self.factory.settings_modulepath)

    def test_import(self):
        self.factory.import_module('something')
        self.mocks['import'].assert_called_once_with(
            'main_modulepath.settings_modulepath.something')

    def test_run_module(self):
        self.factory.settings = MagicMock()
        self.factory.paths = MagicMock()
        with patch.object(self.factory, 'import_module') as import_module:
            self.factory.run_module('some_name')

            import_module.assert_called_once_with('some_name')
            module = import_module.return_value
            module.make_settings.assert_called_once_with(
                self.factory.settings, self.factory.paths)

    def test_run_module_error(self):
        self.factory.settings = MagicMock()
        self.factory.paths = MagicMock()
        with patch.object(self.factory, 'import_module') as import_module:
            module = import_module.return_value
            module.make_settings.side_effect = ImportError()

            self.assertRaises(
                ImportError, self.factory.run_module, 'some_name')

            module.make_settings.assert_called_once_with(
                self.factory.settings, self.factory.paths)
            import_module.assert_called_once_with('some_name')

    def test_run_module_without_errors(self):
        self.factory.settings = MagicMock()
        self.factory.paths = MagicMock()
        with patch.object(self.factory, 'import_module') as import_module:
            self.factory.run_module_without_errors('some_name')

            import_module.assert_called_once_with('some_name')
            module = import_module.return_value
            module.make_settings.assert_called_once_with(
                self.factory.settings, self.factory.paths)

    def test_run_module_without_errors_error(self):
        self.factory.settings = MagicMock()
        self.factory.paths = MagicMock()
        with patch.object(self.factory, 'import_module') as import_module:
            module = import_module.return_value
            module.make_settings.side_effect = ImportError()

            self.factory.run_module_without_errors('some_name')

            import_module.assert_called_once_with('some_name')
            module.make_settings.assert_called_once_with(
                self.factory.settings, self.factory.paths)

    def test_init_data(self):
        self.mocks['import'].return_value.__file__ = '/one/two/three.py'
        self.factory.init_data({'settings': 1}, {'paths': 'path'})

        self.assertEqual({'settings': 1}, self.factory.settings)
        self.assertEqual(
            {'paths': ['path', ], 'project_path': ['/one/two']}, self.factory.paths)

    def test_make_settings(self):
        self.factory.settings = MagicMock()
        self.factory.paths = MagicMock()
        with patch.object(self.factory, 'init_data') as init_data:
            with patch.object(self.factory, 'run_module') as run_module:
                with patch.object(self.factory, 'run_module_without_errors') as run_module_without_errors:
                    result = self.factory.make_settings(
                        {'settings': 1}, {'paths': 'path'}, additional_modules=[('local1', False)])

                    self.assertEqual(
                        (self.factory.settings, self.factory.paths), result)
                    init_data.assert_called_once_with(
                        {'settings': 1}, {'paths': 'path'})
                    run_module.assert_called_once_with('default')
                    run_module_without_errors.assert_called_once_with('local1')

    def test_make_settings_with_error(self):
        self.factory.settings = MagicMock()
        self.factory.paths = MagicMock()
        with patch.object(self.factory, 'init_data') as init_data:
            with patch.object(self.factory, 'run_module') as run_module:
                with patch.object(self.factory, 'run_module_without_errors') as run_module_without_errors:
                    result = self.factory.make_settings(
                        {'settings': 1}, {'paths': 'path'}, additional_modules=[('local1', True)])

                    self.assertEqual(
                        (self.factory.settings, self.factory.paths), result)
                    init_data.assert_called_once_with(
                        {'settings': 1}, {'paths': 'path'})
                    run_module.assert_called_with('local1')
                    self.assertEqual(2, run_module.call_count)
                    self.assertEqual(0, run_module_without_errors.call_count)

    def test_import_wrapper_with_error(self):
        self.patchers['import'].stop()

        self.assertRaises(
            ImportError, self.factory._import_wrapper, 'something')

    def test_import_wrapper(self):
        self.patchers['import'].stop()
        with patch.dict(sys.modules):
            if 'os' in sys.modules:
                sys.modules.pop('os')
            module = self.factory._import_wrapper('os')
            self.assertTrue('os' in sys.modules)
            import os
            self.assertTrue(module is os)