[Checkins] SVN: zope.fixers/trunk/ First import.

Lennart Regebro regebro at gmail.com
Fri Apr 3 12:44:34 EDT 2009


Log message for revision 98846:
  First import.
  

Changed:
  A   zope.fixers/trunk/README.txt
  A   zope.fixers/trunk/docs/
  A   zope.fixers/trunk/docs/HISTORY.txt
  A   zope.fixers/trunk/setup.cfg
  A   zope.fixers/trunk/setup.py
  A   zope.fixers/trunk/zope/
  A   zope.fixers/trunk/zope/__init__.py
  A   zope.fixers/trunk/zope/fixers/
  A   zope.fixers/trunk/zope/fixers/__init__.py
  A   zope.fixers/trunk/zope/fixers/fix_implements.py
  A   zope.fixers/trunk/zope/fixers/tests/
  A   zope.fixers/trunk/zope/fixers/tests/__init__.py

-=-
Added: zope.fixers/trunk/README.txt
===================================================================
--- zope.fixers/trunk/README.txt	                        (rev 0)
+++ zope.fixers/trunk/README.txt	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,4 @@
+Introduction
+============
+
+Fixers for Zope Component Architecture and the frameworks built with it.

Added: zope.fixers/trunk/docs/HISTORY.txt
===================================================================
--- zope.fixers/trunk/docs/HISTORY.txt	                        (rev 0)
+++ zope.fixers/trunk/docs/HISTORY.txt	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,8 @@
+Changelog
+=========
+
+1.0 - Unreleased
+----------------
+
+* Initial release
+

Added: zope.fixers/trunk/setup.cfg
===================================================================
--- zope.fixers/trunk/setup.cfg	                        (rev 0)
+++ zope.fixers/trunk/setup.cfg	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,3 @@
+[egg_info]
+tag_build = dev
+tag_svn_revision = true

Added: zope.fixers/trunk/setup.py
===================================================================
--- zope.fixers/trunk/setup.py	                        (rev 0)
+++ zope.fixers/trunk/setup.py	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,33 @@
+from setuptools import setup, find_packages
+import os
+
+version = '1.0'
+
+setup(name='zope.fixers',
+      version=version,
+      description="2to3 fixers for Zope",
+      long_description=open("README.txt").read() + "\n" +
+                       open(os.path.join("docs", "HISTORY.txt")).read(),
+      # Get more strings from http://www.python.org/pypi?%3Aaction=list_classifiers
+      classifiers=[
+        "Programming Language :: Python",
+        "Topic :: Software Development :: Libraries :: Python Modules",
+        ],
+      keywords='2to3 python3 zope',
+      author='Lennart Regebro',
+      author_email='regebro at gmail.com',
+      url='',
+      license='ZPL',
+      packages=find_packages(exclude=['ez_setup']),
+      namespace_packages=['zope'],
+      include_package_data=True,
+      zip_safe=True,
+      install_requires=[
+          'setuptools',
+          # -*- Extra requirements: -*-
+      ],
+      entry_points="""
+      # -*- Entry points: -*-
+      """,
+      test_suite = 'zope.fixers.tests',
+      )

Added: zope.fixers/trunk/zope/__init__.py
===================================================================
--- zope.fixers/trunk/zope/__init__.py	                        (rev 0)
+++ zope.fixers/trunk/zope/__init__.py	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,6 @@
+# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
+try:
+    __import__('pkg_resources').declare_namespace(__name__)
+except ImportError:
+    from pkgutil import extend_path
+    __path__ = extend_path(__path__, __name__)

