diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py index 0a4bcc74c192d90491198da7509f51a939d568d4..78947ea1c295af07f76de0ac3736911037a6b196 100644 --- a/cal_tools/cal_tools/tools.py +++ b/cal_tools/cal_tools/tools.py @@ -15,10 +15,29 @@ import time from uuid import uuid4 +def atoi(text): + ''' + Convert string to integer is possible + + :param text: string to be converted + :return: integer value or input string + ''' + + return int(text) if text.isdigit() else text + + +def natural_keys(text): + ''' + Decompose string to list of integers and sub-strings + ''' + return [ atoi(c) for c in re.split(r'(\d+)', text) ] + + def combine_report(run_path, calibration): sphinx_path = "{}/sphinx_rep".format(os.path.abspath(run_path)) os.makedirs(sphinx_path) direntries = os.listdir(run_path) + direntries.sort(key=natural_keys) for entry in direntries: @@ -197,30 +216,21 @@ def make_report(run_path, tmp_path, out_path, project, author, version, report_t if ext == ".rst" and "index" not in name: files_to_handle.append(name.strip()) - with open("{}/index.rst.tmp".format(run_path), "w") as mf: - with open("{}/index.rst".format(run_path), "r") as mfr: - indexTmp = Template(''' - .. toctree:: - :maxdepth: 2 - {% for k in keys %} - {{ k }} - {%- endfor %} - ''') - for line in mfr: - line = line.replace(".. toctree::", textwrap.dedent( - indexTmp.render(keys=files_to_handle))) - line = line.replace(":maxdepth: 2", "") - line = line.replace("Documentation", "Calibration") - if ":caption." in line: - continue - mf.write(line) - cdir = os.getcwd() - - os.remove("{}/index.rst".format(run_path)) - move("{}/index.rst.tmp".format(run_path), "{}/index.rst".format(run_path)) + index_tmp = Template(''' + Calibration report + ================== + + .. toctree:: + :maxdepth: 2 + {% for k in keys %} + {{ k }} + {%- endfor %} + ''') - # finally call the make scripts + with open("{}/index.rst".format(run_path), "w+") as mf: + mf.write(textwrap.dedent(index_tmp.render(keys=files_to_handle))) + # finally call the make scripts os.chdir(run_path) try: import subprocess