Commits

Alan Franzoni committed 37dc352

Better example

Comments (0)

Files changed (11)

.idea/libraries/Buildout_Eggs.xml

 <component name="libraryTable">
   <library name="Buildout Eggs">
     <CLASSES>
-      <root url="file://$PROJECT_DIR$/eggs/Twisted-11.0.0-py2.7-linux-i686.egg" />
-      <root url="file://$PROJECT_DIR$/eggs/mock-0.8.0alpha1-py2.7.egg" />
-      <root url="file://$PROJECT_DIR$/eggs/zope.interface-3.6.3-py2.7-linux-i686.egg" />
-      <root url="jar://$PROJECT_DIR$/eggs/distribute-0.6.19-py2.7.egg!/" />
+      <root url="file://$PROJECT_DIR$/../pydenji_release" />
+      <root url="file://$USER_HOME$/.buildout/eggs/ducktypes-0.2.4-py2.7.egg" />
+      <root url="jar://$USER_HOME$/.buildout/eggs/byteplay-0.2-py2.7.egg!/" />
+      <root url="file://$USER_HOME$/.buildout/eggs/configobj-4.7.2-py2.7.egg" />
+      <root url="file://$USER_HOME$/.buildout/eggs/Twisted-11.0.0-py2.7-macosx-10.4-x86_64.egg" />
+      <root url="file://$USER_HOME$/.buildout/eggs/mock-0.8.0alpha1-py2.7.egg" />
+      <root url="file://$USER_HOME$/.buildout/eggs/zope.interface-3.6.3-py2.7-macosx-10.4-x86_64.egg" />
+      <root url="file://$USER_HOME$/.buildout/eggs/distribute-0.6.19-py2.7.egg" />
     </CLASSES>
     <SOURCES />
   </library>
 [buildout]
-parts = python scripts
+parts = python scripts test
 develop = .
+    /Users/alan/sources/pydenji_release
 eggs = ep2011
+       pydenji
 
 [python]
 recipe = zc.recipe.egg
 [scripts]
 recipe = zc.recipe.egg:scripts
 eggs = ${buildout:eggs}
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = pydenji
+defaults = [ "--auto-color", "-vv", "--tests-pattern", "^test_.*" ]
+

ep2011/appcontext.py

+from pydenji.appcontext.context import AppContext
+from ep2011.pydenji_conf import UserService
+
+appcontext = AppContext()
+appcontext.register("db_driver", lambda: "stub_db_driver")
+appcontext.register_anonymous(UserService)
+
+appcontext.start()
+
+us = appcontext.provide("user_service")
+assert us is appcontext.provide("user_service")
+print us

ep2011/driveable.py

 
 class Driveable(object):
    __metaclass__ = ABCMeta
-
    def steerLeft(self):
        pass
-
    def steerRight(self):
        pass

ep2011/hardcoded.py

 
 class UserService(object):
     def __init__(self):
-        self.user_repo = UserRepository(my_db_driver)
+        self.user_repo = UserRepository(
+                            my_db_driver)
         
 

ep2011/interfaces.py

 class FilesystemUserRepository(object):
-    def __init__(self, base_directory, serializer, deserializer):
-        """implementation omitted."""
-
+    def __init__(self, base_directory,
+            serializer, deserializer):
+        """impl."""
     def save(self, user):
-        """implementation omitted."""
-
+        """impl."""
     def load_by_id(self, user_id):
-        """implementation omitted."""
-
+        """impl."""
 
 class DBBasedUserRepository(object):
     def __init__(self, db_driver):
-        """implementation omitted."""
+        """impl."""
+    def save(self, user):
+        """impl."""
+    def load_by_id(self, user_id):
+        """impl."""
 
-    def save(self, user):
-        """implementation omitted."""
-
-    def load_by_id(self, user_id):
-        """implementation omitted."""
-

ep2011/monkeypatching.py

-
-class SomeClass(object):
-    def do_something(self):
-        with open("/some/dir/file", "w") as f:
-            f.write(something)
-
 class SomeClassWithDefaultDir(object):
     base_path = "/some/dir/"
 
     def do_something(self):
-        with open(self.base_path + "file", "w") as f:
-            f.write(something)
+        with open(self.base_path +
+            "somefilename", "w") as f:
+            f.write("something")
 
 # refactored
 from twisted.python.filepath import FilePath
-class SomeClassWithDefaultDirAndFilePath(object):
-    base_path = "/some/dir/"
-    _root = FilePath(base_path)
+class SomeClassWFilePath(object):
+    _root = FilePath("/some/dir")
 
     def do_something(self):
