[Checkins] SVN: zope.introspector/trunk/src/zope/introspector/code.py Improve the code browser with support for files.

Martijn Faassen faassen at infrae.com
Tue Jul 22 12:55:49 EDT 2008


Log message for revision 88714:
  Improve the code browser with support for files. 
  

Changed:
  U   zope.introspector/trunk/src/zope/introspector/code.py

-=-
Modified: zope.introspector/trunk/src/zope/introspector/code.py
===================================================================
--- zope.introspector/trunk/src/zope/introspector/code.py	2008-07-22 16:00:21 UTC (rev 88713)
+++ zope.introspector/trunk/src/zope/introspector/code.py	2008-07-22 16:55:47 UTC (rev 88714)
@@ -1,3 +1,4 @@
+import grokcore.component as grok
 from grokcore.component.interfaces import IContext
 import types
 from martian.scan import module_info_from_dotted_name
@@ -3,4 +4,6 @@
 from martian.util import isclass
 from zope.interface import implements
+from zope.introspector.interfaces import IInfo
+import os
 
 class Code(object):
@@ -16,14 +19,34 @@
         self._module_info = module_info_from_dotted_name(dotted_name)
     
 class Package(PackageOrModule):
+    def getPath(self):
+        return os.path.dirname(self._module_info.path)
+    
     def __getitem__(self, name):
         sub_module = self._module_info.getSubModuleInfo(name)
         if sub_module is None:
-            raise KeyError
+            file = File(self.dotted_name, name)
+            # if the file exists, use it, otherwise it's a KeyError - no
+            # file is here
+            if file.exists():
+                return file
+            else:
+                raise KeyError
         if sub_module.isPackage():
             return Package(sub_module.dotted_name)
         return Module(sub_module.dotted_name)
 
+class PackageInfo(grok.Adapter):
+    grok.context(Package)
+    grok.provides(IInfo)
+    grok.name('package')
+
+    def getPackageFiles(self):
+        pkg_file_path = self.context.getPath()
+        return sorted([x for x in os.listdir(pkg_file_path)
+               if os.path.isfile(os.path.join(pkg_file_path, x))
+               and (x.endswith('.txt') or x.endswith('.rst'))])
+
 class Module(PackageOrModule):
     def __getitem__(self, name):
         module = self._module_info.getModule()
@@ -38,6 +61,18 @@
         else:
             return Instance(sub_dotted_name)
 
+class File(Code):
+    def __init__(self, dotted_name, name):
+        super(File, self).__init__(dotted_name)
+        self.name = name
+
+    def exists(self):
+        """Check whether the file is a file we want to consider."""
+        module_info = module_info_from_dotted_name(self.dotted_name)
+        path = module_info.getResourcePath(self.name)
+        return (os.path.isfile(path) and
+                (path.endswith('.rst') or path.endswith('.txt')))
+
 class Class(Code):
     pass
 



More information about the Checkins mailing list