Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into adrien
[simgrid.git] / docs / source / _ext / javasphinx / javasphinx / util.py
1 #
2 # Copyright 2012-2015 Bronto Software, Inc. and contributors
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 from __future__ import unicode_literals
18 from builtins import str
19
20 import logging
21 import re
22 import sys
23
24 class StringBuilder(list):
25     def build(self):
26         return str(self)
27
28     def __str__(self):
29         return ''.join(self)
30
31 class Directive(object):
32
33     def __init__(self, type, argument=''):
34         self.type = type
35         self.argument = argument
36
37         self.options = []
38         self.content = []
39
40     def add_option(self, name, value=''):
41         self.options.append((name, value))
42
43     def add_content(self, o):
44         assert o is not None
45         self.content.append(o)
46
47     def build(self):
48         doc = Document()
49         doc.add_line('.. %s:: %s' % (self.type, self.argument))
50
51         for name, value in self.options:
52             doc.add_line('   :%s: %s\n' % (name, value))
53
54         content = Document()
55
56         for obj in self.content:
57             content.add_object(obj)
58
59         doc.clear()
60         for line in content.build().splitlines():
61             doc.add_line('   ' + line)
62         doc.clear()
63
64         return doc.build()
65
66 class Document(object):
67     remove_trailing_whitespace_re = re.compile('[ \t]+$', re.MULTILINE)
68     collapse_empty_lines_re = re.compile('\n' + '{3,}', re.DOTALL)
69
70     def __init__(self):
71         self.content = []
72
73     def add_object(self, o):
74         assert o is not None
75
76         self.content.append(o)
77
78     def add(self, s):
79         self.add_object(s)
80
81     def add_line(self, s):
82         self.add(s)
83         self.add('\n')
84
85     def add_heading(self, s, t='-'):
86         self.add_line(s)
87         self.add_line(t * len(s))
88
89     def clear(self):
90         self.add('\n\n')
91
92     def build(self):
93         output = StringBuilder()
94
95         for obj in self.content:
96             if isinstance(obj, Directive):
97                 output.append('\n\n')
98                 output.append(obj.build())
99                 output.append('\n\n')
100             elif isinstance(obj, Document):
101                 output.append(obj.build())
102             else:
103                 output.append(str(obj))
104
105         output.append('\n\n')
106
107         output = str(output)
108         output = self.remove_trailing_whitespace_re.sub('', output)
109         output = self.collapse_empty_lines_re.sub('\n\n', output)
110
111         return output
112
113 def error(s, *args, **kwargs):
114     logging.error(s, *args, **kwargs)
115     sys.exit(1)
116
117 def unexpected(s, *args, **kwargs):
118     logging.exception(s, *args, **kwargs)
119     sys.exit(1)