[Zope3-checkins] SVN: Zope3/trunk/src/ Deprecated passing context as a first argument to createObject.

Jim Fulton jim at zope.com
Tue Feb 22 09:35:25 EST 2005


Log message for revision 29248:
  Deprecated passing context as a first argument to createObject.
  If context needs to be passed, it is passed as a keyword argument.
  All of the existing uses passed None. (Chickens rejoice!)
  

Changed:
  U   Zope3/trunk/src/bugtracker/browser/bug.py
  U   Zope3/trunk/src/bugtracker/browser/comment.py
  U   Zope3/trunk/src/zope/app/apidoc/utilities.py
  U   Zope3/trunk/src/zope/app/component/tests/test_directives.py
  U   Zope3/trunk/src/zope/app/i18n/translationdomain.py
  U   Zope3/trunk/src/zope/app/onlinehelp/browser/__init__.py
  U   Zope3/trunk/src/zope/app/sqlexpr/sqlexpr.py
  U   Zope3/trunk/src/zope/component/__init__.py
  U   Zope3/trunk/src/zope/component/factory.txt
  U   Zope3/trunk/src/zope/component/interfaces.py
  U   Zope3/trunk/src/zope/component/socketexample.txt
  U   Zope3/trunk/src/zwiki/browser/wiki.py
  U   Zope3/trunk/src/zwiki/browser/wikipage.py

-=-
Modified: Zope3/trunk/src/bugtracker/browser/bug.py
===================================================================
--- Zope3/trunk/src/bugtracker/browser/bug.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/bugtracker/browser/bug.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -72,7 +72,7 @@
     def getDescription(self):
         ttype = getattr(self.context.description, 'ttype', None)
         if ttype is not None:
