home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / win32com / client / makepy.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  15.1 KB  |  394 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Generate a .py file from an OLE TypeLibrary file.
  5.  
  6.  
  7.  This module is concerned only with the actual writing of
  8.  a .py file.  It draws on the @build@ module, which builds 
  9.  the knowledge of a COM interface.
  10.  
  11. '''
  12. usageHelp = ' Usage:\n\n  makepy.py [-h] [-x0|1] [-u] [-o filename] [-d] [typelib, ...]\n  \n  typelib -- A TLB, DLL, OCX, Description, or possibly something else.\n  -h    -- Do not generate hidden methods.\n  -u    -- Python 1.5 and earlier: Do not convert all Unicode objects to strings.\n           Python 1.6 and later: Do convert all Unicode objects to strings.\n  -o outputFile -- Generate to named file - dont generate to standard directory.\n  -i [typelib] -- Show info for specified typelib, or select the typelib if not specified.\n  -v    -- Verbose output\n  -q    -- Quiet output\n  -d    -- Generate the base code now and classes code on demand\n  \nExamples:\n  makepy.py\n    Present a list of type libraries.\n    \n  makepy.py "Microsoft Excel 8.0 Object Library"\n    Generate support for the typelibrary with the specified description\n    (in this case, MS Excel object model)\n\n'
  13. import genpy
  14. import string
  15. import sys
  16. import os
  17. import types
  18. import pythoncom
  19. import selecttlb
  20. import gencache
  21. from win32com.client import NeedUnicodeConversions
  22. error = 'makepy.error'
  23.  
  24. def usage():
  25.     sys.stderr.write(usageHelp)
  26.     sys.exit(2)
  27.  
  28.  
  29. def ShowInfo(spec):
  30.     if not spec:
  31.         tlbSpec = selecttlb.SelectTlb(excludeFlags = selecttlb.FLAG_HIDDEN)
  32.         if tlbSpec is None:
  33.             return None
  34.         
  35.         
  36.         try:
  37.             tlb = pythoncom.LoadRegTypeLib(tlbSpec.clsid, tlbSpec.major, tlbSpec.minor, tlbSpec.lcid)
  38.         except pythoncom.com_error:
  39.             sys.stderr.write("Warning - could not load registered typelib '%s'\n" % tlbSpec.clsid)
  40.             tlb = None
  41.  
  42.         infos = [
  43.             (tlb, tlbSpec)]
  44.     else:
  45.         infos = GetTypeLibsForSpec(spec)
  46.     for tlb, tlbSpec in infos:
  47.         desc = tlbSpec.desc
  48.         if desc is None:
  49.             if tlb is None:
  50.                 desc = '<Could not load typelib %s>' % tlbSpec.dll
  51.             else:
  52.                 desc = tlb.GetDocumentation(-1)[0]
  53.         
  54.         print desc
  55.         print ' %s, lcid=%s, major=%s, minor=%s' % (tlbSpec.clsid, tlbSpec.lcid, tlbSpec.major, tlbSpec.minor)
  56.         print ' >>> # Use these commands in Python code to auto generate .py support'
  57.         print ' >>> from win32com.client import gencache'
  58.         print " >>> gencache.EnsureModule('%s', %s, %s, %s)" % (tlbSpec.clsid, tlbSpec.lcid, tlbSpec.major, tlbSpec.minor)
  59.     
  60.  
  61.  
  62. class SimpleProgress(genpy.GeneratorProgress):
  63.     '''A simple progress class prints its output to stderr
  64. \t'''
  65.     
  66.     def __init__(self, verboseLevel):
  67.         self.verboseLevel = verboseLevel
  68.  
  69.     
  70.     def Close(self):
  71.         pass
  72.  
  73.     
  74.     def Finished(self):
  75.         if self.verboseLevel > 1:
  76.             sys.stderr.write('Generation complete..\n')
  77.         
  78.  
  79.     
  80.     def SetDescription(self, desc, maxticks = None):
  81.         if self.verboseLevel:
  82.             sys.stderr.write(desc + '\n')
  83.         
  84.  
  85.     
  86.     def Tick(self, desc = None):
  87.         pass
  88.  
  89.     
  90.     def VerboseProgress(self, desc, verboseLevel = 2):
  91.         if self.verboseLevel >= verboseLevel:
  92.             sys.stderr.write(desc + '\n')
  93.         
  94.  
  95.     
  96.     def LogBeginGenerate(self, filename):
  97.         self.VerboseProgress('Generating to %s' % filename, 1)
  98.  
  99.     
  100.     def LogWarning(self, desc):
  101.         self.VerboseProgress('WARNING: ' + desc, 1)
  102.  
  103.  
  104.  
  105. class GUIProgress(SimpleProgress):
  106.     
  107.     def __init__(self, verboseLevel):
  108.         import win32ui
  109.         import pywin
  110.         SimpleProgress.__init__(self, verboseLevel)
  111.         self.dialog = None
  112.  
  113.     
  114.     def Close(self):
  115.         if self.dialog is not None:
  116.             self.dialog.Close()
  117.             self.dialog = None
  118.         
  119.  
  120.     
  121.     def Starting(self, tlb_desc):
  122.         SimpleProgress.Starting(self, tlb_desc)
  123.         if self.dialog is None:
  124.             status = status
  125.             import pywin.dialogs
  126.             self.dialog = status.ThreadedStatusProgressDialog(tlb_desc)
  127.         else:
  128.             self.dialog.SetTitle(tlb_desc)
  129.  
  130.     
  131.     def SetDescription(self, desc, maxticks = None):
  132.         self.dialog.SetText(desc)
  133.         if maxticks:
  134.             self.dialog.SetMaxTicks(maxticks)
  135.         
  136.  
  137.     
  138.     def Tick(self, desc = None):
  139.         self.dialog.Tick()
  140.         if desc is not None:
  141.             self.dialog.SetText(desc)
  142.         
  143.  
  144.  
  145.  
  146. def GetTypeLibsForSpec(arg):
  147.     '''Given an argument on the command line (either a file name or a library description)
  148. \treturn a list of actual typelibs to use.
  149. \t'''
  150.     typelibs = []
  151.     
  152.     try:
  153.         
  154.         try:
  155.             tlb = pythoncom.LoadTypeLib(arg)
  156.             spec = selecttlb.TypelibSpec(None, 0, 0, 0)
  157.             spec.FromTypelib(tlb, arg)
  158.             typelibs.append((tlb, spec))
  159.         except pythoncom.com_error:
  160.             tlbs = selecttlb.FindTlbsWithDescription(arg)
  161.             if len(tlbs) == 0:
  162.                 print "Could not locate a type library matching '%s'" % arg
  163.             
  164.             for spec in tlbs:
  165.                 tlb = pythoncom.LoadRegTypeLib(spec.clsid, spec.major, spec.minor, spec.lcid)
  166.                 attr = tlb.GetLibAttr()
  167.                 spec.major = attr[3]
  168.                 spec.minor = attr[4]
  169.                 spec.lcid = attr[1]
  170.                 typelibs.append((tlb, spec))
  171.             
  172.  
  173.         return typelibs
  174.     except pythoncom.com_error:
  175.         (t, v, tb) = sys.exc_info()
  176.         sys.stderr.write("Unable to load type library from '%s' - %s\n" % (arg, v))
  177.         tb = None
  178.         sys.exit(1)
  179.  
  180.  
  181.  
  182. def GenerateFromTypeLibSpec(typelibInfo, file = None, verboseLevel = None, progressInstance = None, bUnicodeToString = NeedUnicodeConversions, bQuiet = None, bGUIProgress = None, bForDemand = 0, bBuildHidden = 1):
  183.     if bQuiet is not None or bGUIProgress is not None:
  184.         print 'Please dont use the bQuiet or bGUIProgress params'
  185.         print "use the 'verboseLevel', and 'progressClass' params"
  186.     
  187.     if verboseLevel is None:
  188.         verboseLevel = 0
  189.     
  190.     if bForDemand and file is not None:
  191.         raise RuntimeError, 'You can only perform a demand-build when the output goes to the gen_py directory'
  192.     
  193.     if type(typelibInfo) == type(()):
  194.         (typelibCLSID, lcid, major, minor) = typelibInfo
  195.         tlb = pythoncom.LoadRegTypeLib(typelibCLSID, major, minor, lcid)
  196.         spec = selecttlb.TypelibSpec(typelibCLSID, lcid, major, minor)
  197.         spec.FromTypelib(tlb, str(typelibCLSID))
  198.         typelibs = [
  199.             (tlb, spec)]
  200.     elif type(typelibInfo) == types.InstanceType:
  201.         tlb = pythoncom.LoadRegTypeLib(typelibInfo.clsid, typelibInfo.major, typelibInfo.minor, typelibInfo.lcid)
  202.         typelibs = [
  203.             (tlb, typelibInfo)]
  204.     elif hasattr(typelibInfo, 'GetLibAttr'):
  205.         tla = typelibInfo.GetLibAttr()
  206.         guid = tla[0]
  207.         lcid = tla[1]
  208.         major = tla[3]
  209.         minor = tla[4]
  210.         spec = selecttlb.TypelibSpec(guid, lcid, major, minor)
  211.         typelibs = [
  212.             (typelibInfo, spec)]
  213.     else:
  214.         typelibs = GetTypeLibsForSpec(typelibInfo)
  215.     if progressInstance is None:
  216.         
  217.         try:
  218.             if not bForDemand:
  219.                 import win32api
  220.                 import win32con
  221.                 if win32api.GetVersionEx()[3] == win32con.VER_PLATFORM_WIN32_NT:
  222.                     bMakeGuiProgress = 1
  223.                 else:
  224.                     
  225.                     try:
  226.                         win32api.GetConsoleTitle()
  227.                         bMakeGuiProgress = 0
  228.                     except win32api.error:
  229.                         bMakeGuiProgress = 1
  230.  
  231.                 if bMakeGuiProgress:
  232.                     progressInstance = GUIProgress(verboseLevel)
  233.                 
  234.         except ImportError:
  235.             pass
  236.  
  237.     
  238.     if progressInstance is None:
  239.         progressInstance = SimpleProgress(verboseLevel)
  240.     
  241.     progress = progressInstance
  242.     bToGenDir = file is None
  243.     for typelib, info in typelibs:
  244.         if file is None:
  245.             this_name = gencache.GetGeneratedFileName(info.clsid, info.lcid, info.major, info.minor)
  246.             full_name = os.path.join(gencache.GetGeneratePath(), this_name)
  247.             if bForDemand:
  248.                 
  249.                 try:
  250.                     os.unlink(full_name + '.py')
  251.                 except os.error:
  252.                     pass
  253.  
  254.                 
  255.                 try:
  256.                     os.unlink(full_name + '.pyc')
  257.                 except os.error:
  258.                     pass
  259.  
  260.                 
  261.                 try:
  262.                     os.unlink(full_name + '.pyo')
  263.                 except os.error:
  264.                     pass
  265.  
  266.                 if not os.path.isdir(full_name):
  267.                     os.mkdir(full_name)
  268.                 
  269.                 outputName = os.path.join(full_name, '__init__.py')
  270.             else:
  271.                 outputName = full_name + '.py'
  272.             fileUse = open(outputName, 'wt')
  273.             progress.LogBeginGenerate(outputName)
  274.         else:
  275.             fileUse = file
  276.         gen = genpy.Generator(typelib, info.dll, progress, bUnicodeToString = bUnicodeToString, bBuildHidden = bBuildHidden)
  277.         gen.generate(fileUse, bForDemand)
  278.         if file is None:
  279.             fileUse.close()
  280.         
  281.         if bToGenDir:
  282.             progress.SetDescription('Importing module')
  283.             gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor)
  284.         
  285.     
  286.     progress.Close()
  287.  
  288.  
  289. def GenerateChildFromTypeLibSpec(child, typelibInfo, verboseLevel = None, progressInstance = None, bUnicodeToString = NeedUnicodeConversions):
  290.     if verboseLevel is None:
  291.         verboseLevel = 0
  292.     
  293.     if type(typelibInfo) == type(()):
  294.         (typelibCLSID, lcid, major, minor) = typelibInfo
  295.         tlb = pythoncom.LoadRegTypeLib(typelibCLSID, major, minor, lcid)
  296.     else:
  297.         tlb = typelibInfo
  298.         tla = typelibInfo.GetLibAttr()
  299.         typelibCLSID = tla[0]
  300.         lcid = tla[1]
  301.         major = tla[3]
  302.         minor = tla[4]
  303.     spec = selecttlb.TypelibSpec(typelibCLSID, lcid, major, minor)
  304.     spec.FromTypelib(tlb, str(typelibCLSID))
  305.     typelibs = [
  306.         (tlb, spec)]
  307.     if progressInstance is None:
  308.         progressInstance = SimpleProgress(verboseLevel)
  309.     
  310.     progress = progressInstance
  311.     for typelib, info in typelibs:
  312.         dir_name = gencache.GetGeneratedFileName(info.clsid, info.lcid, info.major, info.minor)
  313.         dir_path_name = os.path.join(gencache.GetGeneratePath(), dir_name)
  314.         progress.LogBeginGenerate(dir_path_name)
  315.         gen = genpy.Generator(typelib, info.dll, progress, bUnicodeToString = bUnicodeToString)
  316.         gen.generate_child(child, dir_path_name)
  317.         progress.SetDescription('Importing module')
  318.         __import__('win32com.gen_py.' + dir_name + '.' + child)
  319.     
  320.     progress.Close()
  321.  
  322.  
  323. def main():
  324.     import getopt
  325.     hiddenSpec = 1
  326.     bUnicodeToString = NeedUnicodeConversions
  327.     outputName = None
  328.     verboseLevel = 1
  329.     doit = 1
  330.     bForDemand = 0
  331.     
  332.     try:
  333.         (opts, args) = getopt.getopt(sys.argv[1:], 'vo:huiqd')
  334.         for o, v in opts:
  335.             if o == '-h':
  336.                 hiddenSpec = 0
  337.             elif o == '-u':
  338.                 bUnicodeToString = not NeedUnicodeConversions
  339.             elif o == '-o':
  340.                 outputName = v
  341.             elif o == '-v':
  342.                 verboseLevel = verboseLevel + 1
  343.             elif o == '-q':
  344.                 verboseLevel = verboseLevel - 1
  345.             elif o == '-i':
  346.                 if len(args) == 0:
  347.                     ShowInfo(None)
  348.                 else:
  349.                     for arg in args:
  350.                         ShowInfo(arg)
  351.                     
  352.                 doit = 0
  353.             elif o == '-d':
  354.                 bForDemand = 1
  355.             
  356.     except (getopt.error, error):
  357.         msg = None
  358.         sys.stderr.write(str(msg) + '\n')
  359.         usage()
  360.  
  361.     if bForDemand and outputName is not None:
  362.         sys.stderr.write('Can not use -d and -o together\n')
  363.         usage()
  364.     
  365.     if not doit:
  366.         return 0
  367.     
  368.     if len(args) == 0:
  369.         rc = selecttlb.SelectTlb()
  370.         if rc is None:
  371.             sys.exit(1)
  372.         
  373.         args = [
  374.             rc]
  375.     
  376.     if outputName is not None:
  377.         f = open(outputName, 'w')
  378.     else:
  379.         f = None
  380.     for arg in args:
  381.         GenerateFromTypeLibSpec(arg, f, verboseLevel = verboseLevel, bForDemand = bForDemand, bBuildHidden = hiddenSpec)
  382.     
  383.     if f:
  384.         f.close()
  385.     
  386.  
  387. if __name__ == '__main__':
  388.     rc = main()
  389.     if rc:
  390.         sys.exit(rc)
  391.     
  392.     sys.exit(0)
  393.  
  394.