Skip to content

Commit 813ea53

Browse files
committed
Refine the pipelines registration from files to support SubSubclasses #237
Signed-off-by: Thomas Druez <tdruez@nexb.com>
1 parent 15fe08d commit 813ea53

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

scanpipe/apps.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ def register_pipeline_from_file(self, path):
105105
Search for a Pipeline subclass in the provided file `path` and register it
106106
when found.
107107
"""
108-
module_name = path.stem
108+
module_name = inspect.getmodulename(path)
109109
module = SourceFileLoader(module_name, str(path)).load_module()
110-
pipeline_classes = inspect.getmembers(module, is_pipeline)
110+
111+
def is_local_module_pipeline(obj):
112+
return is_pipeline(obj) and obj.__module__ == module_name
113+
114+
pipeline_classes = inspect.getmembers(module, is_local_module_pipeline)
111115

112116
if len(pipeline_classes) > 1:
113117
raise ImproperlyConfigured(

scanpipe/pipelines/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def get_steps(cls):
6363
return cls.steps()
6464

6565
warnings.warn(
66-
"Defining ``steps`` as a tuple is deprecated. "
67-
"Use a ``steps(cls)`` classmethod instead."
66+
f"Defining ``steps`` as a tuple is deprecated in {cls} "
67+
f"Use a ``steps(cls)`` classmethod instead."
6868
)
6969
return cls.steps
7070

scanpipe/tests/pipelines/register_from_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/nexB/scancode.io for support and download.
2222

23-
from scanpipe.pipelines import Pipeline
23+
from scanpipe.tests.pipelines.do_nothing import DoNothing
2424

2525

26-
class RegisterFromFile(Pipeline):
26+
class RegisterFromFile(DoNothing):
2727
"""
2828
A pipeline to be registered from its file path.
2929
"""

scanpipe/tests/test_apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from django.test import override_settings
2929

3030
from scanpipe.apps import ScanPipeConfig
31+
from scanpipe.models import Project
3132
from scanpipe.tests import license_policies
3233
from scanpipe.tests import license_policies_index
3334
from scanpipe.tests.pipelines.register_from_file import RegisterFromFile
@@ -86,3 +87,10 @@ def test_scanpipe_apps_register_pipeline_from_file(self):
8687
RegisterFromFile.__name__,
8788
scanpipe_app.pipelines.get("register_from_file").__name__,
8889
)
90+
91+
project1 = Project.objects.create(name="Analysis")
92+
run = project1.add_pipeline("register_from_file")
93+
pipeline_instance = run.make_pipeline_instance()
94+
95+
exitcode, output = pipeline_instance.execute()
96+
self.assertEqual(0, exitcode)

scanpipe/tests/test_pipelines.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ def test_scanpipe_pipelines_is_pipeline(self):
121121
self.assertFalse(is_pipeline(Pipeline))
122122
self.assertTrue(is_pipeline(DoNothing))
123123

124+
class SubSubClass(DoNothing):
125+
pass
126+
127+
self.assertTrue(is_pipeline(SubSubClass))
128+
124129
def test_scanpipe_pipelines_class_get_graph(self):
125130
expected = [
126131
{"doc": "Step1 doc.", "name": "step1"},
@@ -159,8 +164,8 @@ def test_scanpipe_pipelines_class_get_steps(self):
159164
caught_warning = caught_warnings[0]
160165

161166
expected = (
162-
"Defining ``steps`` as a tuple is deprecated. "
163-
"Use a ``steps(cls)`` classmethod instead."
167+
f"Defining ``steps`` as a tuple is deprecated in {StepsAsAttribute} "
168+
f"Use a ``steps(cls)`` classmethod instead."
164169
)
165170
self.assertEqual(expected, str(caught_warning.message))
166171

0 commit comments

Comments
 (0)