本文整理汇总了Python中jedi.Script类的典型用法代码示例。如果您正苦于以下问题:Python Script类的具体用法?Python Script怎么用?Python Script使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Script类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _validate_import
def _validate_import(self, module_line, lineno):
"""Try to validate the given iport line
"""
if 'noqa' in module_line:
return True
error = []
error_string = 'can\'t import {0}'
valid = True
for word in module_line.split():
if word in ('from', 'import', 'as'):
continue
offset = int(module_line.find(word) + len(word) / 2)
s = Script(self.source, lineno, offset, self.filename)
if not self.filename:
s = Script(module_line, 1, offset)
if not s.goto_assignments():
if valid is True:
valid = False
error.append(word)
err = '' if valid else error_string.format(' '.join(error))
return err, valid
示例2: test_module_attributes
def test_module_attributes():
def_, = Script('__name__').completions()
assert def_.name == '__name__'
assert def_.line == None
assert def_.column == None
str_, = def_._goto_definitions()
assert str_.name == 'str'
示例3: feed
def feed(self):
source = self.area.get('1.0', 'end')
line = self.area.indcur()[0]
size = len(self.area.get('insert linestart', 'insert'))
script = Script(source, line, size, self.area.filename)
completions = script.completions()
for ind in completions:
self.box.insert('end', ind.name)
示例4: do_completion
def do_completion(self):
source = self.area.get("1.0", "end")
line = self.area.indcur()[0]
size = len(self.area.get("insert linestart", "insert"))
script = Script(source, line, size, self.area.filename)
completions = script.completions()
for ind in completions:
self.insert("end", ind.name)
示例5: test_completion_docstring
def test_completion_docstring():
"""
Jedi should follow imports in certain conditions
"""
c = Script('import jedi\njed').completions()[0]
assert c.docstring(fast=False) == cleandoc(jedi_doc)
c = Script('import jedi\njedi.Scr').completions()[0]
assert c.docstring(raw=True, fast=False) == cleandoc(Script.__doc__)
示例6: __init__
def __init__(self, area, *args, **kwargs):
CompleteBox.__init__(self, area, *args, **kwargs)
source = self.area.get('1.0', 'end')
line = self.area.indcur()[0]
size = len(self.area.get('insert linestart', 'insert'))
script = Script(source, line, size, self.area.filename)
completions = script.completions()
for ind in completions:
self.insert('end', ind.name)
示例7: run
def run(self, *args, **kwargs):
request = self.__request
script = Script(request.source_code, request.line, request.col,
request.filename, request.encoding)
try:
call = script.get_in_function_call()
if call:
self.resultsAvailable.signal.emit(call, request)
else:
self.failedEvent.signal.emit()
except:
self.failedEvent.signal.emit()
示例8: ComputeCandidates
def ComputeCandidates( self, unused_query, unused_start_column ):
filename = vim.current.buffer.name
line, column = vimsupport.CurrentLineAndColumn()
# Jedi expects lines to start at 1, not 0
line += 1
contents = '\n'.join( vim.current.buffer )
script = Script( contents, line, column, filename )
return [ { 'word': str( completion.word ),
'menu': str( completion.description ),
'info': str( completion.doc ) }
for completion in script.complete() ]
示例9: test_add_dynamic_mods
def test_add_dynamic_mods(self):
fname = '__main__.py'
api.settings.additional_dynamic_modules = [fname]
# Fictional module that defines a function.
src1 = "def r(a): return a"
# Other fictional modules in another place in the fs.
src2 = 'from .. import setup; setup.r(1)'
script = Script(src1, path='../setup.py')
imports.load_module(script._evaluator, os.path.abspath(fname), src2)
result = script.goto_definitions()
assert len(result) == 1
assert result[0].description == 'class int'
示例10: do_complete
def do_complete(self, data):
file_ = self.db.get_file(self.current_file)
file_ = to_unicode(file_)
lines = file_.splitlines()
lno = self.current['lno']
line_before = ''
if len(lines) >= lno:
line_before = lines[lno - 1]
indent = len(line_before) - len(line_before.lstrip())
segments = data.splitlines()
for segment in reversed(segments):
line = u(' ') * indent + segment
lines.insert(lno - 1, line)
script = Script(
u('\n').join(lines), lno - 1 + len(segments),
len(segments[-1]) + indent, '')
try:
completions = script.completions()
except Exception:
self.db.send('Suggest')
self.notify_exc('Completion failed for %s' % (
'\n'.join(reversed(segments))))
return
try:
funs = script.call_signatures() or []
except Exception:
self.db.send('Suggest')
self.notify_exc('Completion of function failed for %s' % (
'\n'.join(reversed(segments))))
return
try:
suggest_obj = {
'params': [{
'params': [p.get_code().replace('\n', '')
for p in fun.params],
'index': fun.index,
'module': fun.module.path,
'call_name': fun.call_name} for fun in funs],
'completions': [{
'base': comp.name[
:len(comp.name) - len(comp.complete)],
'complete': comp.complete,
'description': comp.description
} for comp in completions if comp.name.endswith(
comp.complete)]
}
self.db.send('Suggest|%s' % dump(suggest_obj))
except Exception:
self.db.send('Suggest')
self.notify_exc('Completion generation failed for %s' % (
'\n'.join(reversed(segments))))
示例11: test_loading_unicode_files_with_bad_global_charset
def test_loading_unicode_files_with_bad_global_charset(monkeypatch, tmpdir):
dirname = str(tmpdir.mkdir('jedi-test'))
filename1 = os.path.join(dirname, 'test1.py')
filename2 = os.path.join(dirname, 'test2.py')
if sys.version_info < (3, 0):
data = "# coding: latin-1\nfoo = 'm\xf6p'\n"
else:
data = "# coding: latin-1\nfoo = 'm\xf6p'\n".encode("latin-1")
with open(filename1, "wb") as f:
f.write(data)
s = Script("from test1 import foo\nfoo.",
line=2, column=4, path=filename2)
s.completions()
示例12: test_dict_literal_in_incomplete_call
def test_dict_literal_in_incomplete_call():
source = """\
import json
def foo():
json.loads(
json.load.return_value = {'foo': [],
'bar': True}
c = Foo()
"""
script = Script(dedent(source), line=4, column=15)
assert script.call_signatures()
示例13: __init__
class Traverser:
def __init__(self):
self._script = Script('', 1, 0, 'example.py')
self._module = self._script._get_module()
def names_in_module(self, module_name):
try:
_import_module(module_name)
except Exception as e:
pp.pprint(e)
return []
imp = Importer(
self._script._evaluator, [FakeName('.'.join(module_name))],
self._module, 0)
try:
scope_set = imp.follow()
except Exception as e:
# print('Error "{}" in {}, ignoring...'.format(e, module_name))
return []
all_names = []
for s in scope_set:
names = []
for names_dict in s.names_dicts(search_global=False):
names += chain.from_iterable(names_dict.values())
all_names += finder.filter_definition_names(names, self._module)
return all_names
示例14: test_completion_docstring
def test_completion_docstring():
"""
Jedi should follow imports in certain conditions
"""
def docstr(src, result):
c = Script(src).completions()[0]
assert c.docstring(raw=True, fast=False) == cleandoc(result)
c = Script('import jedi\njed').completions()[0]
assert c.docstring(fast=False) == cleandoc(jedi_doc)
docstr('import jedi\njedi.Scr', cleandoc(Script.__doc__))
docstr('abcd=3;abcd', '')
docstr('"hello"\nabcd=3\nabcd', '')
docstr(dedent('''
def x():
"hello"
0
x'''),
'hello'
)
docstr(dedent('''
def x():
"hello";0
x'''),
'hello'
)
# Shouldn't work with a tuple.
docstr(dedent('''
def x():
"hello",0
x'''),
''
)
# Should also not work if we rename something.
docstr(dedent('''
def x():
"hello"
y = x
y'''),
''
)
示例15: test_completion_docstring
def test_completion_docstring():
"""
Jedi should follow imports in certain conditions
"""
def docstr(src, result):
c = Script(src).completions()[0]
assert c.docstring(raw=True, fast=False) == cleandoc(result)
c = Script('import jedi\njed').completions()[0]
assert c.docstring(fast=False) == cleandoc(jedi_doc)
docstr('import jedi\njedi.Scr', cleandoc(Script.__doc__))
docstr('abcd=3;abcd', '')
docstr('"hello"\nabcd=3\nabcd', 'hello')
# It works with a ; as well.
docstr('"hello"\nabcd=3;abcd', 'hello')
# Shouldn't work with a tuple.
docstr('"hello",0\nabcd=3\nabcd', '')