-            source = zapi.createObject(None, self.context.description.ttype,
+            source = zapi.createObject(self.context.description.ttype,
                                        self.context.description)
             view = zapi.getMultiAdapter(
                 (removeAllProxies(source), self.request))

Modified: Zope3/trunk/src/bugtracker/browser/comment.py
===================================================================
--- Zope3/trunk/src/bugtracker/browser/comment.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/bugtracker/browser/comment.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -53,7 +53,7 @@
     def body(self):
         ttype = getattr(self.context.body, 'ttype', None)
         if ttype is not None:
-            source = zapi.createObject(None, self.context.body.ttype,
+            source = zapi.createObject(self.context.body.ttype,
                                        self.context.body)
             view = zapi.getMultiAdapter(
                 (removeAllProxies(source), self.request))

Modified: Zope3/trunk/src/zope/app/apidoc/utilities.py
===================================================================
--- Zope3/trunk/src/zope/app/apidoc/utilities.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/app/apidoc/utilities.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -543,7 +543,7 @@
     assert format in _format_dict.values()
 
     if text:
-        source = zapi.createObject(None, format, text)
+        source = zapi.createObject(format, text)
         renderer = zapi.getMultiAdapter((source, TestRequest()))
         return renderer.render()
     else:

Modified: Zope3/trunk/src/zope/app/component/tests/test_directives.py
===================================================================
--- Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -1069,7 +1069,7 @@
 
     def testFactory(self):
 
-        self.assertRaises(ComponentLookupError, zapi.createObject, None, 'foo')
+        self.assertRaises(ComponentLookupError, zapi.createObject, 'foo')
 
         xmlconfig(StringIO(template % (
             '''
@@ -1081,7 +1081,7 @@
             )))
 
         from factory import X
-        self.assertEqual(zapi.createObject(None, 'foo.bar').__class__, X)
+        self.assertEqual(zapi.createObject('foo.bar').__class__, X)
 
 
 class ParticipationStub(object):
@@ -1116,7 +1116,7 @@
        />
 </content>''')
         xmlconfig(f)
-        obj = createObject(None, 'test.Example')
+        obj = createObject('test.Example')
         self.failUnless(zapi.isinstance(obj, exampleclass.ExampleClass))
 
 

Modified: Zope3/trunk/src/zope/app/i18n/translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/app/i18n/translationdomain.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/app/i18n/translationdomain.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -165,7 +165,7 @@
 
     def addLanguage(self, language):
         'See `IWriteTranslationDomain`'
-        catalog = zapi.createObject(None, u'zope.app.MessageCatalog',
+        catalog = zapi.createObject(u'zope.app.MessageCatalog',
                                     language)
         self[language] = catalog
 

Modified: Zope3/trunk/src/zope/app/onlinehelp/browser/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/onlinehelp/browser/__init__.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/app/onlinehelp/browser/__init__.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -36,8 +36,7 @@
 
     def topicContent(self):
         """ render the source of the help topic """
-        source = zapi.createObject(None,
-                                   self.context.type,
+        source = zapi.createObject(self.context.type,
                                    self.context.source)
         view = zapi.getMultiAdapter((source, self.request))
         html = view.render()

Modified: Zope3/trunk/src/zope/app/sqlexpr/sqlexpr.py
===================================================================
--- Zope3/trunk/src/zope/app/sqlexpr/sqlexpr.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/app/sqlexpr/sqlexpr.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -45,7 +45,7 @@
             rdb = econtext.vars['rdb']
             dsn = econtext.vars['dsn']
             try:
-                adapter = zapi.createObject(None, rdb, dsn)
+                adapter = zapi.createObject(rdb, dsn)
             except ComponentLookupError:
                 raise ConnectionError, \
                       ("The factory id, '%s', you specified in the `rdb` "

Modified: Zope3/trunk/src/zope/component/__init__.py
===================================================================
--- Zope3/trunk/src/zope/component/__init__.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/component/__init__.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -263,8 +263,21 @@
 
 # Factories
 
-def createObject(context, name, *args, **kwargs):
-    return getUtility(IFactory, name, context)(*args, **kwargs)
+def createObject(__factory_name, *args, **kwargs):
+    # BBB
+    if not isinstance(__factory_name, basestring):
+        import warnings
+        warnings.warn(
+            "Passing a context as a first argument to createObject is "
+            "deprecated.  It will be unsupported in Zope X3.3.  Use a "
+            "context keyword argument instead.",
+            DeprecationWarning, 2)
+        context = __factory_name
+        __factory_name, args = args[0], args[1:]
+    else:
+        context = kwargs.pop('context', None)
+    
+    return getUtility(IFactory, __factory_name, context)(*args, **kwargs)
 
 def getFactoryInterfaces(name, context=None):
     return getUtility(IFactory, name, context).getInterfaces()

Modified: Zope3/trunk/src/zope/component/factory.txt
===================================================================
--- Zope3/trunk/src/zope/component/factory.txt	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/component/factory.txt	2005-02-22 14:35:24 UTC (rev 29248)
@@ -106,7 +106,7 @@
 Creating an Object
 ++++++++++++++++++
 
-  >>> kl = capi.createObject(None, 'klass', 1, 2, foo=3, bar=4)
+  >>> kl = capi.createObject('klass', 1, 2, foo=3, bar=4)
   >>> isinstance(kl, Klass)
   True
   >>> kl.args
@@ -128,3 +128,4 @@
 
   >>> [(name, fac.__class__) for name, fac in capi.getFactoriesFor(IKlass)]
   [(u'klass', <class 'zope.component.factory.Factory'>)]
+

Modified: Zope3/trunk/src/zope/component/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/component/interfaces.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/component/interfaces.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -235,16 +235,17 @@
 
     # Factory service
 
-    # TODO: Hard to make context a keyword, leaving as it is. Maybe we should
-    #       at least move it to the second position.
-    def createObject(context, name, *args, **kwargs):
+    def createObject(factory_name, *args, **kwargs):
         """Create an object using a factory
 
-        Finds the factory of the given name that is nearest to the
-        context, and passes the other given arguments to the factory
-        to create a new instance. Returns a reference to the new
-        object.  If a matching factory cannot be found raises
-        ComponentLookupError
+        Finds the named factory in the current site and calls it with
+        the given arguments.  If a matching factory cannot be found
+        raises ComponentLookupError.  Returns the created object.
+
+        A context keyword argument can be provided to cause the
+        factory to be looked up in a location other than the current
+        site.  (Of course, this means that it is impossible to pass a
+        keyword argument named "context" to the factory.
         """
 
     def getFactoryInterfaces(name, context=None):

Modified: Zope3/trunk/src/zope/component/socketexample.txt
===================================================================
--- Zope3/trunk/src/zope/component/socketexample.txt	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zope/component/socketexample.txt	2005-02-22 14:35:24 UTC (rev 29248)
@@ -514,7 +514,7 @@
 
 Of course you can also just create an object:
 
-  >>> panel = capi.createObject(None, 'SolarPanel')
+  >>> panel = capi.createObject('SolarPanel')
   >>> panel.__class__ is SolarPanel
   True
 

Modified: Zope3/trunk/src/zwiki/browser/wiki.py
===================================================================
--- Zope3/trunk/src/zwiki/browser/wiki.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zwiki/browser/wiki.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -33,7 +33,7 @@
 
         if self.request.get('frontpage'):
             page = removeAllProxies(
-                zapi.createObject(None, 'zwiki.WikiPage'))
+                zapi.createObject('zwiki.WikiPage'))
             page.type = u'zope.source.rest'
             page.source = u'This is the FrontPage of the Wiki.'
             dc = ICMFDublinCore(page)

Modified: Zope3/trunk/src/zwiki/browser/wikipage.py
===================================================================
--- Zope3/trunk/src/zwiki/browser/wikipage.py	2005-02-22 11:40:26 UTC (rev 29247)
+++ Zope3/trunk/src/zwiki/browser/wikipage.py	2005-02-22 14:35:24 UTC (rev 29248)
@@ -125,7 +125,7 @@
 
     def render(self):
         """Render the wiki page source."""
-        source = zapi.createObject(None, self.context.type, self.context.source)
+        source = zapi.createObject(self.context.type, self.context.source)
         view = zapi.getMultiAdapter((removeAllProxies(source), self.request))
         html = view.render()
         html = self.renderWikiLinks(html)
@@ -135,7 +135,7 @@
         result = []
         for name, comment in self.context.items():
             dc = DublinCoreViews(comment, self.request)
-            source = zapi.createObject(None, comment.type, comment.source)
+            source = zapi.createObject(comment.type, comment.source)
             view = zapi.getMultiAdapter(
                 (removeAllProxies(source), self.request))
             result.append({



More information about the Zope3-Checkins mailing list