davidhalter / jedi / 603

Build Details

Build Changes

50.0% jedi/keywords.py

Previous Version (Build #601)

1
from __future__ import with_statement
!
2

3
import pydoc
!
4
import keyword
!
5

6
from jedi._compatibility import is_py3k
!
7
from jedi import common
!
8
import builtin
!
9

10
try:
!
11
    from pydoc_data import topics as pydoc_topics
!
12
except ImportError:
!
13
    # Python 2.6
14
    try:
!
15
        import pydoc_topics
!
16
    except ImportError:
!
17
        # Python 2.5
18
        pydoc_topics = None
!
19

20
if is_py3k:
!
21
    keys = keyword.kwlist
!
22
else:
23
    keys = keyword.kwlist + ['None', 'False', 'True']
!
24

25

26
def get_keywords(string='', pos=(0, 0), all=False):
!
27
    if all:
1×
28
        return set([Keyword(k, pos) for k in keys])
1×
29
    if string in keys:
1×
30
        return set([Keyword(string, pos)])
1×
31
    return set()
1×
32

33

34
def get_operator(string, pos):
!
35
    return Keyword(string, pos)
1×
36

37

38
class Keyword(object):
!
39
    def __init__(self, name, pos):
!
40
        self.name = name
1×
41
        self.start_pos = pos
1×
42
        self.parent = builtin.Builtin.scope
1×
43

44
    def get_parent_until(self):
!
45
        return self.parent
1×
46

47
    @property
!
48
    def names(self):
49
        """ For a `parsing.Name` like comparision """
50
        return [self.name]
1×
51

52
    @property
!
53
    def docstr(self):
54
        return imitate_pydoc(self.name)
1×
55

56
    def __repr__(self):
57
        return '<%s: %s>' % (type(self).__name__, self.name)
58

59

60
def imitate_pydoc(string):
!
61
    """
62
    It's not possible to get the pydoc's without starting the annoying pager
63
    stuff.
64
    """
65
    # str needed because of possible unicode stuff in py2k (pydoc doesn't work
66
    # with unicode strings)
67
    string = str(string)
1×
68
    h = pydoc.help
1×
69
    with common.ignored(KeyError):
1×
70
        # try to access symbols
71
        string = h.symbols[string]
1×
72
        string, _, related = string.partition(' ')
1×
73

74
    get_target = lambda s: h.topics.get(s, h.keywords.get(s))
1×
75
    while isinstance(string, str):
1×
76
        string = get_target(string)
1×
77

78
    try:
1×
79
        # is a tuple now
80
        label, related = string
1×
81
    except TypeError:
!
82
        return ''
!
83

84
    try:
1×
85
        return pydoc_topics.topics[label] if pydoc_topics else ''
1×
86
    except KeyError:
1×
87
        return ''
1×