~pygame/pygame/trunk

« back to all changes in this revision

Viewing changes to docs/reST/ext/utils.py

  • Committer: pygame
  • Date: 2017-01-10 00:31:42 UTC
  • Revision ID: git-v1:2eea4f299a2e791f884608d7ed601558634af73c
commit 1639c41a8cb3433046882ede92c80ce69d59016b
Author: Thomas Kluyver <takowl@gmail.com>
Date:   Sun Jan 8 18:46:46 2017 +0000

    Build newer versions of libogg and libvorbis into Linux base images

    Closes #317
    Closes #323

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import docutils.nodes
 
2
import sphinx.addnodes
 
3
 
 
4
import sys
 
5
 
 
6
 
 
7
class GetError(LookupError):
 
8
    pass
 
9
 
 
10
def get_fullname(node):
 
11
    if isinstance(node, docutils.nodes.section):
 
12
        return get_sectionname(node)
 
13
    if isinstance(node, sphinx.addnodes.desc):
 
14
        return get_descname(node)
 
15
    raise TypeError("Unrecognized node type '%s'" % (node.__class__,))
 
16
 
 
17
def get_descname(desc):
 
18
    try:
 
19
        sig = desc[0]
 
20
    except IndexError:
 
21
        raise GetError("No fullname: missing children in desc")
 
22
    try:
 
23
        names = sig['names']
 
24
    except KeyError:
 
25
        raise GetError(
 
26
            "No fullname: missing names attribute in desc's child")
 
27
    try:
 
28
        return names[0]
 
29
    except IndexError:
 
30
        raise GetError("No fullname: desc's child has empty names list")
 
31
 
 
32
def get_sectionname(section):
 
33
    try:
 
34
        names = section['names']
 
35
    except KeyError:
 
36
        raise GetError("No fullname: missing names attribute in section")
 
37
    try:
 
38
        return names[0]
 
39
    except IndexError:
 
40
        raise GetError("No fullname: section has empty names list")
 
41
 
 
42
def get_refuri(node):
 
43
    return as_refuri(get_refid(node))
 
44
 
 
45
def get_refid(node):
 
46
    try:
 
47
        return get_ids(node)[0]
 
48
    except IndexError:
 
49
        raise GetError("Node has emtpy ids list")
 
50
 
 
51
def as_refid(refuri):
 
52
    return refuri[1:]
 
53
 
 
54
def as_refuri(refid):
 
55
    return NUMBERSIGN + refid
 
56
 
 
57
def get_ids(node):
 
58
    if isinstance(node, docutils.nodes.section):
 
59
        try:
 
60
            return node['ids']
 
61
        except KeyError:
 
62
            raise GetError("No ids: section missing ids attribute")
 
63
    if isinstance(node, sphinx.addnodes.desc):
 
64
        try:
 
65
            sig = node[0]
 
66
        except IndexError:
 
67
            raise GetError("No ids: missing desc children")
 
68
        try:
 
69
            return sig['ids']
 
70
        except KeyError:
 
71
            raise GetError("No ids: desc's child missing ids attribute")
 
72
    raise TypeError("Unrecognized node type '%s'" % (node.__class__,))
 
73
 
 
74
def isections(doctree):
 
75
    for node in doctree:
 
76
        if isinstance(node, docutils.nodes.section):
 
77
            yield node
 
78
 
 
79
def get_name(fullname):
 
80
    return fullname.split('.')[-1]
 
81
 
 
82
def geterror ():
 
83
    return sys.exc_info()[1]
 
84
 
 
85
try:
 
86
    _unicode = unicode
 
87
except NameError:
 
88
    _unicode = str
 
89
 
 
90
# Represent escaped bytes and strings in a portable way.
 
91
#
 
92
# as_bytes: Allow a Python 3.x string to represent a bytes object.
 
93
#   e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
 
94
#         as_bytes("a\x01\b") == "a\x01b"  # Python 2.x
 
95
# as_unicode: Allow a Python "r" string to represent a unicode string.
 
96
#   e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
 
97
#         as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes"  # Python 3.x
 
98
if sys.version_info < (3,):
 
99
    def as_bytes(string):
 
100
        """ '<binary literal>' => '<binary literal>' """
 
101
        return string
 
102
        
 
103
    def as_unicode(rstring):
 
104
        """ r'<Unicode literal>' => u'<Unicode literal>' """
 
105
        return rstring.decode('unicode_escape', 'strict')
 
106
        
 
107
else:
 
108
    def as_bytes(string):
 
109
        """ '<binary literal>' => b'<binary literal>' """
 
110
        return string.encode('latin-1', 'strict')
 
111
        
 
112
    def as_unicode(rstring):
 
113
        """ r'<Unicode literal>' => '<Unicode literal>' """
 
114
        return rstring.encode('ascii', 'strict').decode('unicode_escape',
 
115
                                                        'stict')
 
116
 
 
117
# Ensure Visitor is a new-style class
 
118
_SparseNodeVisitor = docutils.nodes.SparseNodeVisitor
 
119
if not hasattr(_SparseNodeVisitor, '__class__'):
 
120
    class _SparseNodeVisitor(object, docutils.nodes.SparseNodeVisitor):
 
121
        pass
 
122
 
 
123
class Visitor(_SparseNodeVisitor):
 
124
 
 
125
    skip_node = docutils.nodes.SkipNode()
 
126
    skip_departure = docutils.nodes.SkipDeparture()
 
127
 
 
128
    def __init__(self, app, document_node):
 
129
        docutils.nodes.SparseNodeVisitor.__init__(self, document_node)
 
130
        self.app = app
 
131
        self.env = app.builder.env
 
132
 
 
133
    def unknown_visit(self, node):
 
134
        return
 
135
 
 
136
    def unknown_departure(self, node):
 
137
        return
 
138
 
 
139
EMPTYSTR = as_unicode('')
 
140
NUMBERSIGN = as_unicode('#')