mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 23:04:48 +08:00
builtin mixins support (functions custom defined)
This commit is contained in:
46
builtin.py
46
builtin.py
@@ -101,6 +101,32 @@ class Parser(CachedModule):
|
|||||||
def _get_source(self):
|
def _get_source(self):
|
||||||
return self._generate_code(self.module)
|
return self._generate_code(self.module)
|
||||||
|
|
||||||
|
def _load_mixins(self):
|
||||||
|
self.mixin_funcs = {}
|
||||||
|
if not self.path:
|
||||||
|
try:
|
||||||
|
f = open(os.path.sep.join(['mixin', self.name]) + '.py')
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
code = f.read()
|
||||||
|
# TODO implement classes, how? not used yet
|
||||||
|
regex = r'^(def|class)\s+([\w\d]+)'
|
||||||
|
matches = list(re.finditer(regex, code, re.MULTILINE))
|
||||||
|
positions = [m.start() for m in matches]
|
||||||
|
for i, pos in enumerate(positions):
|
||||||
|
try:
|
||||||
|
code_block = code[pos:positions[i+1]]
|
||||||
|
except IndexError:
|
||||||
|
code_block = code[pos:len(code)]
|
||||||
|
structure_name = matches[i].group(1)
|
||||||
|
name = matches[i].group(2)
|
||||||
|
if structure_name == 'def':
|
||||||
|
self.mixin_funcs[name] = code_block
|
||||||
|
else:
|
||||||
|
raise NotImplementedError
|
||||||
|
print code_block
|
||||||
|
|
||||||
def _generate_code(self, scope, depth=0):
|
def _generate_code(self, scope, depth=0):
|
||||||
"""
|
"""
|
||||||
Generate a string, which uses python syntax as an input to the
|
Generate a string, which uses python syntax as an input to the
|
||||||
@@ -127,6 +153,9 @@ class Parser(CachedModule):
|
|||||||
stmts[n] = exe
|
stmts[n] = exe
|
||||||
return classes, funcs, stmts, members
|
return classes, funcs, stmts, members
|
||||||
|
|
||||||
|
if depth == 0:
|
||||||
|
self._load_mixins()
|
||||||
|
|
||||||
code = ''
|
code = ''
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
@@ -154,10 +183,21 @@ class Parser(CachedModule):
|
|||||||
# functions
|
# functions
|
||||||
for name, func in funcs.iteritems():
|
for name, func in funcs.iteritems():
|
||||||
params, ret = parse_function_doc(func)
|
params, ret = parse_function_doc(func)
|
||||||
|
doc_str = parsing.indent_block('"""\n%s\n"""\n' % func.__doc__)
|
||||||
|
try:
|
||||||
|
mixin = self.mixin_funcs[name]
|
||||||
|
except:
|
||||||
|
# normal code generation
|
||||||
code += 'def %s(%s):\n' % (name, params)
|
code += 'def %s(%s):\n' % (name, params)
|
||||||
block = '"""\n%s\n"""\n' % func.__doc__
|
code += doc_str
|
||||||
block += '%s\n\n' % ret
|
code += parsing.indent_block('%s\n\n' % ret)
|
||||||
code += parsing.indent_block(block)
|
else:
|
||||||
|
# generation of code with mixins
|
||||||
|
# find doc_str place
|
||||||
|
pos = re.search(r'\):\s*\n', mixin).end()
|
||||||
|
if pos is None:
|
||||||
|
raise Exception("Builtin function not parsed correctly")
|
||||||
|
code += mixin[:pos] + doc_str + mixin[pos:]
|
||||||
|
|
||||||
# class members (functions)
|
# class members (functions)
|
||||||
for name, func in members.iteritems():
|
for name, func in members.iteritems():
|
||||||
|
|||||||
7
mixin/__builtin__.py
Normal file
7
mixin/__builtin__.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
def next(iterator, default=None):
|
||||||
|
if hasattr("next"):
|
||||||
|
return iterator.next()
|
||||||
|
else:
|
||||||
|
return iterator.__next__()
|
||||||
|
return default
|
||||||
Reference in New Issue
Block a user