home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import __builtin__
- import __main__
- import glob
- import keyword
- import os
- import re
- import shlex
- import sys
- import IPython.rlineimpl as readline
- import itertools
- from IPython.ipstruct import Struct
- from IPython import ipapi
- from IPython import generics
- import types
-
- try:
- set()
- except NameError:
- from sets import Set as set
-
- from IPython.genutils import debugx, dir2
- __all__ = [
- 'Completer',
- 'IPCompleter']
-
- class Completer:
-
- def __init__(self, namespace = None, global_namespace = None):
- if namespace is None:
- self.use_main_ns = 1
- else:
- self.use_main_ns = 0
- self.namespace = namespace
- if global_namespace is None:
- self.global_namespace = { }
- else:
- self.global_namespace = global_namespace
-
-
- def complete(self, text, state):
- if self.use_main_ns:
- self.namespace = __main__.__dict__
-
- if state == 0:
- if '.' in text:
- self.matches = self.attr_matches(text)
- else:
- self.matches = self.global_matches(text)
-
-
- try:
- return self.matches[state]
- except IndexError:
- return None
-
-
-
- def global_matches(self, text):
- matches = []
- match_append = matches.append
- n = len(text)
- for lst in [
- keyword.kwlist,
- __builtin__.__dict__.keys(),
- self.namespace.keys(),
- self.global_namespace.keys()]:
- for word in lst:
- if word[:n] == text and word != '__builtins__':
- match_append(word)
- continue
-
-
- return matches
-
-
- def attr_matches(self, text):
- import re
- m = re.match('(\\S+(\\.\\w+)*)\\.(\\w*)$', text)
- if not m:
- return []
- (expr, attr) = m.group(1, 3)
-
- try:
- obj = eval(expr, self.namespace)
- except:
- m
-
- try:
- obj = eval(expr, self.global_namespace)
- return []
-
-
- words = dir2(obj)
-
- try:
- words = generics.complete_object(obj, words)
- except ipapi.TryNext:
- m
- m
- except:
- m
-
- n = len(attr)
- res = _[1]
- return res
-
-
-
- class IPCompleter(Completer):
-
- def __init__(self, shell, namespace = None, global_namespace = None, omit__names = 0, alias_table = None):
- Completer.__init__(self, namespace, global_namespace)
- self.magic_prefix = shell.name + '.magic_'
- self.magic_escape = shell.ESC_MAGIC
- self.readline = readline
- delims = self.readline.get_completer_delims()
- delims = delims.replace(self.magic_escape, '')
- self.readline.set_completer_delims(delims)
- self.get_line_buffer = self.readline.get_line_buffer
- self.get_endidx = self.readline.get_endidx
- self.omit__names = omit__names
- self.merge_completions = shell.rc.readline_merge_completions
- if alias_table is None:
- alias_table = { }
-
- self.alias_table = alias_table
- self.space_name_re = re.compile('([^\\\\] )')
- self.glob = glob.glob
- term = os.environ.get('TERM', 'xterm')
- self.dumb_terminal = term in ('dumb', 'emacs')
- if sys.platform == 'win32':
- self.clean_glob = self._clean_glob_win32
- else:
- self.clean_glob = self._clean_glob
- self.matchers = [
- self.python_matches,
- self.file_matches,
- self.alias_matches,
- self.python_func_kw_matches]
-
-
- def all_completions(self, text):
- completions = []
- comp_append = completions.append
-
- try:
- for i in xrange(sys.maxint):
- res = self.complete(text, i)
- if not res:
- break
-
- comp_append(res)
- except NameError:
- pass
-
- return completions
-
-
- def _clean_glob(self, text):
- return self.glob('%s*' % text)
-
-
- def _clean_glob_win32(self, text):
- return [ f.replace('\\', '/') for f in self.glob('%s*' % text) ]
-
-
- def file_matches(self, text):
- if sys.platform == 'win32':
- protectables = ' '
- else:
- protectables = ' ()'
- if text.startswith('!'):
- text = text[1:]
- text_prefix = '!'
- else:
- text_prefix = ''
-
- def protect_filename(s):
- return []([ ch for ch in s ])
-
-
- def single_dir_expand(matches):
- if len(matches) == 1 and os.path.isdir(matches[0]):
- d = matches[0]
- if d[-1] in ('/', '\\'):
- d = d[:-1]
-
- subdirs = os.listdir(d)
- if subdirs:
- matches = [ d + '/' + p for p in subdirs ]
- return single_dir_expand(matches)
- return matches
- os.path.isdir(matches[0])
- return matches
-
- lbuf = self.lbuf
- open_quotes = 0
-
- try:
- lsplit = shlex.split(lbuf)[-1]
- except ValueError:
- ((None,),)
- ((None,),)
- if lbuf.count('"') == 1:
- open_quotes = 1
- lsplit = lbuf.split('"')[-1]
- elif lbuf.count("'") == 1:
- open_quotes = 1
- lsplit = lbuf.split("'")[-1]
- else:
- return []
- lbuf.count('"') == 1
- except IndexError:
- lsplit = ''
- except:
- None<EXCEPTION MATCH>IndexError
-
-
- if lsplit != protect_filename(lsplit):
- has_protectables = 1
- text0 = text
- text = lsplit
- else:
- has_protectables = 0
- text = os.path.expanduser(text)
- if text == '':
- return [ text_prefix + protect_filename(f) for f in self.glob('*') ]
- m0 = self.clean_glob(text.replace('\\', ''))
- if has_protectables:
- len_lsplit = len(lsplit)
- matches = [ text_prefix + text0 + protect_filename(f[len_lsplit:]) for f in m0 ]
- elif open_quotes:
- matches = m0
- else:
- matches = [ text_prefix + protect_filename(f) for f in m0 ]
- return single_dir_expand(matches)
-
-
- def alias_matches(self, text):
- if ' ' in self.lbuf.lstrip() and not self.lbuf.lstrip().startswith('sudo'):
- return []
- text = os.path.expanduser(text)
- aliases = self.alias_table.keys()
- if text == '':
- return aliases
- return _[1]
-
-
- def python_matches(self, text):
- if '.' in text:
-
- try:
- matches = self.attr_matches(text)
- if text.endswith('.') and self.omit__names:
- if self.omit__names == 1:
-
- no__name = lambda txt: re.match('.*\\.__.*?__', txt) is None
- else:
-
- no__name = lambda txt: re.match('.*\\._.*?', txt) is None
- matches = filter(no__name, matches)
- except NameError:
- matches = []
- except:
- None<EXCEPTION MATCH>NameError
-
-
- None<EXCEPTION MATCH>NameError
- matches = self.global_matches(text)
- if matches == [] and not text.startswith(os.sep) and ' ' not in self.lbuf:
- matches = self.attr_matches(self.magic_prefix + text)
-
- return matches
-
-
- def _default_arguments(self, obj):
- if not inspect.isfunction(obj) or inspect.ismethod(obj):
- if inspect.isclass(obj):
- if not getattr(obj, '__init__', None):
- pass
- obj = getattr(obj, '__new__', None)
- elif hasattr(obj, '__call__'):
- obj = obj.__call__
-
-
-
- try:
- (args, _, _1, defaults) = inspect.getargspec(obj)
- if defaults:
- return args[-len(defaults):]
- except TypeError:
- pass
-
- return []
-
-
- def python_func_kw_matches(self, text):
- if '.' in text:
- return []
-
- try:
- regexp = self._IPCompleter__funcParamsRegex
- except AttributeError:
- '.' in text
- '.' in text
- regexp = self._IPCompleter__funcParamsRegex = re.compile('\n \'.*?\' | # single quoted strings or\n ".*?" | # double quoted strings or\n \\w+ | # identifier\n \\S # other characters\n ', re.VERBOSE | re.DOTALL)
- except:
- '.' in text
-
- tokens = regexp.findall(self.get_line_buffer())
- tokens.reverse()
- iterTokens = iter(tokens)
- openPar = 0
- for token in iterTokens:
- if token == ')':
- openPar -= 1
- continue
- '.' in text
- if token == '(':
- openPar += 1
- if openPar > 0:
- break
-
- openPar > 0
- else:
- return []
- ids = None
- isId = re.compile('\\w+$').match
- while True:
-
- try:
- ids.append(iterTokens.next())
- if not isId(ids[-1]):
- ids.pop()
- break
-
- if not iterTokens.next() == '.':
- break
- continue
- except StopIteration:
- break
- continue
-
-
- None<EXCEPTION MATCH>StopIteration
- if len(ids) == 1:
- callableMatches = self.global_matches(ids[0])
- else:
- callableMatches = self.attr_matches('.'.join(ids[::-1]))
- argMatches = []
- for callableMatch in callableMatches:
-
- try:
- namedArgs = self._default_arguments(eval(callableMatch, self.namespace))
- except:
- continue
-
- for namedArg in namedArgs:
- if namedArg.startswith(text):
- argMatches.append('%s=' % namedArg)
- continue
-
-
- return argMatches
-
-
- def dispatch_custom_completer(self, text):
- line = self.full_lbuf
- if not line.strip():
- return None
- event = Struct()
- event.line = line
- event.symbol = text
- cmd = line.split(None, 1)[0]
- event.command = cmd
- if not cmd.startswith(self.magic_escape):
- try_magic = self.custom_completers.s_matches(self.magic_escape + cmd)
- else:
- try_magic = []
- for c in itertools.chain(self.custom_completers.s_matches(cmd), try_magic, self.custom_completers.flat_matches(self.lbuf)):
-
- try:
- res = c(event)
- withcase = _[1]
- if withcase:
- return withcase
- return _[2]
- continue
- except ipapi.TryNext:
- continue
-
-
-
-
-
- def complete(self, text, state, line_buffer = None):
- if line_buffer is None:
- self.full_lbuf = self.get_line_buffer()
- else:
- self.full_lbuf = line_buffer
- if not self.dumb_terminal or self.full_lbuf.strip():
- self.readline.insert_text('\t')
- return None
- magic_escape = self.magic_escape
- magic_prefix = self.magic_prefix
- self.lbuf = self.full_lbuf[:self.get_endidx()]
-
- try:
- if text.startswith(magic_escape):
- text = text.replace(magic_escape, magic_prefix)
- elif text.startswith('~'):
- text = os.path.expanduser(text)
-
- if state == 0:
- custom_res = self.dispatch_custom_completer(text)
- if custom_res is not None:
- self.matches = custom_res
- elif self.merge_completions:
- self.matches = []
- for matcher in self.matchers:
- self.matches.extend(matcher(text))
-
- else:
- for matcher in self.matchers:
- self.matches = matcher(text)
- if self.matches:
- break
- continue
-
-
- def uniq(alist):
- set = { }
- return _[1]
-
- self.matches = uniq(self.matches)
-
-
- try:
- ret = self.matches[state].replace(magic_prefix, magic_escape)
- return ret
- except IndexError:
- return None
-
- except:
- self.full_lbuf.strip()
- return None
-
-
-
-