Skip to content

Commit

Permalink
TriBITSPub#386: - trying to avoid duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinwrobel1986 committed Jun 30, 2021
1 parent a042588 commit fdf13c6
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions tribits/doc/sphinx/sphinx_rst_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


class SphinxRstGenerator:
""" Changes include paths to relative to Sphinx build dir. Saves three main .rst docs files inside Sphinx dir.
"""
def __init__(self):
self.paths = {
'mainteiners_guide': {
Expand All @@ -28,6 +30,7 @@ def __init__(self):
'src_path': os.path.join(doc_path, 'build_ref'),
'final_path': os.path.join(doc_path, 'sphinx', 'build_ref_rst.rst')}}
self.sphinx_path = os.path.abspath(os.path.join(doc_path, 'sphinx'))
self.already_modified_files = set()
self.build_docs()

@staticmethod
Expand All @@ -53,26 +56,17 @@ def save_rst(file_path: str, file_content: str) -> None:
with open(file_path, 'w') as dest_file:
dest_file.write(file_content)

@staticmethod
def get_src_path_from_parent() -> str:
pass

def generate_rst(self, source_file: str, final_path: str = None, src_path: str = None,
start_path: str = None) -> list:
start_path: str = None) -> set:
""" Generate corect links in .rst files, so Sphinx can find them
"""
if final_path is None:
overwrite_source = True
else:
overwrite_source = False

if src_path is None:
src_file_path = self.get_src_path_from_parent()
else:
src_file_path = src_path

file_content, includes = self.change_paths_and_get_includes(source_file=source_file,
src_file_path=src_file_path, start_path=start_path)
file_content, includes = self.change_paths_and_get_includes(source_file=source_file, src_file_path=src_path,
start_path=start_path)

if overwrite_source:
self.save_rst(file_path=source_file, file_content=file_content)
Expand All @@ -88,7 +82,7 @@ def change_paths_and_get_includes(self, source_file: str, src_file_path: str, st
with open(source_file, 'r') as src_file:
source_file_str = src_file.read()
source_file_list = list()
include_file_list = list()
include_file_list = set()
for line in source_file_str.split('\n'):
splitted_line = line.split()
if 'include::' in splitted_line:
Expand All @@ -98,7 +92,7 @@ def change_paths_and_get_includes(self, source_file: str, src_file_path: str, st
new_line = splitted_line[:path_index]
abs_path = os.path.abspath(os.path.join(src_file_path, splitted_line[path_index]))
if self.is_rst_file(file_path=abs_path):
include_file_list.append((abs_path, src_file_path))
include_file_list.add((abs_path, src_file_path))
rel_path_from_sphinx_dir = os.path.relpath(path=abs_path, start=start_path)
new_line.append(rel_path_from_sphinx_dir)
new_line = ' '.join(new_line)
Expand All @@ -112,24 +106,29 @@ def change_paths_and_get_includes(self, source_file: str, src_file_path: str, st
return abs_path_str, include_file_list

def main(self):
child_rst = list()
""" Main routine goes for nested .rst docs
"""
child_rst = set()
for doc_name, sources in self.paths.items():
includes = self.generate_rst(source_file=sources.get('src'), src_path=sources.get('src_path'),
final_path=sources.get('final_path'), start_path=self.sphinx_path)
child_rst.extend(includes)
child_rst.update(includes)
self.already_modified_files.update(child_rst)
child_rst_lst = list(child_rst)

grand_child_rst = list()
for child in child_rst:
grand_child_rst = set()
for child in child_rst_lst:
includes_grand = self.generate_rst(source_file=child[0], src_path=os.path.split(child[0])[0],
start_path=self.sphinx_path)
grand_child_rst.extend(includes_grand)
grand_child_rst.update(includes_grand)
grand_child_rst_lst = [gc_rst for gc_rst in grand_child_rst if gc_rst not in self.already_modified_files]

grand_grand_child_rst = list()
for grand_child in grand_child_rst:
grand_grand_child_rst = set()
for grand_child in grand_child_rst_lst:
includes_grand_grand = self.generate_rst(source_file=grand_child[0],
src_path=os.path.split(grand_child[0])[0],
start_path=self.sphinx_path)
grand_grand_child_rst.extend(includes_grand_grand)
grand_grand_child_rst.update(includes_grand_grand)

if not grand_grand_child_rst:
print('DONE! ALL GOOD!')
Expand Down

0 comments on commit fdf13c6

Please sign in to comment.