PEP8: fix E305: expected 2 blank lines after class or function definition, found 1
[bbaumbach/samba-autobuild/.git] / python / samba / ms_schema_markdown.py
1 # Create schema.ldif from Github markdown
2 #
3 # Each LDF section in the markdown file then gets written to a corresponding
4 # .LDF output file.
5 #
6 # Copyright (C) Andrew Bartlett 2017
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 from __future__ import print_function
22 """Generate LDIF from Github documentation."""
23
24 import re
25 import os
26 import markdown
27 import xml.etree.ElementTree as ET
28
29
30 def innertext(tag):
31     return (tag.text or '') + \
32             ''.join(innertext(e) for e in tag) + \
33             (tag.tail or '')
34
35
36 def read_ms_markdown(in_file, out_folder):
37     """Read Github documentation-derived schema files."""
38
39     with open(in_file) as update_file:
40         # Remove any comments from the raw LDF files
41         html = markdown.markdown(re.sub(r'(?m)^# .*\n?', '', update_file.read()),
42                                  output_format='xhtml')
43
44     tree = ET.fromstring('<root>' + html + '</root>')
45
46     ldf = None
47     try:
48         for node in tree:
49             if node.tag == 'h3':
50                 if ldf is not None:
51                     ldf.close()
52
53                 out_path = os.path.join(out_folder, innertext(node).strip())
54                 ldf = open(out_path, 'w')
55             elif node.tag == 'p' and ldf is not None:
56                 ldf.write(innertext(node).replace('```', '') + '\n')
57     finally:
58         if ldf is not None:
59             ldf.close()
60
61
62 if __name__ == '__main__':
63     import sys
64
65     out_folder = ''
66
67     if len(sys.argv) == 0:
68         print("Usage: %s <Schema-Update.md> [<output folder>]" % (sys.argv[0]), file=sys.stderr)
69         sys.exit(1)
70
71     in_file = sys.argv[1]
72     if len(sys.argv) > 2:
73         out_folder = sys.argv[2]
74
75     read_ms_markdown(in_file, out_folder)