a „oeŠNć@sNdZddlZddlmZddd„Zdd„Zd d „Zd d „Zd d„Zdd„Z dS)a¼ Utility functions for writing out gateway C++ files This module will generate a C++/Python binding for a specific COM interface. At this stage, no command line interface exists. You must start Python, import this module, change to the directory where the generated code should be written, and run the public function. This module is capable of generating both 'Interfaces' (ie, Python client side support for the interface) and 'Gateways' (ie, Python server side support for the interface). Many COM interfaces are useful both as Client and Server. Other interfaces, however, really only make sense to implement one side or the other. For example, it would be pointless for Python to implement Server side for 'IRunningObjectTable', unless we were implementing core COM for an operating system in Python (hey - now there's an idea!) Most COM interface code is totally boiler-plate - it consists of converting arguments, dispatching the call to Python, and processing any result values. This module automates the generation of such code. It has the ability to parse a .H file generated by the MIDL tool (ie, almost all COM .h files) and build almost totally complete C++ code. The module understands some of the well known data types, and how to convert them. There are only a couple of places where hand-editing is necessary, as detailed below: unsupported types -- If a type is not known, the generator will pretty much ignore it, but write a comment to the generated code. You may want to add custom support for this type. In some cases, C++ compile errors will result. These are intentional - generating code to remove these errors would imply a false sense of security that the generator has done the right thing. other return policies -- By default, Python never sees the return SCODE from a COM function. The interface usually returns None if OK, else a COM exception if "FAILED(scode)" is TRUE. You may need to change this if: * EXCEPINFO is passed to the COM function. This is not detected and handled * For some reason Python should always see the result SCODE, even if it did fail or succeed. For example, some functions return a BOOLEAN result in the SCODE, meaning Python should always see it. * FAILED(scode) for the interface still has valid data to return (by default, the code generated does not process the return values, and raise an exception to Python/COM éNé)Ś makegwparsec CsLt|ƒ}zt ||”}W| ”n | ”0|r:|r:d}n|rH|sHd}nd}|jdd…dkr|ddl}|jjj}|jjj } nt }t } td|jd ƒ} zL|   d |j|f”|   d |j”|rČ|| |ƒ|rÖ| | |ƒW|  ”n |  ”0td |jd ƒ} z@|   d |j|f”|r"t | |ƒ|r2t| |ƒW|  ”n |  ”0dS)aGenerate C++ code for a Python Interface and Gateway header_file_name -- The full path to the .H file which defines the interface. interface_name -- The name of the interface to search for, and to generate. bMakeInterface = 1 -- Should interface (ie, client) support be generated. bMakeGatewayInterface = 1 -- Should gateway (ie, server) support be generated. This method will write a .cpp and .h file into the current directory, (using the name of the interface to build the file name. zInterface and GatewayZ InterfaceZGatewayNéZIEnumrzPy%s.cppŚwz`// This file implements the %s %s for Python. // Generated by makegw.py #include "shell_pch.h" zG#include "Py%s.h" // @doc - This file contains autoduck documentation zPy%s.hzF// This file declares the %s %s for Python. // Generated by makegw.py )ŚopenrZparse_interface_infoŚcloseŚnameZwin32com.makegw.makegwenumZmakegwZ makegwenumZ_write_enumifc_cppZ_write_enumgw_cppŚ_write_ifc_cppŚ _write_gw_cppŚwriteŚ _write_ifc_hŚ _write_gw_h) Zheader_file_nameZinterface_nameZbMakeInterfaceZ bMakeGatewayZfinŚ interfaceZdescZwin32comZifc_cpp_writerZ gw_cpp_writerZfout©rś7C:\Program Files\Certbot\pkgs\win32com\makegw\makegw.pyŚmake_framework_support6sX  ś’ ’’  ü’  rcCsT| d|j|j|j|jf”|jD]}| d|j”q$| d|j|jf”dS)Nzē// --------------------------------------------------- // // Interface Declaration class Py%s : public Py%s { public: MAKE_PYCOM_CTOR(Py%s); static %s *GetI(PyObject *self); static PyComTypeObject type; // The Python methods z6 static PyObject *%s(PyObject *self, PyObject *args); z1 protected: Py%s(IUnknown *pdisp); ~Py%s(); }; )r rŚbaseŚmethods)ŚfrŚmethodrrrr ‹sņ’ ’ ł’r c Cs |j}| d|j”t dd|j”}|j|dœ}|jD]h}|j|d<| d|”d}}}} } } } } d}|jD]„}z¼t |”}|  d”r|  ”}|r| d |  ”d ”||}|d |  ”}| |  ”} | | ”} |pü| ”}| | ”} | | ”} | ”\}}|r4| d |} |d |}Wq€tjy}z¦| d |j|jf”| d|j|j|jf”| d|j|j|jf”| d|j|j|jf} |d}|d|j}|d |j}| d|j|jf} WYd}~q€d}~00q€|r| d”| | ”| | ”| d||j|f”| rf| d”| | ”| d”|dd…|d<| |d<| |d<| d|”d}} }}}|jD]¾}|  d”sq®zXt |”}|  ”}|r||}|| ”}| | ”} |d | ”}||  ”}WnNtjyh}z2| d|j|j|f”WYd}~q®WYd}~n d}~00q®|rŽ| d||||| f”n | d ”| d!”q8| d"|”| d#|”|jD](}| d$|j|j|j|j|jf”qĘ|j}| d%tƒ”dS)&Na2// --------------------------------------------------- // // Interface Implementation Py%(name)s::Py%(name)s(IUnknown *pdisp): Py%(base)s(pdisp) { ob_type = &type; } Py%(name)s::~Py%(name)s() { } /* static */ %(name)s *Py%(name)s::GetI(PyObject *self) { return (%(name)s *)Py%(base)s::GetI(self); } z[a-z]Ś)Z interfacenameŚptrrzē// @pymethod |Py%(interfacename)s|%(method)s|Description of %(method)s. PyObject *Py%(interfacename)s::%(method)s(PyObject *self, PyObject *args) { %(interfacename)s *p%(ptr)s = GetI(self); if ( p%(ptr)s == NULL ) return NULL; rŚinś Ś ś, z %s; z‡// *** The input argument %s of type "%s" was not processed *** // Please check the conversion function is appropriate and exists! z %s %s; PyObject *ob%s; z, // @pyparm |%s||Description for %s zL if (bPythonIsHappy && !PyObject_As%s( ob%s, &%s )) bPythonIsHappy = FALSE; ŚOz, &ob%sz PyObject_Free%s(%s); ś USES_CONVERSION; z: if ( !PyArg_ParseTuple(args, "%s:%s"%s) ) return NULL; ś BOOL bPythonIsHappy = TRUE; z# if (!bPythonIsHappy) return NULL; rŚargsCOMŚcleanupŚ cleanup_gilzÜ HRESULT hr; PY_INTERFACE_PRECALL; hr = p%(ptr)s->%(method)s(%(argsCOM)s ); %(cleanup)s PY_INTERFACE_POSTCALL; %(cleanup_gil)s if ( FAILED(hr) ) return PyCom_BuildPyException(hr, p%(ptr)s, IID_%(interfacename)s ); ŚoutśK// *** The output argument %s of type "%s" was not processed *** // %s zE%s %s PyObject *pyretval = Py_BuildValue("%s"%s); %s return pyretval;z& Py_INCREF(Py_None); return Py_None; z } z-// @object Py%s|Description of the interface z-static struct PyMethodDef Py%s_methods[] = { z8 { "%s", Py%s::%s, 1 }, // @pymeth %s|Description of %s z¦ { NULL } }; PyComTypeObject Py%(name)s::type("Py%(name)s", &Py%(interfacebase)s::type, sizeof(Py%(name)s), Py%(name)s_methods, GET_PYCOM_CTOR(Py%(name)s)); )rr Ś__dict__ŚreŚsubrŚargsrŚmake_arg_converterŚ HasAttributeŚ GetFormatCharZGetAutoduckStringŚGetParseTupleArgŚ"DeclareParseArgTupleInputConverterŚGetParsePostCodeŚNeedUSES_CONVERSIONZGetInterfaceArgCleanupZGetInterfaceArgCleanupGILZGetInterfaceCppObjectInfoŚerror_not_supportedŚraw_typeŚtypeZGetBuildForInterfacePreCodeZGetBuildForInterfacePostCodeŚGetBuildValueArgrŚlocals)rrrrZstrdictrŚargsParseTuplerŚ formatCharsŚcodePostŚ codePobjectsZ codeCobjectsr r!ŚneedConversionŚargŚargCvtŚvalZ comArgNameZcomArgDeclStringŚwhyŚcodePreZ codeVarsPassZcodeDeclZ formatCharZ interfacebaserrrr ­sė’   ų’ü’żüüü   ’ ’       ’’’’’’’’,    ’’    ÷’       ’’( ’’   ’’ ö’r c Cs\|jddkr"d|jdd…}n d|j}|j}|jdksF|jdkrLd}n,|jddkrnd|jdd…}n d|j}| d|||||||||f ”|jdkrŗ| d |j|jf”n | d ”| d |”|jD]l}| d |j”|jr:|jdd …D]}| d| ””q|jd }| d| ””qŲ| d”qŲ| d”| ”dS)NrŚIŚPyGrŚIUnknownŚ IDispatchŚ PyGatewayBasezŚ// --------------------------------------------------- // // Gateway Declaration class %s : public %s, public %s { protected: %s(PyObject *instance) : %s(instance) { ; } PYGATEWAY_MAKE_SUPPORT2(%s, %s, IID_%s, %s) z2 // %s // *** Manually add %s method decls here z z // %s z STDMETHOD(%s)( é’’’’z %s, z %s); z void); z}; )rrr rr'ŚGetRawDeclarationr)rrŚgnamerŚ base_namerr9rrrr \s@   ō’  ’’     r c Cs’|jddkr"d|jdd…}n d|j}|j}|jdksF|jdkrLd}n,|jddkrnd|jdd…}n d|j}| d|||d œ”|jD]ö}| d ||jf”|jr|jdd …D]&}d  |j”}| d || ”f”qÄ|jd }d  |j”}| d|| ”f”n | d”| d”d}d} } } d} d} d}|jr¢|jD]F}| d”rŽ|d}|j dkrŽ| d|j”| d”rXznt   |”}|  ”|  ”}| pĀ| ”} |rī||}| | ”} | d| ”} | | ”} | | ”} Wn”t jyœ}zx| d|j|j|f”| d|j|j|jf”| d|j|jf”| d|j} |d}| d|j} WYd}~n d}~00qX| r²| d”| | ”| | ”|rÜ| d”d}nd }|röd!||| f}n|}| d"|j|f”| | ”|r‚| d#”| d$”d}}} }d} |jD]ŗ}| d”s`qLzbt   |”}|  ”|  ”}|rĄ||}|d| ”}|| ”}| | ”} | p¾| ”} Wn@t jy}z$| d%|j|j|f”WYd}~n d}~00qL|rNt|ƒdkr"d&}nd'}|r6| |”| d(||||jf”| rx| d)”| | ”| d*|j”| d+”| d,”q”dS)-Nrr>r?rr@rArBzT// --------------------------------------------------- // // Gateway Implementation )rrErFzSTDMETHODIMP %s::%s( rCz][z /* [%s] */ %s, z /* [%s] */ %s) z void) z{ PY_GATEWAY_METHOD; rr"éz! if (%s==NULL) return E_POINTER; rrz“// *** The input argument %s of type "%s" was not processed *** // - Please ensure this conversion function exists, and is appropriate // - %s z' PyObject *ob%s = PyObject_From%s(%s); z? if (ob%s==NULL) return MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s"); z Py_DECREF(ob%s); rz, ob%srz PyObject *result; z&resultZNULLz %s, "%s"%sz' HRESULT hr=InvokeViaPolicy("%s", %s); z if (FAILED(hr)) return hr; zD // Process the Python results, and convert back to the real params r#Z PyArg_ParseZPyArg_ParseTuplezK if (!%s(result, "%s" %s)) return MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s"); rzB if (!bPythonIsHappy) hr = MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s"); z Py_DECREF(result); z return hr; } )rrr rr'ŚjoinZinoutrDr)ZindirectionLevelrr(ZSetGatewayModer*r.r,r2ZGetBuildForGatewayPreCodeZGetBuildForGatewayPostCoder/r0r1r+r-Ślen)rrrErrFrr9ZinoutstrZcoutr=r6ZcodeVarsZargStrr8r5r:Z formatcharr<ZresStrZ fullArgStrr7r4r;ZparseFnrrrr s    ū’ ż’            ’  ’’’’ ’’(      ’   ’ ’  ’’  ’’  ’’ r cCstddƒdS)Nzd:\msdev\include\objidl.hZIStorage)rrrrrŚtest+srJ)rr) Ś__doc__r%rrrr r r r rJrrrrŚs0 ’ U"04