Rev 2407: Change lazy_import to allow proxying when necessary. in http://bzr.arbash-meinel.com/branches/bzr/0.16-dev/doc-cleanup

John Arbash Meinel john at arbash-meinel.com
Tue Apr 10 00:19:55 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.16-dev/doc-cleanup

------------------------------------------------------------
revno: 2407
revision-id: john at arbash-meinel.com-20070409231939-prjmbgzw9fj3zqon
parent: john at arbash-meinel.com-20070409221122-qh66j4pk3kqxjn9c
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: doc-cleanup
timestamp: Mon 2007-04-09 18:19:39 -0500
message:
  Change lazy_import to allow proxying when necessary.
  Write a monkey-patch for epydoc.uid.ObjectUID so that it can understand
  a lazy_import object.
  With the monkey-patch in place, epydoc seems to be able to process bzrlib.
  Now we still need to fix the errors. :)
added:
  tools/bzr_epydoc               bzr_epydoc-20070409231252-ec7uffstn62q7nhv-1
  tools/bzr_epydoc_uid.py        bzr_epydoc_uid.py-20070409231252-ec7uffstn62q7nhv-2
modified:
  Makefile                       Makefile-20050805140406-d96e3498bb61c5bb
  bzrlib/lazy_import.py          lazy_import.py-20060910203832-f77c54gf3n232za0-1
-------------- next part --------------
=== added file 'tools/bzr_epydoc'
--- a/tools/bzr_epydoc	1970-01-01 00:00:00 +0000
+++ b/tools/bzr_epydoc	2007-04-09 23:19:39 +0000
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+#
+# Call the command line interface for Epydoc.
+#
+
+"""Wrapper around Epydoc that just makes it understand bzr's lazy imports."""
+
+# This will enable the lazy_import compatibility code
+import bzr_epydoc_uid
+
+from epydoc.cli import cli
+cli()
+
+

=== added file 'tools/bzr_epydoc_uid.py'
--- a/tools/bzr_epydoc_uid.py	1970-01-01 00:00:00 +0000
+++ b/tools/bzr_epydoc_uid.py	2007-04-09 23:19:39 +0000
@@ -0,0 +1,43 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Monkey patch to make epydoc work with bzrlib's lazy imports."""
+
+import epydoc.uid
+
+import bzrlib.lazy_import
+
+
+_ObjectUID = epydoc.uid.ObjectUID
+_ScopeReplacer = bzrlib.lazy_import.ScopeReplacer
+
+
+class ObjectUID(_ObjectUID):
+
+    def __init__(self, obj):
+        if isinstance(obj, _ScopeReplacer):
+            # The isinstance will trigger a replacement if it is a real
+            # _BzrScopeReplacer, but the local object won't know about it, so
+            # replace it locally.
+            obj = object.__getattribute__(obj, '_real_obj')
+        _ObjectUID.__init__(self, obj)
+
+
+epydoc.uid.ObjectUID = ObjectUID
+
+
+bzrlib.lazy_import._scope_replacer_should_proxy = True
+

=== modified file 'Makefile'
--- a/Makefile	2007-04-09 20:25:47 +0000
+++ b/Makefile	2007-04-09 23:19:39 +0000
@@ -49,6 +49,10 @@
 	mkdir -p api
 	pydoctor -c pydoctor_bzrlib.cfg --make-html
 
+epydoc-api-docs:
+	mkdir -p api/epydoc
+	PYTHONPATH=$(PWD) python tools/bzr_epydoc --html -o api/epydoc --docformat 'restructuredtext en' bzr bzrlib
+
 
 # build emacs cross-reference
 tag_files=./bzr ./bzrlib/*py ./bzrlib/selftest/*.py

=== modified file 'bzrlib/lazy_import.py'
--- a/bzrlib/lazy_import.py	2006-10-16 01:25:46 +0000
+++ b/bzrlib/lazy_import.py	2007-04-09 23:19:39 +0000
@@ -40,6 +40,8 @@
     to inherit from them).
 """
 
+_scope_replacer_should_proxy = False
+
 
 class ScopeReplacer(object):
     """A lazy object that will replace itself in the appropriate scope.
@@ -48,7 +50,7 @@
     needed.
     """
 
-    __slots__ = ('_scope', '_factory', '_name')
+    __slots__ = ('_scope', '_factory', '_name', '_real_obj')
 
     def __init__(self, scope, factory, name):
         """Create a temporary object in the specified scope.
@@ -62,6 +64,7 @@
         self._scope = scope
         self._factory = factory
         self._name = name
+        self._real_obj = None
         scope[name] = self
 
     def _replace(self):
@@ -81,6 +84,8 @@
                           " to another variable?",
                 extra=e)
         obj = factory(self, scope, name)
+        if _scope_replacer_should_proxy:
+            self._real_obj = obj
         scope[name] = obj
         return obj
 
@@ -92,10 +97,12 @@
         # del self._name
 
     def __getattribute__(self, attr):
-        _replace = object.__getattribute__(self, '_replace')
-        obj = _replace()
-        _cleanup = object.__getattribute__(self, '_cleanup')
-        _cleanup()
+        obj = object.__getattribute__(self, '_real_obj')
+        if obj is None:
+            _replace = object.__getattribute__(self, '_replace')
+            obj = _replace()
+            _cleanup = object.__getattribute__(self, '_cleanup')
+            _cleanup()
         return getattr(obj, attr)
 
     def __call__(self, *args, **kwargs):



More information about the bazaar-commits mailing list