github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/bin/make_templated_docs (about)

     1  #!/usr/bin/env python3
     2  
     3  # Copyright 2017 Intel Corporation
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  # ------------------------------------------------------------------------------
    17  
    18  import os
    19  import re
    20  import yaml
    21  from jinja2 import Environment, FileSystemLoader
    22  
    23  # Directory paths
    24  abs_path = os.path.dirname(os.path.realpath(__file__))
    25  
    26  DOCS_ABS = abs_path + '/../docs/'
    27  OUTPUT_REL = 'source/_autogen'
    28  TEMPLATE_REL = 'source/_templates'
    29  
    30  output_abs = DOCS_ABS + OUTPUT_REL
    31  template_abs = DOCS_ABS + TEMPLATE_REL
    32  
    33  # Comment chars
    34  comments = {
    35      '.rst': {'start': '.. /', 'line': ' '},
    36      '.html': {'start': '<!---', 'end': '--->'},
    37      '.md': {'start': '<!---', 'end': '--->'},
    38      '.py': {'line': '#'},
    39      '.sh': {'line': '#'},
    40      '.clj': {'line': ';'},
    41      '.cljs': {'line': ';'},
    42      'default': {'line': '//'}}
    43  
    44  # Build globals
    45  conf = yaml.load(open(template_abs + '/template_config.yaml', 'r'))
    46  env = Environment(
    47      loader=FileSystemLoader(template_abs),
    48      trim_blocks=True,
    49      lstrip_blocks=True,
    50      keep_trailing_newline=True)
    51  warning = env.get_template('partials/warning_header')
    52  
    53  # Remove old templated docs
    54  for file_rel in os.listdir(output_abs):
    55      os.unlink(os.path.join(output_abs, file_rel))
    56  
    57  # Render templates
    58  for name, target in conf['targets'].items():
    59      template = env.get_template(target['template'])
    60  
    61      extension = (re.findall('\.[^.]+$', target['template']) or [''])[0]
    62      output_path = output_abs + '/' + name + extension
    63      template_path = TEMPLATE_REL + '/' + target['template']
    64  
    65      comment_chars = comments.get(extension, comments['default'])
    66      warning.stream(
    67          start_comment=comment_chars.get('start', ''),
    68          end_comment=comment_chars.get('end', ''),
    69          line_comment=comment_chars.get('line', ''),
    70          template_path=template_path
    71      ).dump(output_path)
    72  
    73      output = open(output_path, 'a')
    74      output.write(template.render(**target.get('args', {})))
    75  
    76  print('Templated docs generated. Files located in ' + OUTPUT_REL)
    77  print()