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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import traceback
  6. import string
  7.  
  8. class error_not_found(Exception):
  9.     
  10.     def __init__(self, msg = 'The requested item could not be found'):
  11.         super(error_not_found, self).__init__(msg)
  12.  
  13.  
  14.  
  15. class error_not_supported(Exception):
  16.     
  17.     def __init__(self, msg = 'The required functionality is not supported'):
  18.         super(error_not_supported, self).__init__(msg)
  19.  
  20.  
  21. VERBOSE = 0
  22. DEBUG = 0
  23.  
  24. class ArgFormatter:
  25.     
  26.     def __init__(self, arg, builtinIndirection, declaredIndirection = 0):
  27.         self.arg = arg
  28.         self.builtinIndirection = builtinIndirection
  29.         self.declaredIndirection = declaredIndirection
  30.         self.gatewayMode = 0
  31.  
  32.     
  33.     def _IndirectPrefix(self, indirectionFrom, indirectionTo):
  34.         dif = indirectionFrom - indirectionTo
  35.         if dif == 0:
  36.             return ''
  37.         if dif == -1:
  38.             return '&'
  39.         if dif == 1:
  40.             return '*'
  41.         return '?? (%d)' % (dif,)
  42.         raise error_not_supported, "Can't indirect this far - please fix me :-)"
  43.  
  44.     
  45.     def GetIndirectedArgName(self, indirectFrom, indirectionTo):
  46.         if indirectFrom is None:
  47.             indirectFrom = self._GetDeclaredIndirection() + self.builtinIndirection
  48.         
  49.         return self._IndirectPrefix(indirectFrom, indirectionTo) + self.arg.name
  50.  
  51.     
  52.     def GetBuildValueArg(self):
  53.         return self.arg.name
  54.  
  55.     
  56.     def GetParseTupleArg(self):
  57.         if self.gatewayMode:
  58.             return self.GetIndirectedArgName(None, 1)
  59.         return self.GetIndirectedArgName(self.builtinIndirection, 1)
  60.  
  61.     
  62.     def GetInterfaceCppObjectInfo(self):
  63.         return (self.GetIndirectedArgName(self.builtinIndirection, self.arg.indirectionLevel + self.builtinIndirection), '%s %s' % (self.GetUnconstType(), self.arg.name))
  64.  
  65.     
  66.     def GetInterfaceArgCleanup(self):
  67.         if DEBUG:
  68.             return '/* GetInterfaceArgCleanup output goes here: %s */\n' % self.arg.name
  69.         return ''
  70.  
  71.     
  72.     def GetInterfaceArgCleanupGIL(self):
  73.         if DEBUG:
  74.             return '/* GetInterfaceArgCleanup (GIL held) output goes here: %s */\n' % self.arg.name
  75.         return ''
  76.  
  77.     
  78.     def GetUnconstType(self):
  79.         return self.arg.unc_type
  80.  
  81.     
  82.     def SetGatewayMode(self):
  83.         self.gatewayMode = 1
  84.  
  85.     
  86.     def _GetDeclaredIndirection(self):
  87.         return self.arg.indirectionLevel
  88.         print 'declared:', self.arg.name, self.gatewayMode
  89.         if self.gatewayMode:
  90.             return self.arg.indirectionLevel
  91.         return self.declaredIndirection
  92.  
  93.     
  94.     def DeclareParseArgTupleInputConverter(self):
  95.         if DEBUG:
  96.             return '/* Declare ParseArgTupleInputConverter goes here: %s */\n' % self.arg.name
  97.         return ''
  98.  
  99.     
  100.     def GetParsePostCode(self):
  101.         if DEBUG:
  102.             return '/* GetParsePostCode code goes here: %s */\n' % self.arg.name
  103.         return ''
  104.  
  105.     
  106.     def GetBuildForInterfacePreCode(self):
  107.         if DEBUG:
  108.             return '/* GetBuildForInterfacePreCode goes here: %s */\n' % self.arg.name
  109.         return ''
  110.  
  111.     
  112.     def GetBuildForGatewayPreCode(self):
  113.         s = self.GetBuildForInterfacePreCode()
  114.         if DEBUG:
  115.             if s[:4] == '/* G':
  116.                 s = '/* GetBuildForGatewayPreCode goes here: %s */\n' % self.arg.name
  117.             
  118.         
  119.         return s
  120.  
  121.     
  122.     def GetBuildForInterfacePostCode(self):
  123.         if DEBUG:
  124.             return '/* GetBuildForInterfacePostCode goes here: %s */\n' % self.arg.name
  125.         return ''
  126.  
  127.     
  128.     def GetBuildForGatewayPostCode(self):
  129.         s = self.GetBuildForInterfacePostCode()
  130.         if DEBUG:
  131.             if s[:4] == '/* G':
  132.                 s = '/* GetBuildForGatewayPostCode goes here: %s */\n' % self.arg.name
  133.             
  134.         
  135.         return s
  136.  
  137.     
  138.     def GetAutoduckString(self):
  139.         return '// @pyparm %s|%s||Description for %s' % (self._GetPythonTypeDesc(), self.arg.name, self.arg.name)
  140.  
  141.     
  142.     def _GetPythonTypeDesc(self):
  143.         pass
  144.  
  145.     
  146.     def NeedUSES_CONVERSION(self):
  147.         return 0
  148.  
  149.  
  150.  
  151. class ArgFormatterFloat(ArgFormatter):
  152.     
  153.     def GetFormatChar(self):
  154.         return 'f'
  155.  
  156.     
  157.     def DeclareParseArgTupleInputConverter(self):
  158.         return '\tdouble dbl%s;\n' % self.arg.name
  159.  
  160.     
  161.     def GetParseTupleArg(self):
  162.         return '&dbl' + self.arg.name
  163.  
  164.     
  165.     def _GetPythonTypeDesc(self):
  166.         return 'float'
  167.  
  168.     
  169.     def GetBuildValueArg(self):
  170.         return '&dbl' + self.arg.name
  171.  
  172.     
  173.     def GetBuildForInterfacePreCode(self):
  174.         return '\tdbl' + self.arg.name + ' = ' + self.arg.name + ';\n'
  175.  
  176.     
  177.     def GetBuildForGatewayPreCode(self):
  178.         return '\tdbl%s = ' % self.arg.name + self._IndirectPrefix(self._GetDeclaredIndirection(), 0) + self.arg.name + ';\n'
  179.  
  180.     
  181.     def GetParsePostCode(self):
  182.         s = '\t'
  183.         if self.gatewayMode:
  184.             s = s + self._IndirectPrefix(self._GetDeclaredIndirection(), 0)
  185.         
  186.         s = s + self.arg.name
  187.         s = s + ' = (float)dbl%s;\n' % self.arg.name
  188.         return s
  189.  
  190.  
  191.  
  192. class ArgFormatterShort(ArgFormatter):
  193.     
  194.     def GetFormatChar(self):
  195.         return 'i'
  196.  
  197.     
  198.     def DeclareParseArgTupleInputConverter(self):
  199.         return '\tINT i%s;\n' % self.arg.name
  200.  
  201.     
  202.     def GetParseTupleArg(self):
  203.         return '&i' + self.arg.name
  204.  
  205.     
  206.     def _GetPythonTypeDesc(self):
  207.         return 'int'
  208.  
  209.     
  210.     def GetBuildValueArg(self):
  211.         return '&i' + self.arg.name
  212.  
  213.     
  214.     def GetBuildForInterfacePreCode(self):
  215.         return '\ti' + self.arg.name + ' = ' + self.arg.name + ';\n'
  216.  
  217.     
  218.     def GetBuildForGatewayPreCode(self):
  219.         return '\ti%s = ' % self.arg.name + self._IndirectPrefix(self._GetDeclaredIndirection(), 0) + self.arg.name + ';\n'
  220.  
  221.     
  222.     def GetParsePostCode(self):
  223.         s = '\t'
  224.         if self.gatewayMode:
  225.             s = s + self._IndirectPrefix(self._GetDeclaredIndirection(), 0)
  226.         
  227.         s = s + self.arg.name
  228.         s = s + ' = i%s;\n' % self.arg.name
  229.         return s
  230.  
  231.  
  232.  
  233. class ArgFormatterLONG_PTR(ArgFormatter):
  234.     
  235.     def GetFormatChar(self):
  236.         return 'O'
  237.  
  238.     
  239.     def DeclareParseArgTupleInputConverter(self):
  240.         return '\tPyObject *ob%s;\n' % self.arg.name
  241.  
  242.     
  243.     def GetParseTupleArg(self):
  244.         return '&ob' + self.arg.name
  245.  
  246.     
  247.     def _GetPythonTypeDesc(self):
  248.         return 'int/long'
  249.  
  250.     
  251.     def GetBuildValueArg(self):
  252.         return 'ob' + self.arg.name
  253.  
  254.     
  255.     def GetBuildForInterfacePostCode(self):
  256.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  257.  
  258.     
  259.     def DeclareParseArgTupleInputConverter(self):
  260.         return '\tPyObject *ob%s;\n' % self.arg.name
  261.  
  262.     
  263.     def GetParsePostCode(self):
  264.         return '\tif (bPythonIsHappy && !PyWinLong_AsULONG_PTR(ob%s, (ULONG_PTR *)%s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 2))
  265.  
  266.     
  267.     def GetBuildForInterfacePreCode(self):
  268.         notdirected = self.GetIndirectedArgName(None, 1)
  269.         return '\tob%s = PyWinObject_FromULONG_PTR(%s);\n' % (self.arg.name, notdirected)
  270.  
  271.     
  272.     def GetBuildForGatewayPostCode(self):
  273.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  274.  
  275.  
  276.  
  277. class ArgFormatterPythonCOM(ArgFormatter):
  278.     
  279.     def GetFormatChar(self):
  280.         return 'O'
  281.  
  282.     
  283.     def DeclareParseArgTupleInputConverter(self):
  284.         return '\tPyObject *ob%s;\n' % self.arg.name
  285.  
  286.     
  287.     def GetParseTupleArg(self):
  288.         return '&ob' + self.arg.name
  289.  
  290.     
  291.     def _GetPythonTypeDesc(self):
  292.         return '<o Py%s>' % self.arg.type
  293.  
  294.     
  295.     def GetBuildValueArg(self):
  296.         return 'ob' + self.arg.name
  297.  
  298.     
  299.     def GetBuildForInterfacePostCode(self):
  300.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  301.  
  302.     
  303.     def DeclareParseArgTupleInputConverter(self):
  304.         return '\tPyObject *ob%s;\n' % self.arg.name
  305.  
  306.  
  307.  
  308. class ArgFormatterBSTR(ArgFormatterPythonCOM):
  309.     
  310.     def _GetPythonTypeDesc(self):
  311.         return '<o unicode>'
  312.  
  313.     
  314.     def GetParsePostCode(self):
  315.         return '\tif (bPythonIsHappy && !PyWinObject_AsBstr(ob%s, %s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 2))
  316.  
  317.     
  318.     def GetBuildForInterfacePreCode(self):
  319.         notdirected = self.GetIndirectedArgName(None, 1)
  320.         return '\tob%s = MakeBstrToObj(%s);\n' % (self.arg.name, notdirected)
  321.  
  322.     
  323.     def GetBuildForInterfacePostCode(self):
  324.         return '\tSysFreeString(%s);\n' % (self.arg.name,) + ArgFormatterPythonCOM.GetBuildForInterfacePostCode(self)
  325.  
  326.     
  327.     def GetBuildForGatewayPostCode(self):
  328.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  329.  
  330.  
  331.  
  332. class ArgFormatterOLECHAR(ArgFormatterPythonCOM):
  333.     
  334.     def _GetPythonTypeDesc(self):
  335.         return '<o unicode>'
  336.  
  337.     
  338.     def GetUnconstType(self):
  339.         if self.arg.type[:3] == 'LPC':
  340.             return self.arg.type[:2] + self.arg.type[3:]
  341.         return self.arg.unc_type
  342.  
  343.     
  344.     def GetParsePostCode(self):
  345.         return '\tif (bPythonIsHappy && !PyWinObject_AsBstr(ob%s, %s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 2))
  346.  
  347.     
  348.     def GetInterfaceArgCleanup(self):
  349.         return '\tSysFreeString(%s);\n' % self.GetIndirectedArgName(None, 1)
  350.  
  351.     
  352.     def GetBuildForInterfacePreCode(self):
  353.         notdirected = self.GetIndirectedArgName(self.builtinIndirection, 1)
  354.         return '\tob%s = MakeOLECHARToObj(%s);\n' % (self.arg.name, notdirected)
  355.  
  356.     
  357.     def GetBuildForInterfacePostCode(self):
  358.         return '\tCoTaskMemFree(%s);\n' % (self.arg.name,) + ArgFormatterPythonCOM.GetBuildForInterfacePostCode(self)
  359.  
  360.     
  361.     def GetBuildForGatewayPostCode(self):
  362.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  363.  
  364.  
  365.  
  366. class ArgFormatterTCHAR(ArgFormatterPythonCOM):
  367.     
  368.     def _GetPythonTypeDesc(self):
  369.         return 'string/<o unicode>'
  370.  
  371.     
  372.     def GetUnconstType(self):
  373.         if self.arg.type[:3] == 'LPC':
  374.             return self.arg.type[:2] + self.arg.type[3:]
  375.         return self.arg.unc_type
  376.  
  377.     
  378.     def GetParsePostCode(self):
  379.         return '\tif (bPythonIsHappy && !PyWinObject_AsTCHAR(ob%s, %s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 2))
  380.  
  381.     
  382.     def GetInterfaceArgCleanup(self):
  383.         return '\tPyWinObject_FreeTCHAR(%s);\n' % self.GetIndirectedArgName(None, 1)
  384.  
  385.     
  386.     def GetBuildForInterfacePreCode(self):
  387.         notdirected = self.GetIndirectedArgName(self.builtinIndirection, 1)
  388.         return '\tob%s = PyWinObject_FromTCHAR(%s);\n' % (self.arg.name, notdirected)
  389.  
  390.     
  391.     def GetBuildForInterfacePostCode(self):
  392.         return '// ??? - TCHAR post code\n'
  393.  
  394.     
  395.     def GetBuildForGatewayPostCode(self):
  396.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  397.  
  398.  
  399.  
  400. class ArgFormatterIID(ArgFormatterPythonCOM):
  401.     
  402.     def _GetPythonTypeDesc(self):
  403.         return '<o PyIID>'
  404.  
  405.     
  406.     def GetParsePostCode(self):
  407.         return '\tif (!PyWinObject_AsIID(ob%s, &%s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.arg.name)
  408.  
  409.     
  410.     def GetBuildForInterfacePreCode(self):
  411.         notdirected = self.GetIndirectedArgName(None, 0)
  412.         return '\tob%s = PyWinObject_FromIID(%s);\n' % (self.arg.name, notdirected)
  413.  
  414.     
  415.     def GetInterfaceCppObjectInfo(self):
  416.         return (self.arg.name, 'IID %s' % self.arg.name)
  417.  
  418.  
  419.  
  420. class ArgFormatterTime(ArgFormatterPythonCOM):
  421.     
  422.     def __init__(self, arg, builtinIndirection, declaredIndirection = 0):
  423.         if arg.indirectionLevel == 0 and arg.unc_type[:2] == 'LP':
  424.             arg.unc_type = arg.unc_type[2:]
  425.             arg.indirectionLevel = arg.indirectionLevel + 1
  426.             builtinIndirection = 0
  427.         
  428.         ArgFormatterPythonCOM.__init__(self, arg, builtinIndirection, declaredIndirection)
  429.  
  430.     
  431.     def _GetPythonTypeDesc(self):
  432.         return '<o PyTime>'
  433.  
  434.     
  435.     def GetParsePostCode(self):
  436.         return '\tif (!PyTime_Check(ob%s)) {\n\t\tPyErr_SetString(PyExc_TypeError, "The argument must be a PyTime object");\n\t\tbPythonIsHappy = FALSE;\n\t}\n\tif (!((PyTime *)ob%s)->GetTime(%s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.arg.name, self.GetIndirectedArgName(self.builtinIndirection, 1))
  437.  
  438.     
  439.     def GetBuildForInterfacePreCode(self):
  440.         notdirected = self.GetIndirectedArgName(self.builtinIndirection, 0)
  441.         return '\tob%s = new PyTime(%s);\n' % (self.arg.name, notdirected)
  442.  
  443.     
  444.     def GetBuildForInterfacePostCode(self):
  445.         ret = ''
  446.         if self.builtinIndirection + self.arg.indirectionLevel > 1:
  447.             ret = '\tCoTaskMemFree(%s);\n' % self.arg.name
  448.         
  449.         return ret + ArgFormatterPythonCOM.GetBuildForInterfacePostCode(self)
  450.  
  451.  
  452.  
  453. class ArgFormatterSTATSTG(ArgFormatterPythonCOM):
  454.     
  455.     def _GetPythonTypeDesc(self):
  456.         return '<o STATSTG>'
  457.  
  458.     
  459.     def GetParsePostCode(self):
  460.         return '\tif (!PyCom_PyObjectAsSTATSTG(ob%s, %s, 0/*flags*/)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  461.  
  462.     
  463.     def GetBuildForInterfacePreCode(self):
  464.         notdirected = self.GetIndirectedArgName(None, 1)
  465.         return '\tob%s = PyCom_PyObjectFromSTATSTG(%s);\n\t// STATSTG doco says our responsibility to free\n\tif ((%s).pwcsName) CoTaskMemFree((%s).pwcsName);\n' % (self.arg.name, self.GetIndirectedArgName(None, 1), notdirected, notdirected)
  466.  
  467.  
  468.  
  469. class ArgFormatterGeneric(ArgFormatterPythonCOM):
  470.     
  471.     def _GetPythonTypeDesc(self):
  472.         return '<o %s>' % self.arg.type
  473.  
  474.     
  475.     def GetParsePostCode(self):
  476.         return '\tif (!PyObject_As%s(ob%s, &%s) bPythonIsHappy = FALSE;\n' % (self.arg.type, self.arg.name, self.GetIndirectedArgName(None, 1))
  477.  
  478.     
  479.     def GetInterfaceArgCleanup(self):
  480.         return '\tPyObject_Free%s(%s);\n' % (self.arg.type, self.arg.name)
  481.  
  482.     
  483.     def GetBuildForInterfacePreCode(self):
  484.         notdirected = self.GetIndirectedArgName(None, 1)
  485.         return '\tob%s = PyObject_From%s(%s);\n' % (self.arg.name, self.arg.type, self.GetIndirectedArgName(None, 1))
  486.  
  487.  
  488.  
  489. class ArgFormatterIDLIST(ArgFormatterPythonCOM):
  490.     
  491.     def _GetPythonTypeDesc(self):
  492.         return '<o PyIDL>'
  493.  
  494.     
  495.     def GetParsePostCode(self):
  496.         return '\tif (bPythonIsHappy && !PyObject_AsPIDL(ob%s, &%s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  497.  
  498.     
  499.     def GetInterfaceArgCleanup(self):
  500.         return '\tPyObject_FreePIDL(%s);\n' % (self.arg.name,)
  501.  
  502.     
  503.     def GetBuildForInterfacePreCode(self):
  504.         notdirected = self.GetIndirectedArgName(None, 1)
  505.         return '\tob%s = PyObject_FromPIDL(%s);\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  506.  
  507.  
  508.  
  509. class ArgFormatterHANDLE(ArgFormatterPythonCOM):
  510.     
  511.     def _GetPythonTypeDesc(self):
  512.         return '<o PyHANDLE>'
  513.  
  514.     
  515.     def GetParsePostCode(self):
  516.         return '\tif (!PyWinObject_AsHANDLE(ob%s, &%s, FALSE) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  517.  
  518.     
  519.     def GetBuildForInterfacePreCode(self):
  520.         notdirected = self.GetIndirectedArgName(None, 1)
  521.         return '\tob%s = PyWinObject_FromHANDLE(%s);\n' % (self.arg.name, self.GetIndirectedArgName(None, 0))
  522.  
  523.  
  524.  
  525. class ArgFormatterLARGE_INTEGER(ArgFormatterPythonCOM):
  526.     
  527.     def GetKeyName(self):
  528.         return 'LARGE_INTEGER'
  529.  
  530.     
  531.     def _GetPythonTypeDesc(self):
  532.         return '<o %s>' % self.GetKeyName()
  533.  
  534.     
  535.     def GetParsePostCode(self):
  536.         return '\tif (!PyWinObject_As%s(ob%s, %s)) bPythonIsHappy = FALSE;\n' % (self.GetKeyName(), self.arg.name, self.GetIndirectedArgName(None, 1))
  537.  
  538.     
  539.     def GetBuildForInterfacePreCode(self):
  540.         notdirected = self.GetIndirectedArgName(None, 0)
  541.         return '\tob%s = PyWinObject_From%s(%s);\n' % (self.arg.name, self.GetKeyName(), notdirected)
  542.  
  543.  
  544.  
  545. class ArgFormatterULARGE_INTEGER(ArgFormatterLARGE_INTEGER):
  546.     
  547.     def GetKeyName(self):
  548.         return 'ULARGE_INTEGER'
  549.  
  550.  
  551.  
  552. class ArgFormatterInterface(ArgFormatterPythonCOM):
  553.     
  554.     def GetInterfaceCppObjectInfo(self):
  555.         return (self.GetIndirectedArgName(1, self.arg.indirectionLevel), '%s * %s' % (self.GetUnconstType(), self.arg.name))
  556.  
  557.     
  558.     def GetParsePostCode(self):
  559.         if self.gatewayMode:
  560.             sArg = self.GetIndirectedArgName(None, 2)
  561.         else:
  562.             sArg = self.GetIndirectedArgName(1, 2)
  563.         return '\tif (bPythonIsHappy && !PyCom_InterfaceFromPyInstanceOrObject(ob%s, IID_%s, (void **)%s, TRUE /* bNoneOK */))\n\t\t bPythonIsHappy = FALSE;\n' % (self.arg.name, self.arg.type, sArg)
  564.  
  565.     
  566.     def GetBuildForInterfacePreCode(self):
  567.         return '\tob%s = PyCom_PyObjectFromIUnknown(%s, IID_%s, FALSE);\n' % (self.arg.name, self.arg.name, self.arg.type)
  568.  
  569.     
  570.     def GetBuildForGatewayPreCode(self):
  571.         sPrefix = self._IndirectPrefix(self._GetDeclaredIndirection(), 1)
  572.         return '\tob%s = PyCom_PyObjectFromIUnknown(%s%s, IID_%s, TRUE);\n' % (self.arg.name, sPrefix, self.arg.name, self.arg.type)
  573.  
  574.     
  575.     def GetInterfaceArgCleanup(self):
  576.         return '\tif (%s) %s->Release();\n' % (self.arg.name, self.arg.name)
  577.  
  578.  
  579.  
  580. class ArgFormatterVARIANT(ArgFormatterPythonCOM):
  581.     
  582.     def GetParsePostCode(self):
  583.         return '\tif ( !PyCom_VariantFromPyObject(ob%s, %s) )\n\t\tbPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  584.  
  585.     
  586.     def GetBuildForGatewayPreCode(self):
  587.         notdirected = self.GetIndirectedArgName(None, 1)
  588.         return '\tob%s = PyCom_PyObjectFromVariant(%s);\n' % (self.arg.name, notdirected)
  589.  
  590.     
  591.     def GetBuildForGatewayPostCode(self):
  592.         return '\tPy_XDECREF(ob%s);\n' % self.arg.name
  593.  
  594.  
  595. ConvertSimpleTypes = {
  596.     'BOOL': ('BOOL', 'int', 'i'),
  597.     'UINT': ('UINT', 'int', 'i'),
  598.     'BYTE': ('BYTE', 'int', 'i'),
  599.     'INT': ('INT', 'int', 'i'),
  600.     'DWORD': ('DWORD', 'int', 'l'),
  601.     'HRESULT': ('HRESULT', 'int', 'l'),
  602.     'ULONG': ('ULONG', 'int', 'l'),
  603.     'LONG': ('LONG', 'int', 'l'),
  604.     'int': ('int', 'int', 'i'),
  605.     'long': ('long', 'int', 'l'),
  606.     'DISPID': ('DISPID', 'long', 'l'),
  607.     'APPBREAKFLAGS': ('int', 'int', 'i'),
  608.     'BREAKRESUMEACTION': ('int', 'int', 'i'),
  609.     'ERRORRESUMEACTION': ('int', 'int', 'i'),
  610.     'BREAKREASON': ('int', 'int', 'i'),
  611.     'BREAKPOINT_STATE': ('int', 'int', 'i'),
  612.     'BREAKRESUME_ACTION': ('int', 'int', 'i'),
  613.     'SOURCE_TEXT_ATTR': ('int', 'int', 'i'),
  614.     'TEXT_DOC_ATTR': ('int', 'int', 'i'),
  615.     'QUERYOPTION': ('int', 'int', 'i'),
  616.     'PARSEACTION': ('int', 'int', 'i') }
  617.  
  618. class ArgFormatterSimple(ArgFormatter):
  619.     
  620.     def GetFormatChar(self):
  621.         return ConvertSimpleTypes[self.arg.type][2]
  622.  
  623.     
  624.     def _GetPythonTypeDesc(self):
  625.         return ConvertSimpleTypes[self.arg.type][1]
  626.  
  627.  
  628. AllConverters = {
  629.     'const OLECHAR': (ArgFormatterOLECHAR, 0, 1),
  630.     'WCHAR': (ArgFormatterOLECHAR, 0, 1),
  631.     'OLECHAR': (ArgFormatterOLECHAR, 0, 1),
  632.     'LPCOLESTR': (ArgFormatterOLECHAR, 1, 1),
  633.     'LPOLESTR': (ArgFormatterOLECHAR, 1, 1),
  634.     'LPCWSTR': (ArgFormatterOLECHAR, 1, 1),
  635.     'LPWSTR': (ArgFormatterOLECHAR, 1, 1),
  636.     'LPCSTR': (ArgFormatterOLECHAR, 1, 1),
  637.     'LPTSTR': (ArgFormatterTCHAR, 1, 1),
  638.     'LPCTSTR': (ArgFormatterTCHAR, 1, 1),
  639.     'HANDLE': (ArgFormatterHANDLE, 0),
  640.     'BSTR': (ArgFormatterBSTR, 1, 0),
  641.     'const IID': (ArgFormatterIID, 0),
  642.     'CLSID': (ArgFormatterIID, 0),
  643.     'IID': (ArgFormatterIID, 0),
  644.     'GUID': (ArgFormatterIID, 0),
  645.     'const GUID': (ArgFormatterIID, 0),
  646.     'const IID': (ArgFormatterIID, 0),
  647.     'REFCLSID': (ArgFormatterIID, 0),
  648.     'REFIID': (ArgFormatterIID, 0),
  649.     'REFGUID': (ArgFormatterIID, 0),
  650.     'const FILETIME': (ArgFormatterTime, 0),
  651.     'const SYSTEMTIME': (ArgFormatterTime, 0),
  652.     'const LPSYSTEMTIME': (ArgFormatterTime, 1, 1),
  653.     'LPSYSTEMTIME': (ArgFormatterTime, 1, 1),
  654.     'FILETIME': (ArgFormatterTime, 0),
  655.     'SYSTEMTIME': (ArgFormatterTime, 0),
  656.     'STATSTG': (ArgFormatterSTATSTG, 0),
  657.     'LARGE_INTEGER': (ArgFormatterLARGE_INTEGER, 0),
  658.     'ULARGE_INTEGER': (ArgFormatterULARGE_INTEGER, 0),
  659.     'VARIANT': (ArgFormatterVARIANT, 0),
  660.     'float': (ArgFormatterFloat, 0),
  661.     'single': (ArgFormatterFloat, 0),
  662.     'short': (ArgFormatterShort, 0),
  663.     'WORD': (ArgFormatterShort, 0),
  664.     'VARIANT_BOOL': (ArgFormatterShort, 0),
  665.     'HWND': (ArgFormatterLONG_PTR, 1),
  666.     'HMENU': (ArgFormatterLONG_PTR, 1),
  667.     'HOLEMENU': (ArgFormatterLONG_PTR, 1),
  668.     'HICON': (ArgFormatterLONG_PTR, 1),
  669.     'HDC': (ArgFormatterLONG_PTR, 1),
  670.     'LPARAM': (ArgFormatterLONG_PTR, 1),
  671.     'WPARAM': (ArgFormatterLONG_PTR, 1),
  672.     'LRESULT': (ArgFormatterLONG_PTR, 1),
  673.     'UINT': (ArgFormatterShort, 0),
  674.     'SVSIF': (ArgFormatterShort, 0),
  675.     'Control': (ArgFormatterInterface, 0, 1),
  676.     'DataObject': (ArgFormatterInterface, 0, 1),
  677.     '_PropertyBag': (ArgFormatterInterface, 0, 1),
  678.     'AsyncProp': (ArgFormatterInterface, 0, 1),
  679.     'DataSource': (ArgFormatterInterface, 0, 1),
  680.     'DataFormat': (ArgFormatterInterface, 0, 1),
  681.     'void **': (ArgFormatterInterface, 2, 2),
  682.     'ITEMIDLIST': (ArgFormatterIDLIST, 0, 0),
  683.     'LPITEMIDLIST': (ArgFormatterIDLIST, 0, 1),
  684.     'LPCITEMIDLIST': (ArgFormatterIDLIST, 0, 1),
  685.     'const ITEMIDLIST': (ArgFormatterIDLIST, 0, 1) }
  686. for key in ConvertSimpleTypes.keys():
  687.     AllConverters[key] = (ArgFormatterSimple, 0)
  688.  
  689.  
  690. def make_arg_converter(arg):
  691.     
  692.     try:
  693.         clz = AllConverters[arg.type][0]
  694.         bin = AllConverters[arg.type][1]
  695.         decl = 0
  696.         if len(AllConverters[arg.type]) > 2:
  697.             decl = AllConverters[arg.type][2]
  698.         
  699.         return clz(arg, bin, decl)
  700.     except KeyError:
  701.         if arg.type[0] == 'I':
  702.             return ArgFormatterInterface(arg, 0, 1)
  703.         raise error_not_supported, "The type '%s' (%s) is unknown." % (arg.type, arg.name)
  704.     except:
  705.         arg.type[0] == 'I'
  706.  
  707.  
  708.  
  709. class Argument:
  710.     regex = re.compile('/\\* \\[([^\\]]*.*?)] \\*/[ \\t](.*[* ]+)(\\w+)(\\[ *])?[\\),]')
  711.     
  712.     def __init__(self, good_interface_names):
  713.         self.good_interface_names = good_interface_names
  714.         self.inout = None
  715.         self.name = None
  716.         self.type = None
  717.         self.const = 0
  718.         self.arrayDecl = 0
  719.  
  720.     
  721.     def BuildFromFile(self, file):
  722.         line = file.readline()
  723.         mo = self.regex.search(line)
  724.         if not mo:
  725.             raise error_not_found
  726.         mo
  727.         self.name = mo.group(3)
  728.         self.inout = mo.group(1).split('][')
  729.         typ = mo.group(2).strip()
  730.         self.raw_type = typ
  731.         self.indirectionLevel = 0
  732.         if mo.group(4):
  733.             self.arrayDecl = 1
  734.             
  735.             try:
  736.                 pos = typ.rindex('__RPC_FAR')
  737.                 self.indirectionLevel = self.indirectionLevel + 1
  738.                 typ = typ[:pos].strip()
  739.             except ValueError:
  740.                 pass
  741.             except:
  742.                 None<EXCEPTION MATCH>ValueError
  743.             
  744.  
  745.         None<EXCEPTION MATCH>ValueError
  746.         typ = typ.replace('__RPC_FAR', '')
  747.         while None:
  748.             
  749.             try:
  750.                 pos = typ.rindex('*')
  751.                 self.indirectionLevel = self.indirectionLevel + 1
  752.                 typ = typ[:pos].strip()
  753.             continue
  754.             except ValueError:
  755.                 break
  756.                 continue
  757.             
  758.  
  759.             self.type = typ
  760.             if self.type[:6] == 'const ':
  761.                 self.unc_type = self.type[6:]
  762.             else:
  763.                 self.unc_type = self.type
  764.         if VERBOSE:
  765.             print '\t   Arg %s of type %s%s (%s)' % (self.name, self.type, '*' * self.indirectionLevel, self.inout)
  766.         
  767.  
  768.     
  769.     def HasAttribute(self, typ):
  770.         return typ in self.inout
  771.  
  772.     
  773.     def GetRawDeclaration(self):
  774.         ret = '%s %s' % (self.raw_type, self.name)
  775.         if self.arrayDecl:
  776.             ret = ret + '[]'
  777.         
  778.         return ret
  779.  
  780.  
  781.  
  782. class Method:
  783.     regex = re.compile('virtual (/\\*.*?\\*/ )?(.*?) (.*?) (.*?)\\(\\w?')
  784.     
  785.     def __init__(self, good_interface_names):
  786.         self.good_interface_names = good_interface_names
  787.         self.name = None
  788.         self.result = None
  789.         self.callconv = None
  790.         self.args = []
  791.  
  792.     
  793.     def BuildFromFile(self, file):
  794.         line = file.readline()
  795.         mo = self.regex.search(line)
  796.         if not mo:
  797.             raise error_not_found
  798.         mo
  799.         self.name = mo.group(4)
  800.         self.result = mo.group(2)
  801.         if self.result != 'HRESULT':
  802.             if self.result == 'DWORD':
  803.                 print 'Warning: Old style interface detected - compilation errors likely!'
  804.             else:
  805.                 print 'Method %s - Only HRESULT return types are supported.' % self.name
  806.             print '\t Method %s %s(' % (self.result, self.name)
  807.         
  808.         while None:
  809.             arg = Argument(self.good_interface_names)
  810.             
  811.             try:
  812.                 arg.BuildFromFile(file)
  813.                 self.args.append(arg)
  814.             continue
  815.             except error_not_found:
  816.                 break
  817.                 continue
  818.             
  819.  
  820.             return None
  821.  
  822.  
  823.  
  824. class Interface:
  825.     regex = re.compile('(interface|) ([^ ]*) : public (.*)$')
  826.     
  827.     def __init__(self, mo):
  828.         self.methods = []
  829.         self.name = mo.group(2)
  830.         self.base = mo.group(3)
  831.         if VERBOSE:
  832.             print 'Interface %s : public %s' % (self.name, self.base)
  833.         
  834.  
  835.     
  836.     def BuildMethods(self, file):
  837.         file.readline()
  838.         file.readline()
  839.         while None:
  840.             
  841.             try:
  842.                 method = Method([
  843.                     self.name])
  844.                 method.BuildFromFile(file)
  845.                 self.methods.append(method)
  846.             continue
  847.             except error_not_found:
  848.                 break
  849.                 continue
  850.             
  851.  
  852.             return None
  853.  
  854.  
  855.  
  856. def find_interface(interfaceName, file):
  857.     interface = None
  858.     line = file.readline()
  859.     while line:
  860.         mo = Interface.regex.search(line)
  861.         if mo:
  862.             name = mo.group(2)
  863.             print name
  864.             AllConverters[name] = (ArgFormatterInterface, 0, 1)
  865.             if name == interfaceName:
  866.                 interface = Interface(mo)
  867.                 interface.BuildMethods(file)
  868.             
  869.         
  870.         line = file.readline()
  871.     if interface:
  872.         return interface
  873.     raise error_not_found
  874.  
  875.  
  876. def parse_interface_info(interfaceName, file):
  877.     
  878.     try:
  879.         return find_interface(interfaceName, file)
  880.     except re.error:
  881.         traceback.print_exc()
  882.         print 'The interface could not be built, as the regular expression failed!'
  883.  
  884.  
  885.  
  886. def test():
  887.     f = open('d:\\msdev\\include\\objidl.h')
  888.     
  889.     try:
  890.         parse_interface_info('IPersistStream', f)
  891.     finally:
  892.         f.close()
  893.  
  894.  
  895.  
  896. def test_regex(r, text):
  897.     res = r.search(text, 0)
  898.     if res == -1:
  899.         print '** Not found'
  900.     else:
  901.         print '%d\n%s\n%s\n%s\n%s' % (res, r.group(1), r.group(2), r.group(3), r.group(4))
  902.  
  903.