Added: zope.fixers/trunk/zope/fixers/fix_implements.py
===================================================================
--- zope.fixers/trunk/zope/fixers/fix_implements.py	                        (rev 0)
+++ zope.fixers/trunk/zope/fixers/fix_implements.py	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,92 @@
+"""Fixer for implements(IX) -> @implementor(IX).
+
+"""
+# Author: Lennart Regebro, based on Jack Diederich's metaclass fixer
+
+# Local imports
+from lib2to3.fixer_base import BaseFix
+from lib2to3.patcomp import PatternCompiler
+from lib2to3.fixer_util import syms, Name
+from lib2to3.fixer_util import Node, Leaf
+
+helper = dict([(b,a) for (a,b) in syms.__dict__.items()])
+
+class FixImplements(BaseFix):
+
+    NAMED_IMPORT_PATTERN = """
+    import_from< 'from' dotted_name< 'zope' '.' 'interface' > 'import' import_as_names< any* (name='implements') any* > >
+    |
+    import_from< 'from' dotted_name< 'zope' '.' 'interface' > 'import' name='implements' any* >
+    """
+    
+    RENAMED_IMPORT_PATTERN = """
+    import_from< 'from' dotted_name< 'zope' '.' 'interface' > 'import' import_as_name< name='implements' 'as' rename='renamed' any*> >
+    """
+    
+    CLASS_PATTERN = """
+    classdef< 'class' any* ':' suite< any* simple_stmt< power< statement='%s' trailer < '(' interface=any ')' > any* > any* > any* > >
+    """
+
+    IMPLEMENTS_PATTERN = """
+    simple_stmt< power< old_statement='%s' trailer < '(' any* ')' > > any* >
+    """
+    #classdef< 'class' 'Foo' ':' suite<  simple_stmt< power< 'implements' trailer< '(' 'IFoo' ')' > > '\n' > '' > >
+    
+    fixups = []
+    
+    def compile_pattern(self):
+        """Compiles self.PATTERN into self.pattern.
+
+        Subclass may override if it doesn't want to use
+        self.{pattern,PATTERN} in .match().
+        """
+        self.named_import_pattern = PatternCompiler().compile_pattern(self.NAMED_IMPORT_PATTERN)
+        self.renamed_import_pattern = PatternCompiler().compile_pattern(self.RENAMED_IMPORT_PATTERN)
+            
+    def start_tree(self, tree, filename):
+        self.matches = ['implements']
+        super(FixImplements, self).start_tree(tree, filename)
+        
+    def match(self, node):
+        # Matches up the imports
+        results = {"node": node}
+        if self.named_import_pattern.match(node, results):
+            return results
+        if self.renamed_import_pattern.match(node, results):
+            return results
+        for name in self.matches:
+            pattern = PatternCompiler().compile_pattern(self.CLASS_PATTERN % name)
+            if pattern.match(node, results):
+                return results
+            pattern = PatternCompiler().compile_pattern(self.IMPLEMENTS_PATTERN % name)
+            if pattern.match(node, results):
+                return results
+                
+    def transform(self, node, results):
+        if 'name' in results:
+            # This matched an import statement. Fix that up:
+            name = results["name"]
+            name.replace(Name("implementor", prefix=name.get_prefix()))
+            if 'rename' in results:
+                # The import statement use import as
+                self.matches.append(results['rename'].value)
+        if 'statement' in results:
+            # This matched a class that has an impements(IFoo) statement.
+            # Stick a class decorator first.
+            statement = results['statement'].value
+            interface = results['interface'].value
+            if statement == 'implements':
+                statement = 'implementor'
+            else:
+                statement = results['statement'].value
+            decorator = Node(syms.decorator, [Leaf(50, '@'), Leaf(1, statement), 
+                                              Leaf(7, '('), Leaf(1, interface), 
+                                              Leaf(8, ')'), Leaf(4, '\n')])
+            node.insert_child(0, decorator)
+        if 'old_statement' in results:
+            # This matched an implements statement. We'll remove it.
+            self.fixups.append(node)
+    
+    def finish_tree(self, tree, filename):
+        for node in self.fixups:
+            node.remove()

Added: zope.fixers/trunk/zope/fixers/tests/__init__.py
===================================================================
--- zope.fixers/trunk/zope/fixers/tests/__init__.py	                        (rev 0)
+++ zope.fixers/trunk/zope/fixers/tests/__init__.py	2009-04-03 16:44:34 UTC (rev 98846)
@@ -0,0 +1,66 @@
+import unittest
+from lib2to3.refactor import RefactoringTool
+
+example = """
+# Basic test:
+from zope.interface import Interface, implements, providedBy
+from zope.interface import providedBy, implements, Interface
+from zope.interface import providedBy, implements
+from zope.interface import implements, Interface
+from zope.interface import implements
+from zope.interface import implements as renamed
+
+class IFoo(Interface):
+    pass
+
+class Foo:
+    "An IFoo class"
+    
+    implements(IFoo)
+    
+class IBar(Interface):
+    pass
+    
+class Bar:
+    "An IBar class"
+    
+    renamed(IBar)
+    
+# Test ends
+"""
+
+target = """
+# Basic test:
+from zope.interface import Interface, implementor, providedBy
+from zope.interface import providedBy, implementor, Interface
+from zope.interface import providedBy, implementor
+from zope.interface import implementor, Interface
+from zope.interface import implementor
+from zope.interface import implementor as renamed
+
+class IFoo(Interface):
+    pass
+
+ at implementor(IFoo)
+class Foo:
+    "An IFoo class"
+    
+class IBar(Interface):
+    pass
+    
+ at renamed(IBar)
+class Bar:
+    "An IBar class"
+    
+# Test ends
+"""
+
+class FixerTest(unittest.TestCase):
+    
+    def test_test(self):
+        #import pdb;pdb.set_trace()
+        tool = RefactoringTool(['zope.fixers.fix_implements'])
+        refactored = str(tool.refactor_string(example, 'zope.fixer.test'))
+
+        assert refactored == target
+        
\ No newline at end of file



More information about the Checkins mailing list