home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / wx / py / introspect.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  7.6 KB  |  312 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
  5. __cvsid__ = '$Id: introspect.py 39896 2006-06-29 22:24:00Z RD $'
  6. __revision__ = '$Revision: 39896 $'[11:-2]
  7. import cStringIO
  8. import inspect
  9. import sys
  10. import tokenize
  11. import types
  12. import wx
  13.  
  14. def getAutoCompleteList(command = '', locals = None, includeMagic = 1, includeSingle = 1, includeDouble = 1):
  15.     attributes = []
  16.     root = getRoot(command, terminator = '.')
  17.     
  18.     try:
  19.         if locals is not None:
  20.             object = eval(root, locals)
  21.         else:
  22.             object = eval(root)
  23.     except:
  24.         pass
  25.  
  26.     attributes = getAttributeNames(object, includeMagic, includeSingle, includeDouble)
  27.     return attributes
  28.  
  29.  
  30. def getAttributeNames(object, includeMagic = 1, includeSingle = 1, includeDouble = 1):
  31.     attributes = []
  32.     dict = { }
  33.     str_type = str(type(object))
  34.     for item in attributes:
  35.         dict[item] = None
  36.     
  37.     attributes = dict.keys()
  38.     attributes = _[3]
  39.     attributes.sort((lambda x, y: cmp(x.upper(), y.upper())))
  40.     if not includeDouble:
  41.         attributes = filter((lambda item: item[:2] != '__'), attributes)
  42.     
  43.     return attributes
  44.  
  45.  
  46. def hasattrAlwaysReturnsTrue(object):
  47.     return hasattr(object, 'bogu5_123_aTTri8ute')
  48.  
  49.  
  50. def getAllAttributeNames(object):
  51.     attrdict = { }
  52.     
  53.     try:
  54.         key = type(object).__name__
  55.     except:
  56.         key = 'anonymous'
  57.  
  58.     wakeupcall = dir(object)
  59.     del wakeupcall
  60.     attributes = dir(object)
  61.     attrdict[(key, 'dir', len(attributes))] = attributes
  62.     
  63.     try:
  64.         attributes = object.__dict__.keys()
  65.         attributes.sort()
  66.     except:
  67.         pass
  68.  
  69.     attrdict[(key, '__dict__', len(attributes))] = attributes
  70.     
  71.     try:
  72.         klass = object.__class__
  73.     except:
  74.         pass
  75.  
  76.     if klass is object:
  77.         pass
  78.     else:
  79.         attrdict.update(getAllAttributeNames(klass))
  80.     
  81.     try:
  82.         bases = object.__bases__
  83.     except:
  84.         pass
  85.  
  86.     if isinstance(bases, types.TupleType):
  87.         for base in bases:
  88.             if type(base) is types.TypeType:
  89.                 continue
  90.             attrdict.update(getAllAttributeNames(base))
  91.         
  92.     
  93.     return attrdict
  94.  
  95.  
  96. def getCallTip(command = '', locals = None):
  97.     calltip = ('', '', '')
  98.     root = getRoot(command, terminator = '(')
  99.     
  100.     try:
  101.         if locals is not None:
  102.             object = eval(root, locals)
  103.         else:
  104.             object = eval(root)
  105.     except:
  106.         return calltip
  107.  
  108.     name = ''
  109.     (object, dropSelf) = getBaseObject(object)
  110.     
  111.     try:
  112.         name = object.__name__
  113.     except AttributeError:
  114.         pass
  115.  
  116.     tip1 = ''
  117.     argspec = ''
  118.     if inspect.isbuiltin(object):
  119.         pass
  120.     elif inspect.isfunction(object):
  121.         argspec = apply(inspect.formatargspec, inspect.getargspec(object))
  122.         if dropSelf:
  123.             temp = argspec.split(',')
  124.             if len(temp) == 1:
  125.                 argspec = '()'
  126.             elif temp[0][:2] == '(*':
  127.                 pass
  128.             else:
  129.                 argspec = '(' + ','.join(temp[1:]).lstrip()
  130.         
  131.         tip1 = name + argspec
  132.     
  133.     doc = ''
  134.     if callable(object):
  135.         
  136.         try:
  137.             doc = inspect.getdoc(object)
  138.  
  139.     
  140.     if doc:
  141.         firstline = doc.split('\n')[0].lstrip()
  142.         if tip1 == firstline or firstline[:len(name) + 1] == name + '(':
  143.             tip1 = ''
  144.         else:
  145.             tip1 += '\n\n'
  146.         docpieces = doc.split('\n\n')
  147.         tip2 = docpieces[0]
  148.         tip3 = '\n\n'.join(docpieces[1:])
  149.         tip = '%s%s\n\n%s' % (tip1, tip2, tip3)
  150.     else:
  151.         tip = tip1
  152.     calltip = (name, argspec[1:-1], tip.strip())
  153.     return calltip
  154.  
  155.  
  156. def getRoot(command, terminator = None):
  157.     command = command.split('\n')[-1]
  158.     if command.startswith(sys.ps2):
  159.         command = command[len(sys.ps2):]
  160.     
  161.     command = command.lstrip()
  162.     command = rtrimTerminus(command, terminator)
  163.     tokens = getTokens(command)
  164.     if not tokens:
  165.         return ''
  166.     if tokens[-1][0] is tokenize.ENDMARKER:
  167.         del tokens[-1]
  168.     
  169.     if not tokens:
  170.         return ''
  171.     if terminator == '.':
  172.         if tokens[-1][1] != '.' or tokens[-1][0] is not tokenize.OP:
  173.             return ''
  174.     command = command.rstrip()
  175.     tokens = getTokens(command)
  176.     tokens.reverse()
  177.     line = ''
  178.     start = None
  179.     prefix = ''
  180.     laststring = '.'
  181.     emptyTypes = ('[]', '()', '{}')
  182.     for token in tokens:
  183.         tokentype = token[0]
  184.         tokenstring = token[1]
  185.         line = token[4]
  186.         if tokentype is tokenize.ENDMARKER:
  187.             continue
  188.         
  189.         if tokentype in (tokenize.NAME, tokenize.STRING, tokenize.NUMBER) and laststring != '.':
  190.             if prefix and line[token[3][1]] != ' ':
  191.                 prefix = ''
  192.             
  193.             break
  194.         
  195.         if (tokentype in (tokenize.NAME, tokenize.STRING, tokenize.NUMBER) or tokentype is tokenize.OP) and tokenstring == '.':
  196.             if prefix:
  197.                 prefix = ''
  198.                 break
  199.             else:
  200.                 start = token[2][1]
  201.         elif len(tokenstring) == 1 and tokenstring in '[({])}':
  202.             if prefix in emptyTypes and tokenstring in '[({':
  203.                 break
  204.             else:
  205.                 prefix = tokenstring + prefix
  206.         else:
  207.             break
  208.         laststring = tokenstring
  209.     
  210.     if start is None:
  211.         start = len(line)
  212.     
  213.     root = line[start:]
  214.     if prefix in emptyTypes:
  215.         root = prefix + root
  216.     
  217.     return root
  218.  
  219.  
  220. def getTokens(command):
  221.     if type(command) == unicode:
  222.         
  223.         try:
  224.             command = command.encode(wx.GetDefaultPyEncoding())
  225.         except UnicodeEncodeError:
  226.             pass
  227.         except:
  228.             None<EXCEPTION MATCH>UnicodeEncodeError
  229.         
  230.  
  231.     None<EXCEPTION MATCH>UnicodeEncodeError
  232.     f = cStringIO.StringIO(command)
  233.     tokens = []
  234.     
  235.     try:
  236.         
  237.         def eater(*args):
  238.             tokens.append(args)
  239.  
  240.         tokenize.tokenize_loop(f.readline, eater)
  241.     except tokenize.TokenError:
  242.         pass
  243.  
  244.     return tokens
  245.  
  246.  
  247. def rtrimTerminus(command, terminator = None):
  248.     if terminator:
  249.         pieces = command.split(terminator)
  250.         if len(pieces) > 1:
  251.             command = terminator.join(pieces[:-1]) + terminator
  252.         
  253.     
  254.     return command
  255.  
  256.  
  257. def getBaseObject(object):
  258.     if inspect.isbuiltin(object):
  259.         dropSelf = 0
  260.     elif inspect.ismethod(object):
  261.         
  262.         try:
  263.             if object.im_self is None:
  264.                 dropSelf = 0
  265.             else:
  266.                 dropSelf = 1
  267.             object = object.im_func
  268.         except AttributeError:
  269.             dropSelf = 0
  270.         except:
  271.             None<EXCEPTION MATCH>AttributeError
  272.         
  273.  
  274.     None<EXCEPTION MATCH>AttributeError
  275.     if inspect.isclass(object):
  276.         constructor = getConstructor(object)
  277.         if constructor is not None:
  278.             object = constructor
  279.             dropSelf = 1
  280.         else:
  281.             dropSelf = 0
  282.     elif callable(object):
  283.         
  284.         try:
  285.             object = object.__call__.im_func
  286.             dropSelf = 1
  287.         except AttributeError:
  288.             dropSelf = 0
  289.         except:
  290.             None<EXCEPTION MATCH>AttributeError
  291.         
  292.  
  293.     None<EXCEPTION MATCH>AttributeError
  294.     dropSelf = 0
  295.     return (object, dropSelf)
  296.  
  297.  
  298. def getConstructor(object):
  299.     
  300.     try:
  301.         return object.__init__.im_func
  302.     except AttributeError:
  303.         for base in object.__bases__:
  304.             constructor = getConstructor(base)
  305.             if constructor is not None:
  306.                 return constructor
  307.         
  308.     except:
  309.         constructor is not None
  310.  
  311.  
  312.