def connect(component, controller=None):
<EOL>if not controller or isinstance(controller, dict):<EOL><INDENT>if not controller:<EOL><INDENT>controller = util.get_caller_module_dict()<EOL><DEDENT>controller_name = controller['<STR_LIT>']<EOL>controller_dict = controller<EOL><DEDENT>else:<EOL><INDENT>controller_name = controller.__class__.__name__<EOL>controller_dict = dict([(k, getattr(controller, k)) for k <EOL>in dir(controller) if k.startswith("<STR_LIT>")])<EOL><DEDENT>for fn in [n for n in controller_dict if n.startswith("<STR_LIT>")]:<EOL><INDENT>names = fn.split("<STR_LIT:_>")<EOL>event_name = names.pop(<NUM_LIT:0>) + names.pop(-<NUM_LIT:1>)<EOL>obj = component<EOL>for name in names:<EOL><INDENT>try:<EOL><INDENT>obj = obj[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>obj = None<EOL>break<EOL><DEDENT><DEDENT>if not obj:<EOL><INDENT>from .component import COMPONENTS<EOL>for key, obj in list(COMPONENTS.items()):<EOL><INDENT>if obj.name == name:<EOL><INDENT>print("<STR_LIT>" % (name, key.replace("<STR_LIT:.>", "<STR_LIT:_>")))<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise NameError("<STR_LIT>" % <EOL>(name, controller_name, fn))<EOL><DEDENT><DEDENT>if event_name in PYTHONCARD_EVENT_MAP:<EOL><INDENT>new_name = PYTHONCARD_EVENT_MAP[event_name]<EOL>print("<STR_LIT>" % (event_name, new_name, fn))<EOL>event_name = new_name<EOL><DEDENT>if not hasattr(obj, event_name):<EOL><INDENT>raise NameError("<STR_LIT>" % <EOL>(event_name, controller_name, fn))<EOL><DEDENT>setattr(obj, event_name, controller_dict[fn])<EOL><DEDENT>
Associate event handlers