source: orange/Orange/OrangeCanvas/help/intersphinx.py @ 11263:9f8fc6d26e58

Revision 11263:9f8fc6d26e58, 2.2 KB checked in by Ales Erjavec <ales.erjavec@…>, 15 months ago (diff)

Added canvas help search system.

Line 
1"""
2Parsers for intersphinx inventory files
3
4Taken from `sphinx.ext.intersphinx`
5
6"""
7
8import re
9import codecs
10import zlib
11
12b = str
13UTF8StreamReader = codecs.lookup('utf-8')[2]
14
15
16def read_inventory_v1(f, uri, join):
17    f = UTF8StreamReader(f)
18    invdata = {}
19    line = f.next()
20    projname = line.rstrip()[11:]
21    line = f.next()
22    version = line.rstrip()[11:]
23    for line in f:
24        name, type, location = line.rstrip().split(None, 2)
25        location = join(uri, location)
26        # version 1 did not add anchors to the location
27        if type == 'mod':
28            type = 'py:module'
29            location += '#module-' + name
30        else:
31            type = 'py:' + type
32            location += '#' + name
33        invdata.setdefault(type, {})[name] = (projname, version, location, '-')
34    return invdata
35
36
37def read_inventory_v2(f, uri, join, bufsize=16*1024):
38    invdata = {}
39    line = f.readline()
40    projname = line.rstrip()[11:].decode('utf-8')
41    line = f.readline()
42    version = line.rstrip()[11:].decode('utf-8')
43    line = f.readline().decode('utf-8')
44    if 'zlib' not in line:
45        raise ValueError
46
47    def read_chunks():
48        decompressor = zlib.decompressobj()
49        for chunk in iter(lambda: f.read(bufsize), b('')):
50            yield decompressor.decompress(chunk)
51        yield decompressor.flush()
52
53    def split_lines(iter):
54        buf = b('')
55        for chunk in iter:
56            buf += chunk
57            lineend = buf.find(b('\n'))
58            while lineend != -1:
59                yield buf[:lineend].decode('utf-8')
60                buf = buf[lineend+1:]
61                lineend = buf.find(b('\n'))
62        assert not buf
63
64    for line in split_lines(read_chunks()):
65        # be careful to handle names with embedded spaces correctly
66        m = re.match(r'(?x)(.+?)\s+(\S*:\S*)\s+(\S+)\s+(\S+)\s+(.*)',
67                     line.rstrip())
68        if not m:
69            continue
70        name, type, prio, location, dispname = m.groups()
71        if location.endswith(u'$'):
72            location = location[:-1] + name
73        location = join(uri, location)
74        invdata.setdefault(type, {})[name] = (projname, version,
75                                              location, dispname)
76    return invdata
Note: See TracBrowser for help on using the repository browser.