[Checkins] SVN: martian/trunk/ * Ignore .pyc and .pyo files that don't have a matching .py file via
Chris McDonough
chrism at plope.com
Wed Dec 24 15:07:39 EST 2008
Log message for revision 94309:
* Ignore .pyc and .pyo files that don't have a matching .py file via
``module_info_from_dotted_name`` if its ``ignore_nonsource``
parameter is ``True``. The default is ``True``. To revert to the
older behavior where .pyc files were honored, pass
``ignore_nonsource=False``.
* Pass along ``exclude_filter`` (and the new ``ignore_nonsource``
flag) to ModuleInfo constructor when it calls itself recursively.
(this is an effective merge of the ignore-pyc-branch).
Changed:
U martian/trunk/CHANGES.txt
U martian/trunk/src/martian/core.py
U martian/trunk/src/martian/scan.py
U martian/trunk/src/martian/scan.txt
A martian/trunk/src/martian/tests/withpyconly/
A martian/trunk/src/martian/tests/withpyconly/__init__.py
A martian/trunk/src/martian/tests/withpyconly/foo.py
A martian/trunk/src/martian/tests/withpyconly/subpackage/
A martian/trunk/src/martian/tests/withpyconly/subpackage/__init__.py
-=-
Modified: martian/trunk/CHANGES.txt
===================================================================
--- martian/trunk/CHANGES.txt 2008-12-24 15:04:44 UTC (rev 94308)
+++ martian/trunk/CHANGES.txt 2008-12-24 20:07:39 UTC (rev 94309)
@@ -11,6 +11,15 @@
These are sometimes created by editors, operating systems and
network file systems and we don't want to confuse them.
+* Ignore .pyc and .pyo files that don't have a matching .py file via
+ ``module_info_from_dotted_name`` if its ``ignore_nonsource``
+ parameter is ``True``. The default is ``True``. To revert to the
+ older behavior where .pyc files were honored, pass
+ ``ignore_nonsource=False``.
+
+* Pass along ``exclude_filter`` (and the new ``ignore_nonsource``
+ flag) to ModuleInfo constructor when it calls itself recursively.
+
0.11 (2008-09-24)
=================
Modified: martian/trunk/src/martian/core.py
===================================================================
--- martian/trunk/src/martian/core.py 2008-12-24 15:04:44 UTC (rev 94308)
+++ martian/trunk/src/martian/core.py 2008-12-24 20:07:39 UTC (rev 94309)
@@ -196,8 +196,10 @@
self.register(InstanceMetaGrokker(self))
self.register(GlobalMetaGrokker(self))
-def grok_dotted_name(dotted_name, grokker, exclude_filter=None, **kw):
- module_info = scan.module_info_from_dotted_name(dotted_name, exclude_filter)
+def grok_dotted_name(dotted_name, grokker, exclude_filter=None,
+ ignore_nonsource=True, **kw):
+ module_info = scan.module_info_from_dotted_name(dotted_name, exclude_filter,
+ ignore_nonsource)
grok_package(module_info, grokker, **kw)
def grok_package(module_info, grokker, **kw):
Modified: martian/trunk/src/martian/scan.py
===================================================================
--- martian/trunk/src/martian/scan.py 2008-12-24 15:04:44 UTC (rev 94308)
+++ martian/trunk/src/martian/scan.py 2008-12-24 20:07:39 UTC (rev 94309)
@@ -32,14 +32,17 @@
class ModuleInfo(object):
implements(IModuleInfo)
- def __init__(self, path, dotted_name, exclude_filter=None):
+ def __init__(self, path, dotted_name, exclude_filter=None,
+ ignore_nonsource=True):
# Normalize .pyc files to .py
if path.endswith('c'):
path = path[:-1]
self.path = path
self.dotted_name = dotted_name
+ self.ignore_nonsource = ignore_nonsource
if exclude_filter is None:
+ # this exclude filter receives extensionless filenames
self.exclude_filter = lambda x: False
else:
self.exclude_filter = exclude_filter
@@ -82,6 +85,9 @@
dotted_name = self.dotted_name + '.' + name
if self.exclude_filter(name):
continue
+ if self.ignore_nonsource:
+ if ext in ['.pyo', '.pyc']:
+ continue
# Case one: modules
if (os.path.isfile(entry_path) and ext in ['.py', '.pyc']):
if name == '__init__':
@@ -90,24 +96,36 @@
if name in seen:
continue
seen.append(name)
- module_infos.append(ModuleInfo(entry_path, dotted_name))
+ module_infos.append(
+ ModuleInfo(entry_path,
+ dotted_name,
+ exclude_filter=self.exclude_filter,
+ ignore_nonsource=self.ignore_nonsource)
+ )
# Case two: packages
elif is_package(entry_path):
# We can blindly use __init__.py even if only
# __init__.pyc exists because we never actually use
# that filename.
module_infos.append(ModuleInfo(
- os.path.join(entry_path, '__init__.py'), dotted_name))
+ os.path.join(entry_path, '__init__.py'),
+ dotted_name,
+ exclude_filter=self.exclude_filter,
+ ignore_nonsource=self.ignore_nonsource))
return module_infos
def getSubModuleInfo(self, name):
path = os.path.join(os.path.dirname(self.path), name)
if is_package(path):
return ModuleInfo(os.path.join(path, '__init__.py'),
- '%s.%s' % (self.package_dotted_name, name))
+ '%s.%s' % (self.package_dotted_name, name),
+ exclude_filter=self.exclude_filter,
+ ignore_nonsource=self.ignore_nonsource)
elif os.path.isfile(path + '.py') or os.path.isfile(path + '.pyc'):
return ModuleInfo(path + '.py',
- '%s.%s' % (self.package_dotted_name, name))
+ '%s.%s' % (self.package_dotted_name, name),
+ exclude_filter=self.exclude_filter,
+ ignore_nonsource = self.ignore_nonsource)
else:
return None
@@ -159,7 +177,8 @@
def getAnnotation(self, key, default):
return default
-def module_info_from_dotted_name(dotted_name, exclude_filter=None):
+def module_info_from_dotted_name(dotted_name, exclude_filter=None,
+ ignore_nonsource=True):
if dotted_name == '__builtin__':
# in case of the use of individually grokking something during a
# test the dotted_name being passed in could be __builtin__
@@ -167,10 +186,12 @@
# implements enough interface to work
return BuiltinModuleInfo()
module = resolve(dotted_name)
- return ModuleInfo(module.__file__, dotted_name, exclude_filter)
+ return ModuleInfo(module.__file__, dotted_name, exclude_filter,
+ ignore_nonsource)
-def module_info_from_module(module, exclude_filter=None):
- return ModuleInfo(module.__file__, module.__name__, exclude_filter)
+def module_info_from_module(module, exclude_filter=None, ignore_nonsource=True):
+ return ModuleInfo(module.__file__, module.__name__, exclude_filter,
+ ignore_nonsource)
# taken from zope.dottedname.resolve
Modified: martian/trunk/src/martian/scan.txt
===================================================================
--- martian/trunk/src/martian/scan.txt 2008-12-24 15:04:44 UTC (rev 94308)
+++ martian/trunk/src/martian/scan.txt 2008-12-24 20:07:39 UTC (rev 94309)
@@ -216,3 +216,30 @@
>>> module_info.getSubModuleInfos()
[<ModuleInfo object for 'martian.tests.withbogusmodules.nonbogus'>,
<ModuleInfo object for 'martian.tests.withbogusmodules.subpackage'>]
+
+Packages which contain .pyc files only
+--------------------------------------
+
+When a .py file in a package is renamed, an "orphaned" .pyc object is
+often left around. By default, Martian will ignore such .pyc files:
+
+ >>> import martian.tests.withpyconly
+ >>> from martian.tests.withpyconly import foo
+ >>> d = os.path.abspath(os.path.dirname(martian.tests.withpyconly.__file__))
+ >>> os.rename(os.path.join(d, 'foo.py'), os.path.join(d, 'foo.py_aside'))
+ >>> module_info = module_info_from_dotted_name(
+ ... 'martian.tests.withpyconly')
+ >>> module_info.getSubModuleInfos()
+ [<ModuleInfo object for 'martian.tests.withpyconly.subpackage'>]
+
+However, if ``ignore_nonsource=False`` is passed to
+``module_info_from_dotted_name`` (or friends, such as
+``module_info_from_module``), we will pick up these modules::
+
+ >>> module_info = module_info_from_dotted_name(
+ ... 'martian.tests.withpyconly', ignore_nonsource=False)
+ >>> module_info.getSubModuleInfos()
+ [<ModuleInfo object for 'martian.tests.withpyconly.foo'>,
+ <ModuleInfo object for 'martian.tests.withpyconly.subpackage'>]
+ >>> # rename back to normal name
+ >>> os.rename(os.path.join(d, 'foo.py_aside'), os.path.join(d, 'foo.py'))
Added: martian/trunk/src/martian/tests/withpyconly/foo.py
===================================================================
--- martian/trunk/src/martian/tests/withpyconly/foo.py (rev 0)
+++ martian/trunk/src/martian/tests/withpyconly/foo.py 2008-12-24 20:07:39 UTC (rev 94309)
@@ -0,0 +1 @@
+# hello
More information about the Checkins
mailing list