[Zope-Checkins] CVS: Zope3/lib/python/Interface - pyskel.py:1.1.2.1

Jim Fulton jim@zope.com
Wed, 2 Jan 2002 16:02:18 -0500


Update of /cvs-repository/Zope3/lib/python/Interface
In directory cvs.zope.org:/tmp/cvs-serv2052

Added Files:
      Tag: Zope-3x-branch
	pyskel.py 
Log Message:
Simple utility to generate method skeletin from interfaces.
See the doc string.


=== Added File Zope3/lib/python/Interface/pyskel.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
# 
##############################################################################
"""
Generate method skeletins for intefaces.

Usage: python pyskel.py dotted_name

Example:

    cd lib/python
    python Interfaces/pyskel.py Zope.App.Security.IRoleService.IRoleService

The dotted name is the module name and interface object name connected
with a dot.

Revision information: $Id: pyskel.py,v 1.1.2.1 2002/01/02 21:02:18 jim Exp $
"""

import sys, os

sys.path.insert(0, os.getcwd())

from types import ModuleType
from Interface.Method import Method

def rskel(iface):
    name = iface.__name__
    for base in iface.__bases__:
        rskel(base)

    for aname, ades in iface.namesAndDescriptions():
        if isinstance(ades, Method):
            sig = ades.getSignatureString()[1:-1]
            if sig: sig = "self, %s" % sig
            else:   sig = "self"
            print
            print "    def %s(%s):" % (aname, sig)
            print "        '''See interface %s'''" % name

def skel(name):
    iface = resolve(name)
    print
    print "    # Implementation methods for interface"
    print "    #", name

    rskel(iface)
    

def resolve(name, _silly=('__doc__',), _globals={}):
    if name[:1]=='.':
        name='Zope.Products'+name

    if name[-1:] == '.':
        name = name[:-1]
        repeat = 1
    else:
        repeat = 0

    names=name.split('.')
    last=names[-1]
    mod='.'.join(names[:-1])
                 
    while 1:
        m=__import__(mod, _globals, _globals, _silly)
        try:
            a=getattr(m, last)
        except AttributeError:
            pass
        else:
            if not repeat or (type(a) is not ModuleType):
                return a
        mod += '.' + last
        
if __name__ == '__main__':
    for a in sys.argv[1:]:
        skel(a)