mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-15 18:17:07 +08:00
pep correct python code
This commit is contained in:
148
pyfuzzyparser.py
148
pyfuzzyparser.py
@@ -5,13 +5,15 @@ import sys
|
||||
import tokenize
|
||||
import cStringIO
|
||||
|
||||
|
||||
def indent_block(text, indention=" "):
|
||||
""" This function indents a text block with a default of four spaces """
|
||||
lines = text.split('\n')
|
||||
return '\n'.join(map(lambda s: indention+s, lines))
|
||||
return '\n'.join(map(lambda s: indention + s, lines))
|
||||
|
||||
|
||||
class Scope(object):
|
||||
def __init__(self,name,indent,docstr=''):
|
||||
def __init__(self, name, indent, docstr=''):
|
||||
self.subscopes = []
|
||||
self.locals = []
|
||||
self.imports = []
|
||||
@@ -20,29 +22,32 @@ class Scope(object):
|
||||
self.name = name
|
||||
self.indent = indent
|
||||
|
||||
def add_scope(self,sub):
|
||||
#print 'push scope: [%s@%s]' % (sub.name,sub.indent)
|
||||
def add_scope(self, sub):
|
||||
#print 'push scope: [%s@%s]' % (sub.name, sub.indent)
|
||||
sub.parent = self
|
||||
self.subscopes.append(sub)
|
||||
return sub
|
||||
|
||||
def doc(self,str):
|
||||
def doc(self, str):
|
||||
""" Clean up a docstring """
|
||||
d = str.replace('\n',' ')
|
||||
d = d.replace('\t',' ')
|
||||
while d.find(' ') > -1: d = d.replace(' ',' ')
|
||||
while d[0] in '"\'\t ': d = d[1:]
|
||||
while d[-1] in '"\'\t ': d = d[:-1]
|
||||
dbg("Scope(%s)::docstr = %s" % (self,d))
|
||||
d = str.replace('\n', ' ')
|
||||
d = d.replace('\t', ' ')
|
||||
while d.find(' ') > -1:
|
||||
d = d.replace(' ', ' ')
|
||||
while d[0] in '"\'\t ':
|
||||
d = d[1:]
|
||||
while d[-1] in '"\'\t ':
|
||||
d = d[:-1]
|
||||
dbg("Scope(%s)::docstr = %s" % (self, d))
|
||||
self.docstr = d
|
||||
|
||||
def add_local(self,loc):
|
||||
def add_local(self, loc):
|
||||
self.locals.append(loc)
|
||||
|
||||
def add_import(self, imp):
|
||||
self.imports.append(imp)
|
||||
|
||||
def _checkexisting(self,test):
|
||||
def _checkexisting(self, test):
|
||||
"Convienance function... keep out duplicates"
|
||||
if test.find('=') > -1:
|
||||
var = test.split('=')[0].strip()
|
||||
@@ -52,25 +57,26 @@ class Scope(object):
|
||||
|
||||
def get_code(self, first_indent=False, indention=" "):
|
||||
str = ""
|
||||
#str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n'
|
||||
if len(self.docstr) > 0: str += '"""'+self.docstr+'"""\n'
|
||||
if len(self.docstr) > 0:
|
||||
str += '"""' + self.docstr + '"""\n'
|
||||
for i in self.imports:
|
||||
str += i.get_code() + '\n'
|
||||
#str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n'
|
||||
for sub in self.subscopes:
|
||||
str += sub.get_code(first_indent=True, indention=indention)
|
||||
for l in self.locals:
|
||||
str += l+'\n'
|
||||
str += l + '\n'
|
||||
|
||||
if first_indent: str = indent_block(str, indention = indention)
|
||||
if first_indent:
|
||||
str = indent_block(str, indention=indention)
|
||||
return "_%s_%s" % (self.indent, str)
|
||||
|
||||
def is_empty(self):
|
||||
"""
|
||||
"""
|
||||
this function returns true if there are no subscopes, imports, locals.
|
||||
"""
|
||||
return not (self.locals, self.imports, self.subscopes)
|
||||
|
||||
|
||||
class Class(Scope):
|
||||
def __init__(self, name, supers, indent, docstr=''):
|
||||
super(Class, self).__init__(name, indent, docstr)
|
||||
@@ -78,7 +84,8 @@ class Class(Scope):
|
||||
|
||||
def get_code(self, first_indent=False, indention=" "):
|
||||
str = 'class %s' % (self.name)
|
||||
if len(self.supers) > 0: str += '(%s)' % ','.join(self.supers)
|
||||
if len(self.supers) > 0:
|
||||
str += '(%s)' % ','.join(self.supers)
|
||||
str += ':\n'
|
||||
str += super(Class, self).get_code(True, indention)
|
||||
if self.is_empty():
|
||||
@@ -88,18 +95,20 @@ class Class(Scope):
|
||||
|
||||
class Function(Scope):
|
||||
def __init__(self, name, params, indent, docstr=''):
|
||||
Scope.__init__(self,name,indent, docstr)
|
||||
Scope.__init__(self, name, indent, docstr)
|
||||
self.params = params
|
||||
|
||||
def get_code(self, first_indent=False, indention=" "):
|
||||
str = "def %s(%s):\n" % (self.name,','.join(self.params))
|
||||
#if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
|
||||
str = "def %s(%s):\n" % (self.name, ','.join(self.params))
|
||||
#if len(self.docstr) > 0:
|
||||
# str += self.childindent()+'"""'+self.docstr+'"""\n'
|
||||
str += super(Function, self).get_code(True, indention)
|
||||
if self.is_empty():
|
||||
str += indent_block("pass\n", indention=indention)
|
||||
print "func", self.locals
|
||||
return str
|
||||
|
||||
|
||||
class Import(object):
|
||||
"""
|
||||
stores the imports of class files
|
||||
@@ -128,29 +137,33 @@ class Import(object):
|
||||
else:
|
||||
return "test import " + ns_str
|
||||
|
||||
|
||||
class PyFuzzyParser(object):
|
||||
"""
|
||||
This class is used to parse a Python file, it then divides them into a
|
||||
class structure of differnt scopes.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.top = Scope('global',0)
|
||||
self.top = Scope('global', 0)
|
||||
self.scope = self.top
|
||||
|
||||
def _parsedotname(self,pre=None):
|
||||
def _parsedotname(self, pre=None):
|
||||
#returns (dottedname, nexttoken)
|
||||
name = []
|
||||
if pre is None:
|
||||
tokentype, tok, indent = self.next()
|
||||
if tokentype != tokenize.NAME and tok != '*':
|
||||
return ('', tok)
|
||||
else: tok = pre
|
||||
else:
|
||||
tok = pre
|
||||
name.append(tok)
|
||||
while True:
|
||||
tokentype, tok, indent = self.next()
|
||||
if tok != '.': break
|
||||
if tok != '.':
|
||||
break
|
||||
tokentype, tok, indent = self.next()
|
||||
if tokentype != tokenize.NAME: break
|
||||
if tokentype != tokenize.NAME:
|
||||
break
|
||||
name.append(tok)
|
||||
return (".".join(name), tok)
|
||||
|
||||
@@ -158,13 +171,16 @@ class PyFuzzyParser(object):
|
||||
imports = []
|
||||
while True:
|
||||
name, tok = self._parsedotname()
|
||||
if not name: break
|
||||
if not name:
|
||||
break
|
||||
name2 = ''
|
||||
if tok == 'as': name2, tok = self._parsedotname()
|
||||
if tok == 'as':
|
||||
name2, tok = self._parsedotname()
|
||||
imports.append((name, name2))
|
||||
while tok != "," and "\n" not in tok:
|
||||
tokentype, tok, indent = self.next()
|
||||
if tok != ",": break
|
||||
if tok != ",":
|
||||
break
|
||||
return imports
|
||||
|
||||
def _parseparen(self):
|
||||
@@ -174,7 +190,8 @@ class PyFuzzyParser(object):
|
||||
while True:
|
||||
tokentype, tok, indent = self.next()
|
||||
if tok in (')', ',') and level == 1:
|
||||
if '=' not in name: name = name.replace(' ', '')
|
||||
if '=' not in name:
|
||||
name = name.replace(' ', '')
|
||||
names.append(name.strip())
|
||||
name = ''
|
||||
if tok == '(':
|
||||
@@ -182,8 +199,10 @@ class PyFuzzyParser(object):
|
||||
name += "("
|
||||
elif tok == ')':
|
||||
level -= 1
|
||||
if level == 0: break
|
||||
else: name += ")"
|
||||
if level == 0:
|
||||
break
|
||||
else:
|
||||
name += ")"
|
||||
elif tok == ',' and level == 1:
|
||||
pass
|
||||
else:
|
||||
@@ -192,31 +211,36 @@ class PyFuzzyParser(object):
|
||||
|
||||
def _parsefunction(self, indent):
|
||||
tokentype, fname, ind = self.next()
|
||||
if tokentype != tokenize.NAME: return None
|
||||
if tokentype != tokenize.NAME:
|
||||
return None
|
||||
|
||||
tokentype, open, ind = self.next()
|
||||
if open != '(': return None
|
||||
params=self._parseparen()
|
||||
if open != '(':
|
||||
return None
|
||||
params = self._parseparen()
|
||||
|
||||
tokentype, colon, ind = self.next()
|
||||
if colon != ':': return None
|
||||
if colon != ':':
|
||||
return None
|
||||
|
||||
return Function(fname, params, indent)
|
||||
|
||||
def _parseclass(self, indent):
|
||||
tokentype, cname, ind = self.next()
|
||||
if tokentype != tokenize.NAME: return None
|
||||
if tokentype != tokenize.NAME:
|
||||
return None
|
||||
|
||||
super = []
|
||||
tokentype, next, ind = self.next()
|
||||
if next == '(':
|
||||
super=self._parseparen()
|
||||
elif next != ':': return None
|
||||
super = self._parseparen()
|
||||
elif next != ':':
|
||||
return None
|
||||
|
||||
return Class(cname,super,indent)
|
||||
return Class(cname, super, indent)
|
||||
|
||||
def _parseassignment(self):
|
||||
assign=''
|
||||
assign = ''
|
||||
tokentype, tok, indent = self.next()
|
||||
if tokentype == tokenize.STRING or tok == 'str':
|
||||
return '""'
|
||||
@@ -233,41 +257,42 @@ class PyFuzzyParser(object):
|
||||
elif tok == 'None':
|
||||
return '_PyCmplNoType()'
|
||||
elif tok == 'type':
|
||||
return 'type(_PyCmplNoType)' #only for method resolution
|
||||
return 'type(_PyCmplNoType)' # only for method resolution
|
||||
else:
|
||||
assign += tok
|
||||
level = 0
|
||||
while True:
|
||||
tokentype, tok, indent = self.next()
|
||||
if tok in ('(','{','['):
|
||||
if tok in ('(', '{', '['):
|
||||
level += 1
|
||||
elif tok in (']','}',')'):
|
||||
elif tok in (']', '}', ')'):
|
||||
level -= 1
|
||||
if level == 0: break
|
||||
if level == 0:
|
||||
break
|
||||
elif level == 0:
|
||||
if tok in (';','\n'): break
|
||||
if tok in (';', '\n'):
|
||||
break
|
||||
assign += tok
|
||||
return "%s" % assign
|
||||
|
||||
def next(self):
|
||||
type, tok, (lineno, indent), end, self.parserline = self.gen.next()
|
||||
if lineno == self.curline:
|
||||
#print 'line found [%s] scope=%s' % (line.replace('\n',''),self.scope.name)
|
||||
self.currentscope = self.scope
|
||||
return (type, tok, indent)
|
||||
|
||||
#p.parse(vim.current.buffer[:],vim.eval("line('.')"))
|
||||
def parse(self,text,curline=0):
|
||||
#p.parse(vim.current.buffer[:], vim.eval("line('.')"))
|
||||
def parse(self, text, curline=0):
|
||||
self.curline = int(curline)
|
||||
buf = cStringIO.StringIO(''.join(text) + '\n')
|
||||
self.gen = tokenize.generate_tokens(buf.readline)
|
||||
self.currentscope = self.scope
|
||||
|
||||
try:
|
||||
freshscope=True
|
||||
freshscope = True
|
||||
while True:
|
||||
tokentype, tok, indent = self.next()
|
||||
dbg( 'main: tok=[%s] indent=[%s]' % (tok,indent))
|
||||
dbg('main: tok=[%s] indent=[%s]' % (tok, indent))
|
||||
|
||||
if tokentype == tokenize.DEDENT:
|
||||
self.scope = self.scope.parent
|
||||
@@ -302,33 +327,36 @@ class PyFuzzyParser(object):
|
||||
self.scope.add_import(Import(name, alias, mod))
|
||||
freshscope = False
|
||||
elif tokentype == tokenize.STRING:
|
||||
if freshscope: self.scope.doc(tok)
|
||||
if freshscope:
|
||||
self.scope.doc(tok)
|
||||
elif tokentype == tokenize.NAME:
|
||||
name,tok = self._parsedotname(tok)
|
||||
name, tok = self._parsedotname(tok)
|
||||
if tok == '=':
|
||||
stmt = self._parseassignment()
|
||||
dbg("parseassignment: %s = %s" % (name, stmt))
|
||||
if stmt != None:
|
||||
self.scope.add_local("%s = %s" % (name,stmt))
|
||||
self.scope.add_local("%s = %s" % (name, stmt))
|
||||
freshscope = False
|
||||
except StopIteration: #thrown on EOF
|
||||
except StopIteration: # thrown on EOF
|
||||
pass
|
||||
#except:
|
||||
# dbg("parse error: %s, %s @ %s" %
|
||||
# (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
|
||||
return self.top #self._adjustvisibility()
|
||||
return self.top # self._adjustvisibility()
|
||||
|
||||
|
||||
def _sanitize(str):
|
||||
val = ''
|
||||
level = 0
|
||||
for c in str:
|
||||
if c in ('(','{','['):
|
||||
if c in ('(', '{', '['):
|
||||
level += 1
|
||||
elif c in (']','}',')'):
|
||||
elif c in (']', '}', ')'):
|
||||
level -= 1
|
||||
elif level == 0:
|
||||
val += c
|
||||
return val
|
||||
|
||||
|
||||
def dbg(*args):
|
||||
print args
|
||||
|
||||
Reference in New Issue
Block a user