-        with self._root.child("file").open("w") as f:
-            f.write(something)
+        with self._root.child(
+            "somefilename").open("w") as f:
+            f.write("something")
 
-class SomeClassWithDefaultResourceOpener(object):
-    base_path = "/some/dir"
-    def __init__(self, resource_opener=open):
-        self.resource_opener = resource_opener
+class SomeClassWithInjectedResource(object):
+    def __init__(self, resource):
+        self.resource = resource
 
     def do_something(self):
-        with self.resource_opener(self.base_path + "file", "w") as f:
-            f.write(something)
+        with self.resource as f:
+            f.write("something")
 
 
 

ep2011/mysupercar.py

-
 class MySuperCar(object):
     def turnLeft(self):
-        """implementation omitted."""
+        """impl."""
     def turnRight(self):
-        """implementation omitted."""
+        """impl."""
     def makeBaconAndEggs(self):
-        """implementation omitted."""
+        """impl."""

ep2011/pydenji_conf.py

+from pydenji.config.pythonconfig import Configuration
+from pydenji.config.provider import provider, singleton, prototype
+from pydenji.importer import NI
+
+class UserService(Configuration):
+    @provider(scope=singleton, lazy_init=True)
+    def user_service(self):
+        return NI("ep2011.users.UserService")(
+            self._user_repository())
+    @provider()
+    def _user_repository(self):
+        db_driver = self.appcontext.provide(
+            "db_driver")
+        repo = NI("ep2011.users.UserRepository")(
+            db_driver)
+        return repo
+    def set_app_context(self, appcontext):
+        self.appcontext = appcontext

ep2011/test/test_monkeypatching.py

 
 from pickle import dump, load
 
-from ep2011.monkeypatching import SomeClass, SomeClassWithDefaultDir, SomeClassWithDefaultDirAndFilePath, SomeClassWithDefaultResourceOpener
+from ep2011.monkeypatching import SomeClassWithDefaultDir, SomeClassWFilePath, SomeClassWithInjectedResource
 
 
 class TestSomeClassWithMonkeyPatching(TestCase):
     def setUp(self):
-        self.tmpdir = mkdtemp("ep2011test")
+        self._tmpdir = mkdtemp("ep2011test")
+        self.tmpfilename = self._tmpdir + os.sep + "persist"
 
     def tearDown(self):
-        rmtree(self.tmpdir)
+        rmtree(self._tmpdir)
 
-# of course you could just stub the basepath in this context.
-    
-    @patch("ep2011.monkeypatching.open", create=True)
-    def test_repository_can_retrieve_saved_object(self, mock_open):
-        persist = open(self.tmpdir + os.sep + "persist", "w")
+    @patch("ep2011.monkeypatching.open",
+           create=True)
+    def test_dstn(self, mock_open):
+        persist = open(self.tmpfilename, "w")
         mock_open.return_value = persist
-        repo = HardcodedFSBasedUserRepo()
-        user = MockUser(123)
-        repo.save(user)
-        mock_open.return_value = open(self.tmpdir + os.sep + "persist")
-        load_user = repo.load_by_id(123)
-        self.assertEquals(user, load_user)
+        sc = SomeClassWithDefaultDir()
+        sc.do_something()
+        self.assertEquals("something",
+            open(self.tmpfilename).read())
 
-    @patch("ep2011.repository.open", create=True)
-    def test_refactored_repository_saves_objects(self, mock_open):
-        persist = open(self.tmpdir + os.sep + "persist", "w")
+    @patch("ep2011.monkeypatching.open",
+           create=True)
+    def test_dstn_fp(self, mock_open):
+        persist = open(self.tmpfilename, "w")
         mock_open.return_value = persist
-        repo = RefactoredHardcodedRepo()
-        user = MockUser(123)
-        repo.save(user)
-        mock_open.return_value = open(self.tmpdir + os.sep + "persist")
-        load_user = repo.load_by_id(123)
-        self.assertEquals(user, load_user)
+        sc = SomeClassWFilePath()
+        sc.do_something()
+        self.assertEquals("something",
+            open(self.tmpfilename).read())
+
+    def test_inj(self):
+        f = open(self.tmpfilename, "w")
+        s = SomeClassWithInjectedResource(f)
+        s.do_something()
+        self.assertEquals("something",
+                open(self.tmpfilename).read())
 
 
 
+class UserService(object):
+    def __init__(self, repo):
+        pass
+
+def UserRepository(object):
+    def __init__(self, db_driver):
+        pass
+