Fix 'bad' words in Boron docs
[docs.git] / docs_autotranslation / split.py
1 #!/usr/bin/env python
2 from __future__ import print_function
3 import os
4 import shutil
5 import argparse
6
7 chapterNames = []
8
9
10 def isEquqalsSign(str):
11     # Check if this line only contains '='
12     if str.strip() == '':
13         return False
14     for ch in str:
15         if ch != '=':
16             return False
17     return True
18
19
20 def isGuideHeader(title, equalsSign):
21     if not isEquqalsSign(equalsSign.rstrip()):
22         return False
23     if len(title.rstrip()) == len(equalsSign.rstrip()):
24         return True
25     else:
26         return False
27
28
29 def getLines(lines, start, end):
30     # Get the text of one chapter
31     out = ''
32     while(start < end):
33         out += lines[start]
34         start += 1
35     return out
36
37
38 def getGuides(lines, lineNumbers):
39     output = []
40     for i in range(0, len(lineNumbers)):
41         if i != len(lineNumbers) - 1:
42             start = lineNumbers[i] - 1
43             end = lineNumbers[i + 1] - 1
44             output.append(getLines(lines, start, end))
45         else:
46             start = lineNumbers[i] - 1
47             end = len(lines)
48             output.append(getLines(lines, start, end))
49     return output
50
51
52 def printToFile(textList):
53     chapterIndex = 0
54
55     for text in textList:
56         try:
57             file = open(outputPath + '/' +
58                         chapterNames[chapterIndex] + '.rst', 'w')
59         except:
60             file = open(outputPath + '/' +
61                         chapterNames[chapterIndex] + '.rst', 'w')
62         file.write(text)
63         file.close()
64         chapterIndex += 1
65     return
66
67
68 def copyDirectory(src, dest):
69     try:
70         shutil.copytree(src, dest)
71     # Directories are the same
72     except shutil.Error as e:
73         print('Directory not copied. Error: %s' % e)
74     # Any error saying that the directory doesn't exist
75     except OSError as e:
76         print('Directory not copied. Error: %s' % e)
77
78
79 class FileWrapper(object):
80     def __init__(self, f):
81         self.f = f
82         self.line = 0
83
84     def close(self):
85         return self.f.close
86
87     def readline(self):
88         self.line += 1
89         return self.f.readline()
90
91     def readlines(self):
92         return self.f.readlines()
93
94 # Obtain command line arguments
95 parser = argparse.ArgumentParser(
96     description='Obtain input/output and image directories')
97 parser.add_argument('-i', '--input', help='Input reST file. ' +
98                     'Default="developer-guide/index.rst"',
99                     default='developer-guide/index.rst')
100 parser.add_argument('-r', '--resource', help='Image resource directory',
101                     default='argparse.SUPPRESS')
102 parser.add_argument('-o', '--output', help='Output directory',
103                     default='argparse.SUPPRESS')
104 args = parser.parse_args()
105 inputFile = args.input
106 inputPath = os.path.dirname(inputFile)
107 if not hasattr(parser, 'resource'):
108     imagePath = inputPath + '/images'
109 else:
110     imagePath = parser.resource
111 if not hasattr(parser, 'output'):
112     outputPath = inputPath + '-autogenerated'
113 else:
114     outputPath = parser.output
115
116 # Create outputPath if necessary
117 if not os.path.exists(outputPath):
118     os.makedirs(outputPath)
119
120 deGuide = FileWrapper(open(inputFile))
121
122 lineAbove = deGuide.readline()
123 line = deGuide.readline()
124
125 lineNumbers = []
126
127 while (line != ''):
128     if isGuideHeader(lineAbove, line):
129         lineNumbers.append(deGuide.line - 1)
130         chapterNames.append(lineAbove.lower().replace(' ', '-').
131                             replace(':', '_').rstrip()) # replace ':' in case sphinx builder can't process.
132     lineAbove = line
133     line = deGuide.readline()
134 deGuide.close()
135 deGuide = FileWrapper(open(inputFile))
136 lines = deGuide.readlines()
137 deGuide.close()
138 textList = getGuides(lines, lineNumbers)
139 printToFile(textList)
140
141 # Copy image path to developer-guide-autogenerated derectory
142 copyDirectory(imagePath, outputPath + '/images')
143
144 # Initialize the index.rst file
145 # 1.rename opendaylight-developer-guide.rst(title + author) to index.rst
146 # 2.write toctree info to index.rst
147 indexPath = outputPath + '/' + 'index.rst'
148 try:
149     os.rename(outputPath + '/' + chapterNames[0] + '.rst', indexPath)
150 except OSError:
151     print(chapterNames[0] + '.rst ' + 'does not exists.')
152
153 rstIndexText = open(indexPath, 'a')
154 rstIndexText.write('.. toctree:: \n')
155 rstIndexText.write('\t' + ':maxdepth: 1 \n')
156 rstIndexText.write('\n')
157 for i in range(1, len(chapterNames)):
158     rstIndexText.write('\t' + chapterNames[i] + '\n')
159 rstIndexText.close()