[Checkins] SVN: grok/trunk/src/grok/ implemented the 105th version
of a thing that scans packages for their modules.
Wolfgang Schnerring
wosc at wosc.de
Tue Oct 17 06:40:41 EDT 2006
Log message for revision 70737:
implemented the 105th version of a thing that scans packages for their modules.
Changed:
A grok/trunk/src/grok/scan.py
A grok/trunk/src/grok/tests/scan/
A grok/trunk/src/grok/tests/scan/__init__.py
A grok/trunk/src/grok/tests/scan/scan.py
A grok/trunk/src/grok/tests/scan/stoneage/
A grok/trunk/src/grok/tests/scan/stoneage/__init__.py
A grok/trunk/src/grok/tests/scan/stoneage/cave.py
A grok/trunk/src/grok/tests/scan/stoneage/hunt/
A grok/trunk/src/grok/tests/scan/stoneage/hunt/__init__.py
A grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py
A grok/trunk/src/grok/tests/scan/stoneage/notpackage/
A grok/trunk/src/grok/tests/scan/stoneage/notpackage/dummy.py
A grok/trunk/src/grok/tests/scan/stoneage/painting/
A grok/trunk/src/grok/tests/scan/stoneage/painting/__init__.py
U grok/trunk/src/grok/tests/test_grok.py
-=-
Added: grok/trunk/src/grok/scan.py
===================================================================
--- grok/trunk/src/grok/scan.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/scan.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1,56 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Scanning packages and modules
+"""
+
+import os
+from glob import glob
+import setuptools
+from zope.dottedname.resolve import resolve
+
+def modules(dotted_name, module_path):
+ yield dotted_name
+
+ if not (module_path.endswith('__init__.py')
+ or module_path.endswith('__init__.pyc')):
+ return
+
+ package_directory = os.path.dirname(module_path)
+ seen = []
+ for entry in sorted(os.listdir(package_directory)):
+ entry_path = os.path.join(package_directory, entry)
+
+ if entry in ['__init__.py', '__init__.pyc']:
+ continue
+ elif os.path.isfile(entry_path) and (entry.endswith('.py')
+ or entry.endswith('.pyc')):
+ module_name = os.path.splitext(entry)[0]
+ if module_name in seen:
+ continue
+ seen.append(module_name)
+ yield '%s.%s' % (dotted_name, module_name)
+ else:
+ if os.path.isdir(entry_path):
+ init_py = os.path.join(entry_path, '__init__.py')
+ init_pyc = os.path.join(entry_path, '__init__.pyc')
+
+ if os.path.exists(init_py):
+ init_path = init_py
+ elif os.path.exists(init_pyc):
+ init_path = init_pyc
+ else:
+ continue
+
+ for name in modules('%s.%s' % (dotted_name, entry), init_path):
+ yield name
Added: grok/trunk/src/grok/tests/scan/__init__.py
===================================================================
--- grok/trunk/src/grok/tests/scan/__init__.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/__init__.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1 @@
+# this is a package
Added: grok/trunk/src/grok/tests/scan/scan.py
===================================================================
--- grok/trunk/src/grok/tests/scan/scan.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/scan.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1,19 @@
+"""
+ >>> import grok.scan
+
+If you call grok.scan.modules() on a module, it yields the module name:
+
+ >>> from grok.tests.scan.stoneage import cave
+ >>> list(grok.scan.modules('grok.tests.scan.stoneage.cave', cave.__file__))
+ ['grok.tests.scan.stoneage.cave']
+
+If you call it on a package, it yield a list of the names of the
+package, its modules, and all its subpackages and their modules.
+
+ >>> from grok.tests.scan import stoneage
+ >>> list(grok.scan.modules('grok.tests.scan.stoneage', stoneage.__file__))
+ ['grok.tests.scan.stoneage', 'grok.tests.scan.stoneage.cave',
+ 'grok.tests.scan.stoneage.hunt', 'grok.tests.scan.stoneage.hunt.mammoth',
+ 'grok.tests.scan.stoneage.painting']
+
+"""
Added: grok/trunk/src/grok/tests/scan/stoneage/__init__.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/__init__.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/stoneage/__init__.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1 @@
+# this is a package
Added: grok/trunk/src/grok/tests/scan/stoneage/cave.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/cave.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/stoneage/cave.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1,12 @@
+import grok
+
+class Cave(grok.Model):
+ pass
+
+index = grok.PageTemplate("""\
+<html>
+<body>
+<h1>A comfy cave</h1>
+</body>
+</html>
+""")
Added: grok/trunk/src/grok/tests/scan/stoneage/hunt/__init__.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/hunt/__init__.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/stoneage/hunt/__init__.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1 @@
+# this is a package
Added: grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/stoneage/hunt/mammoth.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1,12 @@
+import grok
+
+class Mammoth(grok.Model):
+ pass
+
+index = grok.PageTemplate("""\
+<html>
+<body>
+<h1>ME GROK HUNT MAMMOTH!</h1>
+</body>
+</html>
+""")
Added: grok/trunk/src/grok/tests/scan/stoneage/notpackage/dummy.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/notpackage/dummy.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/stoneage/notpackage/dummy.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1 @@
+# foo
Added: grok/trunk/src/grok/tests/scan/stoneage/painting/__init__.py
===================================================================
--- grok/trunk/src/grok/tests/scan/stoneage/painting/__init__.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/scan/stoneage/painting/__init__.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -0,0 +1 @@
+# this is a package
Modified: grok/trunk/src/grok/tests/test_grok.py
===================================================================
--- grok/trunk/src/grok/tests/test_grok.py 2006-10-17 09:06:53 UTC (rev 70736)
+++ grok/trunk/src/grok/tests/test_grok.py 2006-10-17 10:40:40 UTC (rev 70737)
@@ -26,7 +26,7 @@
def test_suite():
suite = unittest.TestSuite()
- for name in ['adapter', 'error', 'view', 'security']:
+ for name in ['adapter', 'error', 'view', 'security', 'scan']:
suite.addTest(suiteFromPackage(name))
return suite
More information about the Checkins
mailing list