Skip to content

Commit 9d90149

Browse files
committed
add jedi 0.10.0 #496
1 parent a9d82b9 commit 9d90149

60 files changed

Lines changed: 15411 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Jedi is a static analysis tool for Python that can be used in IDEs/editors. Its
3+
historic focus is autocompletion, but does static analysis for now as well.
4+
Jedi is fast and is very well tested. It understands Python on a deeper level
5+
than all other static analysis frameworks for Python.
6+
7+
Jedi has support for two different goto functions. It's possible to search for
8+
related names and to list all names in a Python file and infer them. Jedi
9+
understands docstrings and you can use Jedi autocompletion in your REPL as
10+
well.
11+
12+
Jedi uses a very simple API to connect with IDE's. There's a reference
13+
implementation as a `VIM-Plugin <https://github.com/davidhalter/jedi-vim>`_,
14+
which uses Jedi's autocompletion. We encourage you to use Jedi in your IDEs.
15+
It's really easy.
16+
17+
To give you a simple example how you can use the Jedi library, here is an
18+
example for the autocompletion feature:
19+
20+
>>> import jedi
21+
>>> source = '''
22+
... import datetime
23+
... datetime.da'''
24+
>>> script = jedi.Script(source, 3, len('datetime.da'), 'example.py')
25+
>>> script
26+
<Script: 'example.py'>
27+
>>> completions = script.completions()
28+
>>> completions #doctest: +ELLIPSIS
29+
[<Completion: date>, <Completion: datetime>, ...]
30+
>>> print(completions[0].complete)
31+
te
32+
>>> print(completions[0].name)
33+
date
34+
35+
As you see Jedi is pretty simple and allows you to concentrate on writing a
36+
good text editor, while still having very good IDE features for Python.
37+
"""
38+
39+
__version__ = '0.10.0'
40+
41+
from jedi.api import Script, Interpreter, NotFoundError, set_debug_function
42+
from jedi.api import preload_module, defined_names, names
43+
from jedi import settings
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
from os.path import join, dirname, abspath, isdir
3+
4+
5+
def _start_linter():
6+
"""
7+
This is a pre-alpha API. You're not supposed to use it at all, except for
8+
testing. It will very likely change.
9+
"""
10+
import jedi
11+
12+
if '--debug' in sys.argv:
13+
jedi.set_debug_function()
14+
15+
for path in sys.argv[2:]:
16+
if path.startswith('--'):
17+
continue
18+
if isdir(path):
19+
import fnmatch
20+
import os
21+
22+
paths = []
23+
for root, dirnames, filenames in os.walk(path):
24+
for filename in fnmatch.filter(filenames, '*.py'):
25+
paths.append(os.path.join(root, filename))
26+
else:
27+
paths = [path]
28+
29+
try:
30+
for path in paths:
31+
for error in jedi.Script(path=path)._analysis():
32+
print(error)
33+
except Exception:
34+
if '--pdb' in sys.argv:
35+
import traceback
36+
traceback.print_exc()
37+
import pdb
38+
pdb.post_mortem()
39+
else:
40+
raise
41+
42+
43+
if len(sys.argv) == 2 and sys.argv[1] == 'repl':
44+
# don't want to use __main__ only for repl yet, maybe we want to use it for
45+
# something else. So just use the keyword ``repl`` for now.
46+
print(join(dirname(abspath(__file__)), 'api', 'replstartup.py'))
47+
elif len(sys.argv) > 1 and sys.argv[1] == 'linter':
48+
_start_linter()
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
"""
2+
To ensure compatibility from Python ``2.6`` - ``3.3``, a module has been
3+
created. Clearly there is huge need to use conforming syntax.
4+
"""
5+
import sys
6+
import imp
7+
import os
8+
import re
9+
import pkgutil
10+
try:
11+
import importlib
12+
except ImportError:
13+
pass
14+
15+
is_py3 = sys.version_info[0] >= 3
16+
is_py33 = is_py3 and sys.version_info.minor >= 3
17+
is_py34 = is_py3 and sys.version_info.minor >= 4
18+
is_py35 = is_py3 and sys.version_info.minor >= 5
19+
is_py26 = not is_py3 and sys.version_info[1] < 7
20+
21+
22+
class DummyFile(object):
23+
def __init__(self, loader, string):
24+
self.loader = loader
25+
self.string = string
26+
27+
def read(self):
28+
return self.loader.get_source(self.string)
29+
30+
def close(self):
31+
del self.loader
32+
33+
34+
def find_module_py33(string, path=None):
35+
loader = importlib.machinery.PathFinder.find_module(string, path)
36+
37+
if loader is None and path is None: # Fallback to find builtins
38+
try:
39+
loader = importlib.find_loader(string)
40+
except ValueError as e:
41+
# See #491. Importlib might raise a ValueError, to avoid this, we
42+
# just raise an ImportError to fix the issue.
43+
raise ImportError("Originally " + repr(e))
44+
45+
if loader is None:
46+
raise ImportError("Couldn't find a loader for {0}".format(string))
47+
48+
try:
49+
is_package = loader.is_package(string)
50+
if is_package:
51+
if hasattr(loader, 'path'):
52+
module_path = os.path.dirname(loader.path)
53+
else:
54+
# At least zipimporter does not have path attribute
55+
module_path = os.path.dirname(loader.get_filename(string))
56+
if hasattr(loader, 'archive'):
57+
module_file = DummyFile(loader, string)
58+
else:
59+
module_file = None
60+
else:
61+
module_path = loader.get_filename(string)
62+
module_file = DummyFile(loader, string)
63+
except AttributeError:
64+
# ExtensionLoader has not attribute get_filename, instead it has a
65+
# path attribute that we can use to retrieve the module path
66+
try:
67+
module_path = loader.path
68+
module_file = DummyFile(loader, string)
69+
except AttributeError:
70+
module_path = string
71+
module_file = None
72+
finally:
73+
is_package = False
74+
75+
if hasattr(loader, 'archive'):
76+
module_path = loader.archive
77+
78+
return module_file, module_path, is_package
79+
80+
81+
def find_module_pre_py33(string, path=None):
82+
try:
83+
module_file, module_path, description = imp.find_module(string, path)
84+
module_type = description[2]
85+
return module_file, module_path, module_type is imp.PKG_DIRECTORY
86+
except ImportError:
87+
pass
88+
89+
if path is None:
90+
path = sys.path
91+
for item in path:
92+
loader = pkgutil.get_importer(item)
93+
if loader:
94+
try:
95+
loader = loader.find_module(string)
96+
if loader:
97+
is_package = loader.is_package(string)
98+
is_archive = hasattr(loader, 'archive')
99+
try:
100+
module_path = loader.get_filename(string)
101+
except AttributeError:
102+
# fallback for py26
103+
try:
104+
module_path = loader._get_filename(string)
105+
except AttributeError:
106+
continue
107+
if is_package:
108+
module_path = os.path.dirname(module_path)
109+
if is_archive:
110+
module_path = loader.archive
111+
file = None
112+
if not is_package or is_archive:
113+
file = DummyFile(loader, string)
114+
return (file, module_path, is_package)
115+
except ImportError:
116+
pass
117+
raise ImportError("No module named {0}".format(string))
118+
119+
120+
find_module = find_module_py33 if is_py33 else find_module_pre_py33
121+
find_module.__doc__ = """
122+
Provides information about a module.
123+
124+
This function isolates the differences in importing libraries introduced with
125+
python 3.3 on; it gets a module name and optionally a path. It will return a
126+
tuple containin an open file for the module (if not builtin), the filename
127+
or the name of the module if it is a builtin one and a boolean indicating
128+
if the module is contained in a package.
129+
"""
130+
131+
132+
# unicode function
133+
try:
134+
unicode = unicode
135+
except NameError:
136+
unicode = str
137+
138+
if is_py3:
139+
u = lambda s: s
140+
else:
141+
u = lambda s: s.decode('utf-8')
142+
143+
u.__doc__ = """
144+
Decode a raw string into unicode object. Do nothing in Python 3.
145+
"""
146+
147+
# exec function
148+
if is_py3:
149+
def exec_function(source, global_map):
150+
exec(source, global_map)
151+
else:
152+
eval(compile("""def exec_function(source, global_map):
153+
exec source in global_map """, 'blub', 'exec'))
154+
155+
# re-raise function
156+
if is_py3:
157+
def reraise(exception, traceback):
158+
raise exception.with_traceback(traceback)
159+
else:
160+
eval(compile("""
161+
def reraise(exception, traceback):
162+
raise exception, None, traceback
163+
""", 'blub', 'exec'))
164+
165+
reraise.__doc__ = """
166+
Re-raise `exception` with a `traceback` object.
167+
168+
Usage::
169+
170+
reraise(Exception, sys.exc_info()[2])
171+
172+
"""
173+
174+
class Python3Method(object):
175+
def __init__(self, func):
176+
self.func = func
177+
178+
def __get__(self, obj, objtype):
179+
if obj is None:
180+
return lambda *args, **kwargs: self.func(*args, **kwargs)
181+
else:
182+
return lambda *args, **kwargs: self.func(obj, *args, **kwargs)
183+
184+
185+
def use_metaclass(meta, *bases):
186+
""" Create a class with a metaclass. """
187+
if not bases:
188+
bases = (object,)
189+
return meta("HackClass", bases, {})
190+
191+
192+
try:
193+
encoding = sys.stdout.encoding
194+
if encoding is None:
195+
encoding = 'utf-8'
196+
except AttributeError:
197+
encoding = 'ascii'
198+
199+
200+
def u(string):
201+
"""Cast to unicode DAMMIT!
202+
Written because Python2 repr always implicitly casts to a string, so we
203+
have to cast back to a unicode (and we now that we always deal with valid
204+
unicode, because we check that in the beginning).
205+
"""
206+
if is_py3:
207+
return str(string)
208+
elif not isinstance(string, unicode):
209+
return unicode(str(string), 'UTF-8')
210+
return string
211+
212+
try:
213+
import builtins # module name in python 3
214+
except ImportError:
215+
import __builtin__ as builtins
216+
217+
218+
import ast
219+
220+
221+
def literal_eval(string):
222+
# py3.0, py3.1 and py32 don't support unicode literals. Support those, I
223+
# don't want to write two versions of the tokenizer.
224+
if is_py3 and sys.version_info.minor < 3:
225+
if re.match('[uU][\'"]', string):
226+
string = string[1:]
227+
return ast.literal_eval(string)
228+
229+
230+
try:
231+
from itertools import zip_longest
232+
except ImportError:
233+
from itertools import izip_longest as zip_longest # Python 2
234+
235+
236+
def no_unicode_pprint(dct):
237+
"""
238+
Python 2/3 dict __repr__ may be different, because of unicode differens
239+
(with or without a `u` prefix). Normally in doctests we could use `pprint`
240+
to sort dicts and check for equality, but here we have to write a separate
241+
function to do that.
242+
"""
243+
import pprint
244+
s = pprint.pformat(dct)
245+
print(re.sub("u'", "'", s))
246+
247+
248+
def utf8_repr(func):
249+
"""
250+
``__repr__`` methods in Python 2 don't allow unicode objects to be
251+
returned. Therefore cast them to utf-8 bytes in this decorator.
252+
"""
253+
def wrapper(self):
254+
result = func(self)
255+
if isinstance(result, unicode):
256+
return result.encode('utf-8')
257+
else:
258+
return result
259+
260+
if is_py3:
261+
return func
262+
else:
263+
return wrapper

0 commit comments

Comments
 (0)