home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2690 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.2 KB  |  335 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import string
  6. import makegwparse
  7.  
  8. def make_framework_support(header_file_name, interface_name, bMakeInterface = 1, bMakeGateway = 1):
  9.     fin = open(header_file_name)
  10.     
  11.     try:
  12.         interface = makegwparse.parse_interface_info(interface_name, fin)
  13.     finally:
  14.         fin.close()
  15.  
  16.     if bMakeInterface and bMakeGateway:
  17.         desc = 'Interface and Gateway'
  18.     elif bMakeInterface and not bMakeGateway:
  19.         desc = 'Interface'
  20.     else:
  21.         desc = 'Gateway'
  22.     if interface.name[:5] == 'IEnum':
  23.         import win32com.makegw.makegwenum as win32com
  24.         ifc_cpp_writer = win32com.makegw.makegwenum._write_enumifc_cpp
  25.         gw_cpp_writer = win32com.makegw.makegwenum._write_enumgw_cpp
  26.     else:
  27.         ifc_cpp_writer = _write_ifc_cpp
  28.         gw_cpp_writer = _write_gw_cpp
  29.     fout = open('Py%s.cpp' % interface.name, 'w')
  30.     
  31.     try:
  32.         fout.write('// This file implements the %s %s for Python.\n// Generated by makegw.py\n\n#include "shell_pch.h"\n' % (interface.name, desc))
  33.         fout.write('#include "Py%s.h"\n\n// @doc - This file contains autoduck documentation\n' % interface.name)
  34.         if bMakeInterface:
  35.             ifc_cpp_writer(fout, interface)
  36.         
  37.         if bMakeGateway:
  38.             gw_cpp_writer(fout, interface)
  39.     finally:
  40.         fout.close()
  41.  
  42.     fout = open('Py%s.h' % interface.name, 'w')
  43.     
  44.     try:
  45.         fout.write('// This file declares the %s %s for Python.\n// Generated by makegw.py\n' % (interface.name, desc))
  46.         if bMakeInterface:
  47.             _write_ifc_h(fout, interface)
  48.         
  49.         if bMakeGateway:
  50.             _write_gw_h(fout, interface)
  51.     finally:
  52.         fout.close()
  53.  
  54.  
  55.  
  56. def _write_ifc_h(f, interface):
  57.     f.write('// ---------------------------------------------------\n//\n// Interface Declaration\n\nclass Py%s : public Py%s\n{\npublic:\n\tMAKE_PYCOM_CTOR(Py%s);\n\tstatic %s *GetI(PyObject *self);\n\tstatic PyComTypeObject type;\n\n\t// The Python methods\n' % (interface.name, interface.base, interface.name, interface.name))
  58.     for method in interface.methods:
  59.         f.write('\tstatic PyObject *%s(PyObject *self, PyObject *args);\n' % method.name)
  60.     
  61.     f.write('\nprotected:\n\tPy%s(IUnknown *pdisp);\n\t~Py%s();\n};\n' % (interface.name, interface.name))
  62.  
  63.  
  64. def _write_ifc_cpp(f, interface):
  65.     name = interface.name
  66.     f.write('// ---------------------------------------------------\n//\n// Interface Implementation\n\nPy%(name)s::Py%(name)s(IUnknown *pdisp):\n\tPy%(base)s(pdisp)\n{\n\tob_type = &type;\n}\n\nPy%(name)s::~Py%(name)s()\n{\n}\n\n/* static */ %(name)s *Py%(name)s::GetI(PyObject *self)\n{\n\treturn (%(name)s *)Py%(base)s::GetI(self);\n}\n\n' % interface.__dict__)
  67.     ptr = re.sub('[a-z]', '', interface.name)
  68.     strdict = {
  69.         'interfacename': interface.name,
  70.         'ptr': ptr }
  71.     for method in interface.methods:
  72.         strdict['method'] = method.name
  73.         f.write('// @pymethod |Py%(interfacename)s|%(method)s|Description of %(method)s.\nPyObject *Py%(interfacename)s::%(method)s(PyObject *self, PyObject *args)\n{\n\t%(interfacename)s *p%(ptr)s = GetI(self);\n\tif ( p%(ptr)s == NULL )\n\t\treturn NULL;\n' % strdict)
  74.         argsParseTuple = argsCOM = formatChars = codePost = codePobjects = codeCobjects = cleanup = cleanup_gil = ''
  75.         needConversion = 0
  76.         for arg in method.args:
  77.             
  78.             try:
  79.                 argCvt = makegwparse.make_arg_converter(arg)
  80.                 if arg.HasAttribute('in'):
  81.                     val = argCvt.GetFormatChar()
  82.                     if val:
  83.                         f.write('\t' + argCvt.GetAutoduckString() + '\n')
  84.                         formatChars = formatChars + val
  85.                         argsParseTuple = argsParseTuple + ', ' + argCvt.GetParseTupleArg()
  86.                         codePobjects = codePobjects + argCvt.DeclareParseArgTupleInputConverter()
  87.                         codePost = codePost + argCvt.GetParsePostCode()
  88.                         if not needConversion:
  89.                             pass
  90.                         needConversion = argCvt.NeedUSES_CONVERSION()
  91.                         cleanup = cleanup + argCvt.GetInterfaceArgCleanup()
  92.                         cleanup_gil = cleanup_gil + argCvt.GetInterfaceArgCleanupGIL()
  93.                     
  94.                 
  95.                 (comArgName, comArgDeclString) = argCvt.GetInterfaceCppObjectInfo()
  96.                 if comArgDeclString:
  97.                     codeCobjects = codeCobjects + '\t%s;\n' % comArgDeclString
  98.                 
  99.                 argsCOM = argsCOM + ', ' + comArgName
  100.             continue
  101.             except makegwparse.error_not_supported:
  102.                 why = None
  103.                 f.write('// *** The input argument %s of type "%s" was not processed ***\n//     Please check the conversion function is appropriate and exists!\n' % (arg.name, arg.raw_type))
  104.                 f.write('\t%s %s;\n\tPyObject *ob%s;\n' % (arg.type, arg.name, arg.name))
  105.                 f.write('\t// @pyparm <o Py%s>|%s||Description for %s\n' % (arg.type, arg.name, arg.name))
  106.                 codePost = codePost + '\tif (bPythonIsHappy && !PyObject_As%s( ob%s, &%s )) bPythonIsHappy = FALSE;\n' % (arg.type, arg.name, arg.name)
  107.                 formatChars = formatChars + 'O'
  108.                 argsParseTuple = argsParseTuple + ', &ob%s' % arg.name
  109.                 argsCOM = argsCOM + ', ' + arg.name
  110.                 cleanup = cleanup + '\tPyObject_Free%s(%s);\n' % (arg.type, arg.name)
  111.                 continue
  112.             
  113.  
  114.         
  115.         if needConversion:
  116.             f.write('\tUSES_CONVERSION;\n')
  117.         
  118.         f.write(codePobjects)
  119.         f.write(codeCobjects)
  120.         f.write('\tif ( !PyArg_ParseTuple(args, "%s:%s"%s) )\n\t\treturn NULL;\n' % (formatChars, method.name, argsParseTuple))
  121.         if codePost:
  122.             f.write('\tBOOL bPythonIsHappy = TRUE;\n')
  123.             f.write(codePost)
  124.             f.write('\tif (!bPythonIsHappy) return NULL;\n')
  125.         
  126.         strdict['argsCOM'] = argsCOM[1:]
  127.         strdict['cleanup'] = cleanup
  128.         strdict['cleanup_gil'] = cleanup_gil
  129.         f.write('\tHRESULT hr;\n\tPY_INTERFACE_PRECALL;\n\thr = p%(ptr)s->%(method)s(%(argsCOM)s );\n%(cleanup)s\n\tPY_INTERFACE_POSTCALL;\n%(cleanup_gil)s\n\tif ( FAILED(hr) )\n\t\treturn PyCom_BuildPyException(hr, p%(ptr)s, IID_%(interfacename)s );\n' % strdict)
  130.         codePre = codePost = formatChars = codeVarsPass = codeDecl = ''
  131.         for arg in method.args:
  132.             if not arg.HasAttribute('out'):
  133.                 continue
  134.             
  135.             
  136.             try:
  137.                 argCvt = makegwparse.make_arg_converter(arg)
  138.                 formatChar = argCvt.GetFormatChar()
  139.                 if formatChar:
  140.                     formatChars = formatChars + formatChar
  141.                     codePre = codePre + argCvt.GetBuildForInterfacePreCode()
  142.                     codePost = codePost + argCvt.GetBuildForInterfacePostCode()
  143.                     codeVarsPass = codeVarsPass + ', ' + argCvt.GetBuildValueArg()
  144.                     codeDecl = codeDecl + argCvt.DeclareParseArgTupleInputConverter()
  145.             continue
  146.             except makegwparse.error_not_supported:
  147.                 why = None
  148.                 f.write('// *** The output argument %s of type "%s" was not processed ***\n//     %s\n' % (arg.name, arg.raw_type, why))
  149.                 continue
  150.                 continue
  151.             
  152.  
  153.         
  154.         if formatChars:
  155.             f.write('%s\n%s\tPyObject *pyretval = Py_BuildValue("%s"%s);\n%s\treturn pyretval;' % (codeDecl, codePre, formatChars, codeVarsPass, codePost))
  156.         else:
  157.             f.write('\tPy_INCREF(Py_None);\n\treturn Py_None;\n')
  158.         f.write('\n}\n\n')
  159.     
  160.     f.write('// @object Py%s|Description of the interface\n' % name)
  161.     f.write('static struct PyMethodDef Py%s_methods[] =\n{\n' % name)
  162.     for method in interface.methods:
  163.         f.write('\t{ "%s", Py%s::%s, 1 }, // @pymeth %s|Description of %s\n' % (method.name, interface.name, method.name, method.name, method.name))
  164.     
  165.     interfacebase = interface.base
  166.     f.write('\t{ NULL }\n};\n\nPyComTypeObject Py%(name)s::type("Py%(name)s",\n\t\t&Py%(interfacebase)s::type,\n\t\tsizeof(Py%(name)s),\n\t\tPy%(name)s_methods,\n\t\tGET_PYCOM_CTOR(Py%(name)s));\n' % locals())
  167.  
  168.  
  169. def _write_gw_h(f, interface):
  170.     if interface.name[0] == 'I':
  171.         gname = 'PyG' + interface.name[1:]
  172.     else:
  173.         gname = 'PyG' + interface.name
  174.     name = interface.name
  175.     if interface.base == 'IUnknown' or interface.base == 'IDispatch':
  176.         base_name = 'PyGatewayBase'
  177.     elif interface.base[0] == 'I':
  178.         base_name = 'PyG' + interface.base[1:]
  179.     else:
  180.         base_name = 'PyG' + interface.base
  181.     f.write('// ---------------------------------------------------\n//\n// Gateway Declaration\n\nclass %s : public %s, public %s\n{\nprotected:\n\t%s(PyObject *instance) : %s(instance) { ; }\n\tPYGATEWAY_MAKE_SUPPORT2(%s, %s, IID_%s, %s)\n\n' % (gname, base_name, name, gname, base_name, gname, name, name, base_name))
  182.     if interface.base != 'IUnknown':
  183.         f.write('\t// %s\n\t// *** Manually add %s method decls here\n\n' % (interface.base, interface.base))
  184.     else:
  185.         f.write('\n\n')
  186.     f.write('\t// %s\n' % name)
  187.     for method in interface.methods:
  188.         f.write('\tSTDMETHOD(%s)(\n' % method.name)
  189.         if method.args:
  190.             for arg in method.args[:-1]:
  191.                 f.write('\t\t%s,\n' % arg.GetRawDeclaration())
  192.             
  193.             arg = method.args[-1]
  194.             f.write('\t\t%s);\n\n' % arg.GetRawDeclaration())
  195.             continue
  196.         f.write('\t\tvoid);\n\n')
  197.     
  198.     f.write('};\n')
  199.     f.close()
  200.  
  201.  
  202. def _write_gw_cpp(f, interface):
  203.     if interface.name[0] == 'I':
  204.         gname = 'PyG' + interface.name[1:]
  205.     else:
  206.         gname = 'PyG' + interface.name
  207.     name = interface.name
  208.     if interface.base == 'IUnknown' or interface.base == 'IDispatch':
  209.         base_name = 'PyGatewayBase'
  210.     elif interface.base[0] == 'I':
  211.         base_name = 'PyG' + interface.base[1:]
  212.     else:
  213.         base_name = 'PyG' + interface.base
  214.     f.write('// ---------------------------------------------------\n//\n// Gateway Implementation\n' % {
  215.         'name': name,
  216.         'gname': gname,
  217.         'base_name': base_name })
  218.     for method in interface.methods:
  219.         f.write('STDMETHODIMP %s::%s(\n' % (gname, method.name))
  220.         if method.args:
  221.             for arg in method.args[:-1]:
  222.                 inoutstr = string.join(arg.inout, '][')
  223.                 f.write('\t\t/* [%s] */ %s,\n' % (inoutstr, arg.GetRawDeclaration()))
  224.             
  225.             arg = method.args[-1]
  226.             inoutstr = string.join(arg.inout, '][')
  227.             f.write('\t\t/* [%s] */ %s)\n' % (inoutstr, arg.GetRawDeclaration()))
  228.         else:
  229.             f.write('\t\tvoid)\n')
  230.         f.write('{\n\tPY_GATEWAY_METHOD;\n')
  231.         cout = 0
  232.         codePre = codePost = codeVars = ''
  233.         argStr = ''
  234.         needConversion = 0
  235.         formatChars = ''
  236.         if method.args:
  237.             for arg in method.args:
  238.                 if arg.HasAttribute('out'):
  239.                     cout = cout + 1
  240.                     if arg.indirectionLevel == 2:
  241.                         f.write('\tif (%s==NULL) return E_POINTER;\n' % arg.name)
  242.                     
  243.                 
  244.                 if arg.HasAttribute('in'):
  245.                     
  246.                     try:
  247.                         argCvt = makegwparse.make_arg_converter(arg)
  248.                         argCvt.SetGatewayMode()
  249.                         formatchar = argCvt.GetFormatChar()
  250.                         if not needConversion:
  251.                             pass
  252.                         needConversion = argCvt.NeedUSES_CONVERSION()
  253.                         if formatchar:
  254.                             formatChars = formatChars + formatchar
  255.                             codeVars = codeVars + argCvt.DeclareParseArgTupleInputConverter()
  256.                             argStr = argStr + ', ' + argCvt.GetBuildValueArg()
  257.                         
  258.                         codePre = codePre + argCvt.GetBuildForGatewayPreCode()
  259.                         codePost = codePost + argCvt.GetBuildForGatewayPostCode()
  260.                     except makegwparse.error_not_supported:
  261.                         why = None
  262.                         f.write('// *** The input argument %s of type "%s" was not processed ***\n//   - Please ensure this conversion function exists, and is appropriate\n//   - %s\n' % (arg.name, arg.raw_type, why))
  263.                         f.write('\tPyObject *ob%s = PyObject_From%s(%s);\n' % (arg.name, arg.type, arg.name))
  264.                         f.write('\tif (ob%s==NULL) return MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n' % (arg.name, method.name))
  265.                         codePost = codePost + '\tPy_DECREF(ob%s);\n' % arg.name
  266.                         formatChars = formatChars + 'O'
  267.                         argStr = argStr + ', ob%s' % arg.name
  268.                     except:
  269.                         None<EXCEPTION MATCH>makegwparse.error_not_supported
  270.                     
  271.  
  272.                 None<EXCEPTION MATCH>makegwparse.error_not_supported
  273.             
  274.         
  275.         if needConversion:
  276.             f.write('\tUSES_CONVERSION;\n')
  277.         
  278.         f.write(codeVars)
  279.         f.write(codePre)
  280.         if cout:
  281.             f.write('\tPyObject *result;\n')
  282.             resStr = '&result'
  283.         else:
  284.             resStr = 'NULL'
  285.         if formatChars:
  286.             fullArgStr = '%s, "%s"%s' % (resStr, formatChars, argStr)
  287.         else:
  288.             fullArgStr = resStr
  289.         f.write('\tHRESULT hr=InvokeViaPolicy("%s", %s);\n' % (method.name, fullArgStr))
  290.         f.write(codePost)
  291.         if cout:
  292.             f.write('\tif (FAILED(hr)) return hr;\n')
  293.             f.write('\t// Process the Python results, and convert back to the real params\n')
  294.             formatChars = codePobjects = codePost = argsParseTuple = ''
  295.             needConversion = 0
  296.             for arg in method.args:
  297.                 if not arg.HasAttribute('out'):
  298.                     continue
  299.                 
  300.                 
  301.                 try:
  302.                     argCvt = makegwparse.make_arg_converter(arg)
  303.                     argCvt.SetGatewayMode()
  304.                     val = argCvt.GetFormatChar()
  305.                     if val:
  306.                         formatChars = formatChars + val
  307.                         argsParseTuple = argsParseTuple + ', ' + argCvt.GetParseTupleArg()
  308.                         codePobjects = codePobjects + argCvt.DeclareParseArgTupleInputConverter()
  309.                         codePost = codePost + argCvt.GetParsePostCode()
  310.                         if not needConversion:
  311.                             pass
  312.                         needConversion = argCvt.NeedUSES_CONVERSION()
  313.                 continue
  314.                 except makegwparse.error_not_supported:
  315.                     why = None
  316.                     f.write('// *** The output argument %s of type "%s" was not processed ***\n//     %s\n' % (arg.name, arg.raw_type, why))
  317.                     continue
  318.                 
  319.  
  320.             
  321.             if codePost:
  322.                 f.write('\tBOOL bPythonIsHappy = TRUE;\n')
  323.                 f.write(codePost)
  324.                 f.write('\tif (!bPythonIsHappy) hr = MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n' % method.name)
  325.             
  326.             f.write('\tPy_DECREF(result);\n')
  327.         
  328.         f.write('\treturn hr;\n}\n\n')
  329.     
  330.  
  331.  
  332. def test():
  333.     make_framework_support('d:\\msdev\\include\\objidl.h', 'IStorage')
  334.  
  335.