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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __docformat__ = 'restructuredtext en'
  5. import re
  6. import IPython.ipapi as IPython
  7.  
  8. class LineInfo(object):
  9.     
  10.     def __init__(self, line, continue_prompt):
  11.         self.line = line
  12.         self.continue_prompt = continue_prompt
  13.         (self.pre, self.iFun, self.theRest) = splitUserInput(line)
  14.         self.preChar = self.pre.strip()
  15.         if self.preChar:
  16.             self.preWhitespace = ''
  17.         else:
  18.             self.preWhitespace = self.pre
  19.         self._oinfo = None
  20.  
  21.     
  22.     def ofind(self, ip):
  23.         if not self._oinfo:
  24.             self._oinfo = ip._ofind(self.iFun)
  25.         
  26.         return self._oinfo
  27.  
  28.     
  29.     def __str__(self):
  30.         return 'Lineinfo [%s|%s|%s]' % (self.pre, self.iFun, self.theRest)
  31.  
  32.  
  33.  
  34. def splitUserInput(line, pattern = None):
  35.     if not pattern:
  36.         pattern = line_split
  37.     
  38.     match = pattern.match(line)
  39.     if not match:
  40.         
  41.         try:
  42.             (iFun, theRest) = line.split(None, 1)
  43.         except ValueError:
  44.             iFun = line
  45.             theRest = ''
  46.  
  47.         pre = re.match('^(\\s*)(.*)', line).groups()[0]
  48.     else:
  49.         (pre, iFun, theRest) = match.groups()
  50.     
  51.     try:
  52.         iFun = iFun.encode('ascii')
  53.     except UnicodeEncodeError:
  54.         theRest = iFun + u' ' + theRest
  55.         iFun = u''
  56.  
  57.     return (pre, iFun.strip(), theRest.lstrip())
  58.  
  59. line_split = re.compile('^([,;/%?]|!!?|\\s*)\\s*([\\w\\.]+)(\\s+.*$|$)')
  60. shell_line_split = re.compile('^(\\s*)(\\S*\\s*)(.*$)')
  61.  
  62. def prefilter(line_info, ip):
  63.     for check in [
  64.         checkEmacs,
  65.         checkShellEscape,
  66.         checkIPyAutocall,
  67.         checkMultiLineMagic,
  68.         checkEscChars,
  69.         checkAssignment,
  70.         checkAutomagic,
  71.         checkAlias,
  72.         checkPythonOps,
  73.         checkAutocall]:
  74.         handler = check(line_info, ip)
  75.         if handler:
  76.             return handler(line_info)
  77.     
  78.     return ip.handle_normal(line_info)
  79.  
  80.  
  81. def checkShellEscape(l_info, ip):
  82.     if l_info.line.lstrip().startswith(ip.ESC_SHELL):
  83.         return ip.handle_shell_escape
  84.  
  85.  
  86. def checkEmacs(l_info, ip):
  87.     if l_info.line.endswith('# PYTHON-MODE'):
  88.         return ip.handle_emacs
  89.     return None
  90.  
  91.  
  92. def checkIPyAutocall(l_info, ip):
  93.     obj = ip.user_ns.get(l_info.iFun, None)
  94.     if isinstance(obj, IPython.ipapi.IPyAutocall):
  95.         obj.set_ip(ip.api)
  96.         return ip.handle_auto
  97.     return None
  98.  
  99.  
  100. def checkMultiLineMagic(l_info, ip):
  101.     if l_info.continue_prompt and ip.rc.multi_line_specials:
  102.         if l_info.iFun.startswith(ip.ESC_MAGIC):
  103.             return ip.handle_magic
  104.     else:
  105.         return None
  106.     return l_info.iFun.startswith(ip.ESC_MAGIC)
  107.  
  108.  
  109. def checkEscChars(l_info, ip):
  110.     if l_info.line[-1] == ip.ESC_HELP and l_info.preChar != ip.ESC_SHELL and l_info.preChar != ip.ESC_SH_CAP:
  111.         return ip.handle_help
  112.     if l_info.preChar in ip.esc_handlers:
  113.         return ip.esc_handlers[l_info.preChar]
  114.     return None
  115.  
  116.  
  117. def checkAssignment(l_info, ip):
  118.     if l_info.theRest and l_info.theRest[0] in '=,':
  119.         return ip.handle_normal
  120.     return None
  121.  
  122.  
  123. def checkAutomagic(l_info, ip):
  124.     if not (ip.rc.automagic) or not hasattr(ip, 'magic_' + l_info.iFun):
  125.         return None
  126.     if l_info.continue_prompt and not (ip.rc.multi_line_specials):
  127.         return None
  128.     head = l_info.iFun.split('.', 1)[0]
  129.     if isShadowed(head, ip):
  130.         return None
  131.     return ip.handle_magic
  132.  
  133.  
  134. def checkAlias(l_info, ip):
  135.     head = l_info.iFun.split('.', 1)[0]
  136.     if l_info.iFun not in ip.alias_table and head not in ip.alias_table or isShadowed(head, ip):
  137.         return None
  138.     return ip.handle_alias
  139.  
  140.  
  141. def checkPythonOps(l_info, ip):
  142.     if l_info.theRest and l_info.theRest[0] in '!=()<>,+*/%^&|':
  143.         return ip.handle_normal
  144.     return None
  145.  
  146.  
  147. def checkAutocall(l_info, ip):
  148.     if not ip.rc.autocall:
  149.         return None
  150.     oinfo = l_info.ofind(ip)
  151.     if not oinfo['found']:
  152.         return None
  153.     if callable(oinfo['obj']) and not re_exclude_auto.match(l_info.theRest) and re_fun_name.match(l_info.iFun):
  154.         return ip.handle_auto
  155.     return None
  156.  
  157. re_fun_name = re.compile('[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
  158. re_exclude_auto = re.compile('^[,&^\\|\\*/\\+-]|^is |^not |^in |^and |^or ')
  159.  
  160. def isShadowed(identifier, ip):
  161.     if not identifier in ip.user_ns and identifier in ip.internal_ns:
  162.         pass
  163.     return identifier in ip.ns_table['builtin']
  164.  
  165.