How to wrap a class's methods?

Michael Spencer mahs at telcopartners.com
Thu Feb 17 17:01:14 EST 2005


John Lenton wrote:
> On Thu, Feb 17, 2005 at 07:32:55PM +0000, Grant Edwards wrote:


> 
> I'd usually put big fat warnings around this code, and explain exaclty
> why I need to do things this way...
> 
> 

As a low-tech alternative, what about sourcecode generation, since you are 
targetting a python module?  This gives two advantages vs the wrapping function: 
1) the magic all occurs at coding time 2) the method signatures are documented.

Michael

import imaplib
import inspect
import types
instancemethod = types.MethodType

# The function template
funcwrapper = \
"""
     def %(name)s%(argspec)s:
         s,r = imaplib.IMAP4_SSL.%(name)s%(callspec)s
         if s!='OK':
             raise NotOK((s,r))
         return r"""

# A helper function to get the template parameters
def getargs(method):
     argspec = inspect.getargspec(method)
     callspec = tuple(argspec[:3] + (None,))# No default

     return {"name": method.__name__,
             "argspec": inspect.formatargspec(*argspec),
             "callspec": inspect.formatargspec(*callspec)}

# Do the stuff manually:
  >>> obj = imaplib.IMAP4_SSL
  >>> attrnames = [meth for meth in dir(imaplib.IMAP4_SSL) if not 
meth.startswith("_")]
  >>> attributes = [getattr(obj, attrname) for attrname in attrnames]
  >>> methods = [attribute for attribute in attributes if 
inspect.ismethod(attribute)]
  >>> print  "\n".join(funcwrapper % getargs(method) for method in methods)

     def append(self, mailbox, flags, date_time, message):
         s,r = imaplib.IMAP4_SSL.append(self, mailbox, flags, date_time, message)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def authenticate(self, mechanism, authobject):
         s,r = imaplib.IMAP4_SSL.authenticate(self, mechanism, authobject)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def check(self):
         s,r = imaplib.IMAP4_SSL.check(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def close(self):
         s,r = imaplib.IMAP4_SSL.close(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def copy(self, message_set, new_mailbox):
         s,r = imaplib.IMAP4_SSL.copy(self, message_set, new_mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def create(self, mailbox):
         s,r = imaplib.IMAP4_SSL.create(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def delete(self, mailbox):
         s,r = imaplib.IMAP4_SSL.delete(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def deleteacl(self, mailbox, who):
         s,r = imaplib.IMAP4_SSL.deleteacl(self, mailbox, who)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def expunge(self):
         s,r = imaplib.IMAP4_SSL.expunge(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def fetch(self, message_set, message_parts):
         s,r = imaplib.IMAP4_SSL.fetch(self, message_set, message_parts)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def getacl(self, mailbox):
         s,r = imaplib.IMAP4_SSL.getacl(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def getquota(self, root):
         s,r = imaplib.IMAP4_SSL.getquota(self, root)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def getquotaroot(self, mailbox):
         s,r = imaplib.IMAP4_SSL.getquotaroot(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def list(self, directory='""', pattern='*'):
         s,r = imaplib.IMAP4_SSL.list(self, directory, pattern)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def login(self, user, password):
         s,r = imaplib.IMAP4_SSL.login(self, user, password)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def login_cram_md5(self, user, password):
         s,r = imaplib.IMAP4_SSL.login_cram_md5(self, user, password)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def logout(self):
         s,r = imaplib.IMAP4_SSL.logout(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def lsub(self, directory='""', pattern='*'):
         s,r = imaplib.IMAP4_SSL.lsub(self, directory, pattern)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def myrights(self, mailbox):
         s,r = imaplib.IMAP4_SSL.myrights(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def namespace(self):
         s,r = imaplib.IMAP4_SSL.namespace(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def noop(self):
         s,r = imaplib.IMAP4_SSL.noop(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def open(self, host='', port=993):
         s,r = imaplib.IMAP4_SSL.open(self, host, port)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def partial(self, message_num, message_part, start, length):
         s,r = imaplib.IMAP4_SSL.partial(self, message_num, message_part, start, 
length)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def print_log(self):
         s,r = imaplib.IMAP4_SSL.print_log(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def proxyauth(self, user):
         s,r = imaplib.IMAP4_SSL.proxyauth(self, user)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def read(self, size):
         s,r = imaplib.IMAP4_SSL.read(self, size)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def readline(self):
         s,r = imaplib.IMAP4_SSL.readline(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def recent(self):
         s,r = imaplib.IMAP4_SSL.recent(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def rename(self, oldmailbox, newmailbox):
         s,r = imaplib.IMAP4_SSL.rename(self, oldmailbox, newmailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def response(self, code):
         s,r = imaplib.IMAP4_SSL.response(self, code)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def search(self, charset, *criteria):
         s,r = imaplib.IMAP4_SSL.search(self, charset, *criteria)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def select(self, mailbox='INBOX', readonly=None):
         s,r = imaplib.IMAP4_SSL.select(self, mailbox, readonly)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def send(self, data):
         s,r = imaplib.IMAP4_SSL.send(self, data)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def setacl(self, mailbox, who, what):
         s,r = imaplib.IMAP4_SSL.setacl(self, mailbox, who, what)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def setquota(self, root, limits):
         s,r = imaplib.IMAP4_SSL.setquota(self, root, limits)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def shutdown(self):
         s,r = imaplib.IMAP4_SSL.shutdown(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def socket(self):
         s,r = imaplib.IMAP4_SSL.socket(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def sort(self, sort_criteria, charset, *search_criteria):
         s,r = imaplib.IMAP4_SSL.sort(self, sort_criteria, charset, 
*search_criteria)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def ssl(self):
         s,r = imaplib.IMAP4_SSL.ssl(self)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def status(self, mailbox, names):
         s,r = imaplib.IMAP4_SSL.status(self, mailbox, names)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def store(self, message_set, command, flags):
         s,r = imaplib.IMAP4_SSL.store(self, message_set, command, flags)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def subscribe(self, mailbox):
         s,r = imaplib.IMAP4_SSL.subscribe(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def thread(self, threading_algorithm, charset, *search_criteria):
         s,r = imaplib.IMAP4_SSL.thread(self, threading_algorithm, charset, 
*search_criteria)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def uid(self, command, *args):
         s,r = imaplib.IMAP4_SSL.uid(self, command, *args)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def unsubscribe(self, mailbox):
         s,r = imaplib.IMAP4_SSL.unsubscribe(self, mailbox)
         if s!='OK':
             raise NotOK((s,r))
         return r

     def xatom(self, name, *args):
         s,r = imaplib.IMAP4_SSL.xatom(self, name, *args)
         if s!='OK':
             raise NotOK((s,r))
         return r
  >>>




More information about the Python-list mailing list