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

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