Blame view

3rdparty/boost_1_81_0/tools/litre/litre.py 1.86 KB
66d54af3   Hu Chunming   提交_GLIBCXX_USE_CX...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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()