Repository: ettoreleandrotognoli/python-cdi

Issue:
Boot
Import a group of files to boot the CDI
Define files with some type of wildcard 

Comments:

Patch:
diff --git a/.gitignore b/.gitignore
index 967cc90..b514e5f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
 *.egg-info
-venv/
+venv*/
 dist/
 build/
 htmlcov/
diff --git a/pycdi/utils.py b/pycdi/utils.py
index 56d715c..6643e7c 100644
--- a/pycdi/utils.py
+++ b/pycdi/utils.py
@@ -1,5 +1,10 @@
 # -*- encoding: utf-8 -*-
+import glob
 import random
+import sys
+
+import os
+from six import string_types
 
 from .core import CDIDecorator, DEFAULT_CONTAINER
 
@@ -52,3 +57,18 @@ class Provide(CDIDecorator):
             return self.container.call(func, *args, **kwargs)
 
         return wrapper
+
+
+def boot_cdi(paths=['*_cdi.py'], root=None):
+    root = sys.path if root is None else root
+    if isinstance(root, string_types):
+        root = [root]
+    libs = []
+    for base_path in root:
+        for path in paths:
+            search_rule = os.path.join(base_path, path)
+            for file_name in list(glob.iglob(search_rule)):
+                file_name, ext = os.path.splitext(file_name)
+                lib = '.'.join(filter(None, file_name[len(base_path):].split(os.sep)))
+                libs.append(lib)
+    return list(map(__import__, libs))


Test patch:
diff --git a/tests/common/boot/__init__.py b/tests/common/boot/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/common/boot/deep/__init__.py b/tests/common/boot/deep/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/common/boot/deep/me_too_cdi.py b/tests/common/boot/deep/me_too_cdi.py
new file mode 100644
index 0000000..a2bdd49
--- /dev/null
+++ b/tests/common/boot/deep/me_too_cdi.py
@@ -0,0 +1,6 @@
+from pycdi import Producer
+
+
+@Producer(str, _context='me_too')
+def producer():
+    return __name__
diff --git a/tests/common/boot/load_me_cdi.py b/tests/common/boot/load_me_cdi.py
new file mode 100644
index 0000000..f7fa3c9
--- /dev/null
+++ b/tests/common/boot/load_me_cdi.py
@@ -0,0 +1,6 @@
+from pycdi import Producer
+
+
+@Producer(str, _context='load_me')
+def producer():
+    return __name__
diff --git a/tests/common/test_boot.py b/tests/common/test_boot.py
new file mode 100644
index 0000000..efede6b
--- /dev/null
+++ b/tests/common/test_boot.py
@@ -0,0 +1,34 @@
+from unittest import TestCase
+
+import os
+
+from pycdi.shortcuts import new
+from pycdi.utils import boot_cdi
+
+DIR = os.path.dirname(__file__)
+
+
+class BootTest(TestCase):
+    def test_boot_with_root(self):
+        expected = [
+            'boot.load_me_cdi',
+            'boot.deep.me_too_cdi',
+        ]
+        boot_cdi(paths=['boot/*_cdi.py', 'boot/deep/*_cdi.py'], root=DIR)
+        result = [
+            new(str, 'load_me'),
+            new(str, 'me_too')
+        ]
+        self.assertEqual(expected, result)
+
+    def test_boot_without_root(self):
+        expected = [
+            'boot.load_me_cdi',
+            'boot.deep.me_too_cdi',
+        ]
+        boot_cdi(paths=['boot/*_cdi.py', 'boot/deep/*_cdi.py'])
+        result = [
+            new(str, 'load_me'),
+            new(str, 'me_too')
+        ]
+        self.assertEqual(expected, result)
diff --git a/tests/common/test_call.py b/tests/common/test_call.py
index 6cb4f85..6ca4097 100644
--- a/tests/common/test_call.py
+++ b/tests/common/test_call.py
@@ -34,3 +34,13 @@ class CallTest(TestCase):
             self.assertIsNotNone(cdi)
 
         self.container.call(django_view, object(), pk=1)
+
+    def test_override_cdi(self):
+        @Inject(cdi=CDIContainer)
+        def django_view(request, pk, cdi):
+            self.assertIsNotNone(request)
+            self.assertIsNotNone(pk)
+            self.assertIsNotNone(cdi)
+            self.assertEqual(cdi, 'fuu')
+
+        self.container.call(django_view, object(), pk=1, cdi='fuu')