From 75c3bcd5f550d09510c8c0d1c4620e9dc24db202 Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Fri, 1 Apr 2016 11:17:44 +0300 Subject: [PATCH 1/2] Add util to split ramses.ignored_routes param --- ramses/__init__.py | 2 ++ ramses/utils.py | 9 +++++++++ tests/test_utils.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/ramses/__init__.py b/ramses/__init__.py index 089a34e..9bf5415 100644 --- a/ramses/__init__.py +++ b/ramses/__init__.py @@ -10,6 +10,7 @@ def includeme(config): from .generators import generate_server, generate_models + from .utils import split_ignored_resources Settings = dictset(config.registry.settings) config.include('nefertari.engine') @@ -56,6 +57,7 @@ def includeme(config): config.include('nefertari.elasticsearch') log.info('Starting server generation') + split_ignored_resources(config) generate_server(raml_root, config) log.info('Running nefertari.engine.setup_database') diff --git a/ramses/utils.py b/ramses/utils.py index e6a83ba..f571ac8 100644 --- a/ramses/utils.py +++ b/ramses/utils.py @@ -4,6 +4,8 @@ import six import inflection +from nefertari.utils import split_strip + log = logging.getLogger(__name__) @@ -337,3 +339,10 @@ def patch_view_model(view_cls, model_cls): yield finally: view_cls.Model = original_model + + +def split_ignored_routes(config): + """ Helper function to convert param string into list. """ + param = 'ramses.ignored_routes' + val = config.registry.settings.get(param, '') + config.registry.settings[param] = split_strip(val) diff --git a/tests/test_utils.py b/tests/test_utils.py index b6e1da6..aef5575 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -409,3 +409,17 @@ def test_patch_view_model(self): assert view_cls.Model is model1 assert not model1.called model2.assert_called_once_with() + + def test_split_ignored_routes_no_val(self): + config = Mock() + config.registry.settings = {} + utils.split_ignored_routes(config) + assert config.registry.settings['ramses.ignored_routes'] == [] + + def test_split_ignored_routes(self): + config = Mock() + config.registry.settings = { + 'ramses.ignored_routes': '/users/self,/stories/{id}'} + utils.split_ignored_routes(config) + assert set(config.registry.settings['ramses.ignored_routes']) == { + '/users/self', '/stories/{id}'} From 94b8bab86718db24001d5931e595e6fc41eb89b7 Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Fri, 1 Apr 2016 13:44:42 +0300 Subject: [PATCH 2/2] Dont generate ignored resources --- ramses/__init__.py | 4 ++-- ramses/generators.py | 4 +++- ramses/utils.py | 27 +++++++++++++++++++++++++-- tests/test_generators.py | 23 +++++++++++++++++++++-- tests/test_utils.py | 26 ++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 7 deletions(-) diff --git a/ramses/__init__.py b/ramses/__init__.py index 9bf5415..fdc5edc 100644 --- a/ramses/__init__.py +++ b/ramses/__init__.py @@ -10,7 +10,7 @@ def includeme(config): from .generators import generate_server, generate_models - from .utils import split_ignored_resources + from .utils import split_ignored_routes Settings = dictset(config.registry.settings) config.include('nefertari.engine') @@ -57,7 +57,7 @@ def includeme(config): config.include('nefertari.elasticsearch') log.info('Starting server generation') - split_ignored_resources(config) + split_ignored_routes(config) generate_server(raml_root, config) log.info('Running nefertari.engine.setup_database') diff --git a/ramses/generators.py b/ramses/generators.py index 16d6abc..dac0a22 100644 --- a/ramses/generators.py +++ b/ramses/generators.py @@ -7,7 +7,7 @@ from .utils import ( is_dynamic_uri, resource_view_attrs, generate_model_name, dynamic_part_name, attr_subresource, singular_subresource, - get_static_parent) + get_static_parent, is_ignored_resource) log = logging.getLogger(__name__) @@ -132,6 +132,8 @@ def generate_server(raml_root, config): for raml_resource in raml_root.resources: if raml_resource.path in generated_resources: continue + if is_ignored_resource(config, raml_resource): + continue # Get Nefertari parent resource parent_resource = _get_nefertari_parent_resource( diff --git a/ramses/utils.py b/ramses/utils.py index f571ac8..fa10cad 100644 --- a/ramses/utils.py +++ b/ramses/utils.py @@ -342,7 +342,30 @@ def patch_view_model(view_cls, model_cls): def split_ignored_routes(config): - """ Helper function to convert param string into list. """ + """ Helper function to convert param string into list. + + :param config: Pyramid Configurator instance. + """ param = 'ramses.ignored_routes' val = config.registry.settings.get(param, '') - config.registry.settings[param] = split_strip(val) + delim = ',' if ',' in val else '\n' + config.registry.settings[param] = split_strip(val, delim) + + +def is_ignored_resource(config, raml_resource): + """ Check if resource should be ignored. + + Route is considered ignored when its path starts with one of + paths from 'ramses.ignored_routes' config param. + + :param config: Pyramid Configurator instance. + :param raml_resource: Instance of ramlfications.raml.ResourceNode. + :returns bool: Indicating whether resource should be ignored. + """ + ignored_routes = config.registry.settings[ + 'ramses.ignored_routes'] + res_path = raml_resource.path + for path in ignored_routes: + if path and res_path.startswith(path): + return True + return False diff --git a/tests/test_generators.py b/tests/test_generators.py index 97d8462..0bdb379 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -33,10 +33,12 @@ def test_generate_server_no_resources(self, mock_gen): generators.generate_server(Mock(resources=None), 'foo') assert not mock_gen.called + @patch.object(generators, 'is_ignored_resource') @patch.object(generators, '_get_nefertari_parent_resource') @patch.object(generators, 'generate_resource') def test_generate_server_resources_generated( - self, mock_gen, mock_get): + self, mock_gen, mock_get, mock_check): + mock_check.return_value = False config = Mock() resources = [ Mock(path='/foo'), @@ -49,10 +51,12 @@ def test_generate_server_resources_generated( call(config, resources[1], mock_get()), ]) + @patch.object(generators, 'is_ignored_resource') @patch.object(generators, '_get_nefertari_parent_resource') @patch.object(generators, 'generate_resource') def test_generate_server_call_per_path( - self, mock_gen, mock_get): + self, mock_gen, mock_get, mock_check): + mock_check.return_value = False config = Mock() resources = [ Mock(path='/foo'), @@ -62,6 +66,21 @@ def test_generate_server_call_per_path( assert mock_get.call_count == 1 mock_gen.assert_called_once_with(config, resources[0], mock_get()) + @patch.object(generators, 'is_ignored_resource') + @patch.object(generators, '_get_nefertari_parent_resource') + @patch.object(generators, 'generate_resource') + def test_generate_server_ignored_resources( + self, mock_gen, mock_get, mock_check): + mock_check.return_value = True + config = Mock() + resources = [ + Mock(path='/foo'), + Mock(path='/bar'), + ] + generators.generate_server(Mock(resources=resources), config) + assert not mock_gen.called + assert not mock_get.called + @pytest.mark.usefixtures('engine_mock') class TestGenerateModels(object): diff --git a/tests/test_utils.py b/tests/test_utils.py index aef5575..3e5b7e4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -423,3 +423,29 @@ def test_split_ignored_routes(self): utils.split_ignored_routes(config) assert set(config.registry.settings['ramses.ignored_routes']) == { '/users/self', '/stories/{id}'} + + def test_split_ignored_routes_newline_delim(self): + config = Mock() + config.registry.settings = { + 'ramses.ignored_routes': '/users/self\n/stories/{id}'} + utils.split_ignored_routes(config) + assert set(config.registry.settings['ramses.ignored_routes']) == { + '/users/self', '/stories/{id}'} + + def test_is_ignored_resource_true(self): + config = Mock() + config.registry.settings = {'ramses.ignored_routes': ['/users']} + raml_res = Mock(path='/users') + assert utils.is_ignored_resource(config, raml_res) + + def test_is_ignored_resource_parent_true(self): + config = Mock() + config.registry.settings = {'ramses.ignored_routes': ['/users']} + raml_res = Mock(path='/users/{id}') + assert utils.is_ignored_resource(config, raml_res) + + def test_is_ignored_resource_false(self): + config = Mock() + config.registry.settings = {'ramses.ignored_routes': ['/users']} + raml_res = Mock(path='/stories') + assert not utils.is_ignored_resource(config, raml_res)