Created
March 30, 2012 14:58
-
-
Save schuster-rainer/2252113 to your computer and use it in GitHub Desktop.
create a proxy class from the baseclass with methods publish by the method projector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import logging | |
def interrogate(item): | |
"""Print useful information about item.""" | |
if hasattr(item, '__name__'): | |
logging.debug( "NAME: " + item.__name__) | |
if hasattr(item, '__class__'): | |
logging.debug("CLASS: " + item.__class__.__name__) | |
logging.debug("ID: {0}".format(id(item))) | |
logging.debug("TYPE: {0}".format(type(item))) | |
logging.debug("VALUE: " + repr(item)) | |
logging.debug("CALLABLE:") | |
if callable(item): | |
logging.debug("Yes") | |
else: | |
logging.debug("No") | |
if hasattr(item, '__doc__'): | |
doc = getattr(item, '__doc__') | |
if doc: | |
doc = doc.strip() # Remove leading/trailing whitespace. | |
firstline = doc.split('\n')[0] | |
logging.debug( "DOC: ", firstline) | |
def create_public_method_proxy(component, method_projector, baseclass, class_suffix='ServerProxy'): | |
interrogate(component) | |
methods = inspect.getmembers(component, inspect.ismethod) | |
public_methods = { method_projector: method | |
for method_name, method in methods | |
if not method_name.startswith("_")} | |
logging.debug(public_methods) | |
class_name = component.__class__.__name__ + class_suffix | |
proxy_class = type(class_name, (baseclass,), public_methods ) | |
return proxy_class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment