diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/boost/tools/litre/litre.py | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/tools/litre/litre.py')
-rw-r--r-- | src/boost/tools/litre/litre.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/boost/tools/litre/litre.py b/src/boost/tools/litre/litre.py new file mode 100644 index 000000000..c0ac67e28 --- /dev/null +++ b/src/boost/tools/litre/litre.py @@ -0,0 +1,61 @@ +from docutils import writers +from docutils import nodes + +class LitreTranslator(nodes.GenericNodeVisitor): + + def __init__(self, document, config): + nodes.GenericNodeVisitor.__init__(self,document) + self._config = config + + def default_visit(self, node): + pass + # print '**visiting:', repr(node) + + def default_departure(self, node): + pass + # print '**departing:', repr(node) + + def visit_raw(self, node): + if node.has_key('format'): + key = node['format'].lower() + if key == 'litre': + # This is probably very evil ;-) + #if node.has_key('source'): + # node.file = node.attributes['source'] + + self._handle_code(node, node.astext()) + + raise nodes.SkipNode + + def visit_comment(self, node): + code = node.astext() + if code[0] == '@': + self._handle_code(node, code[1:].strip()) + + def _handle_code(self, node, code): + start_line = node.line or 0 + start_line -= code.count('\n') + 2 # docutils bug workaround? + try: + self._execute(compile( start_line*'\n' + code, str(node.source), 'exec')) + except: + print '\n------- begin offending Python source -------' + print code + print '------- end offending Python source -------' + raise + + def _execute(self, code): + """Override this to set up local variable context for code before + invoking it + """ + eval(code) + +class Writer(writers.Writer): + translator = LitreTranslator + _config = None + + def translate(self): + visitor = self.translator(self.document, self._config) + self.document.walkabout(visitor) + self.output = visitor.astext() + + |