'Generate code for a single event'

def compileevent(self, event):

    [name, desc, code, subcode, returns, accepts, arguments] = event
    fp = self.fp
    funcname = identify(name)
    if arguments:
        fp.write(('                _argmap_%s    =    {\n' % funcname))
        for a in arguments:
            fp.write(('                                %r    :    %r,\n' % (identify(a[0]), a[1])))
        fp.write('                }\n\n')
    has_arg = (not is_null(accepts))
    opt_arg = (has_arg and is_optional(accepts))
    fp.write(('                def    %s(self,    ' % funcname))
    if has_arg:
        if (not opt_arg):
            fp.write('_object,    ')
        else:
            fp.write('_object=None,    ')
    else:
        fp.write('_no_object=None,    ')
    fp.write('_attributes={},    **_arguments):\n')
    fp.write(('                                """%s:    %s\n' % (ascii(name), ascii(desc))))
    if has_arg:
        fp.write(('                                Required    argument:    %s\n' % getdatadoc(accepts)))
    elif opt_arg:
        fp.write(('                                Optional    argument:    %s\n' % getdatadoc(accepts)))
    for arg in arguments:
        fp.write(('                                Keyword    argument    %s:    %s\n' % (identify(arg[0]), getdatadoc(arg[2]))))
    fp.write('                                Keyword    argument    _attributes:    AppleEvent    attribute    dictionary\n')
    if (not is_null(returns)):
        fp.write(('                                Returns:    %s\n' % getdatadoc(returns)))
    fp.write('                                """\n')
    fp.write(('                                _code    =    %r\n' % (code,)))
    fp.write(('                                _subcode    =    %r\n\n' % (subcode,)))
    if arguments:
        fp.write(('                                aetools.keysubst(_arguments,    self._argmap_%s)\n' % funcname))
    else:
        fp.write("                                if    _arguments:    raise    TypeError,    'No    optional    args    expected'\n")
    if has_arg:
        fp.write("                                _arguments['----']    =    _object\n")
    elif opt_arg:
        fp.write('                                if    _object:\n')
        fp.write("                                                _arguments['----']    =    _object\n")
    else:
        fp.write("                                if    _no_object    is    not    None:    raise    TypeError,    'No    direct    arg    expected'\n")
    fp.write('\n')
    for a in arguments:
        if is_enum(a[2]):
            kname = a[1]
            ename = a[2][0]
            if (ename != '****'):
                fp.write(('                                aetools.enumsubst(_arguments,    %r,    _Enum_%s)\n' % (kname, identify(ename))))
                self.enumsneeded[ename] = 1
    fp.write('\n')
    fp.write('                                _reply,    _arguments,    _attributes    =    self.send(_code,    _subcode,\n')
    fp.write('                                                                _arguments,    _attributes)\n')
    fp.write("                                if    _arguments.get('errn',    0):\n")
    fp.write('                                                raise    aetools.Error,    aetools.decodeerror(_arguments)\n')
    fp.write('                                #    XXXX    Optionally    decode    result\n')
    fp.write("                                if    '----'    in    _arguments:\n")
    if is_enum(returns):
        fp.write('                                                #    XXXX    Should    do    enum    remapping    here...\n')
    fp.write("                                                return    _arguments['----']\n")
    fp.write('\n')
