diff --git a/tests/build-documentation.ipynb b/tests/build-documentation.ipynb deleted file mode 100644 index fa2275e..0000000 --- a/tests/build-documentation.ipynb +++ /dev/null @@ -1,567 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import sys, os, git\n", - "import shutil\n", - "import numpy as np\n", - "import subprocess\n", - "\n", - "from utilities import detect_saving_folder, try_to_copy_file, \\\n", - " return_file_content, detect_block_code, \\\n", - " detect_block_types, create_file, detect_last_matching_line, \\\n", - " append_content\n", - "\n", - "current_path = os.getcwd()\n", - "git_repo = git.Repo(current_path, search_parent_directories=True)\n", - "git_path = git_repo.git.rev_parse(\"--show-toplevel\")\n", - "\n", - "path_to_docs = git_path + \"/docs/source/chapters/\"\n", - "\n", - "# make sure the documentaiton was found\n", - "assert os.path.exists(path_to_docs), \"\"\"Documentation files not found\"\"\"\n", - "\n", - "if os.path.exists(\"generated-codes/\") is False:\n", - " os.mkdir(\"generated-codes/\")" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "TEST -- chapter1.rst -- test_First.py\n", - "TEST -- chapter2.rst -- test_Prepare.py\n", - "TEST -- chapter3.rst -- test_InitializeSimulation.py\n", - "TEST -- chapter4.rst -- test_MinimizeEnergy.py\n" - ] - } - ], - "source": [ - "mycwd = os.getcwd()\n", - "for chapter_id in np.arange(10):\n", - " filename = path_to_docs + \"chapter\"+str(chapter_id)+\".rst\"\n", - " created_files, created_tests = [], []\n", - " if os.path.exists(filename):\n", - " folder = detect_saving_folder(chapter_id)\n", - " created_files = try_to_copy_file(chapter_id, created_files)\n", - " file_content = return_file_content(filename)\n", - " block_contents, block_names = detect_block_code(file_content)\n", - " block_types = detect_block_types(block_contents)\n", - " created_files, created_tests = create_file(block_contents, block_names,\n", - " created_files, created_tests, folder)\n", - " for content, name, type in zip(block_contents, block_names, block_types):\n", - " append_content(folder, name, content, type)\n", - " # Run the tests\n", - " os.chdir(folder)\n", - " for test_file in created_tests:\n", - " print(\"TEST --\", \"chapter\"+str(chapter_id)+\".rst\", \"--\", test_file)\n", - " subprocess.call([\"python3\", test_file])\n", - " os.chdir(mycwd)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'generated-codes/chapter1/'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "folder" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['\\n',\n", - " '\\n',\n", - " 'import os\\n',\n", - " 'from MonteCarlo import MonteCarlo\\n',\n", - " 'from MolecularDynamics import MolecularDynamics\\n',\n", - " '\\n',\n", - " 'md = MolecularDynamics(data_folder = \"md-output/\")\\n',\n", - " 'md.__init__()\\n',\n", - " 'mc = MonteCarlo(data_folder = \"mc-output/\")\\n',\n", - " 'mc.__init__()\\n',\n", - " '\\n',\n", - " 'assert os.path.exists(\"mc-output\"), \"\"\"Error, missing mc-output/ folder\"\"\"\\n',\n", - " 'assert os.path.exists(\"md-output\"), \"\"\"Error, missing md-output/ folder\"\"\"\\n',\n", - " 'assert os.path.exists(\"Outputs\"), \"\"\"Error, missing Outputs/ folder\"\"\"\\n',\n", - " '\\n']" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "content" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (560269670.py, line 1)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m Cell \u001b[0;32mIn[5], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m elif (ISMETHOD) & (ISPARTIAL is False):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" - ] - } - ], - "source": [ - "\n", - "\n", - "\n", - "\n", - "\n", - " elif (ISMETHOD) & (ISPARTIAL is False):\n", - "\n", - " elif ISPARTIAL & ISINIT:\n", - " method_name = \"__init__\"\n", - " # Add the content to the end of the __init__ method\n", - " cpt_new_last, cpt_old_last = detect_last_matching_line(content, original_file_content)\n", - " file = open(folder+name+\".py\", \"w\")\n", - " for line in original_file_content[:cpt_old_last]:\n", - " file.write(line)\n", - " for line in content[cpt_new_last-1:]:\n", - " if \"(...)\" not in line:\n", - " file.write(\" \"+line)\n", - " for line in original_file_content[cpt_old_last:]:\n", - " file.write(line)\n", - " file.close()\n", - " original_file_content = return_file_content(folder+name+\".py\")\n", - " original_start, original_end = detect_method_boundaries(method_name,\n", - " original_file_content)\n", - " unique_lines = detect_unique_lines(original_file_content, original_start, original_end)\n", - " replace_method(folder, name, original_file_content, unique_lines, original_start, original_end)\n", - " elif ISCLASS:\n", - " stop\n", - "\n", - " else:\n", - " \n", - " print(ISIMPORT, ISMETHOD, ISCLASS, ISPARTIAL, ISINIT)\n", - " for line in content:\n", - " print(line[:-1])\n", - " print()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "content" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "name" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "ISIMPORT = False\n", - "ISMETHOD = False\n", - "ISCLASS = False\n", - "ISINIT = False\n", - "PARTIAL = False\n", - "new_position_class = None\n", - "new_position_partial = None\n", - "for cpt, l in enumerate(myclass):\n", - " if ((\"import\" in l) & (\"as\" in l)) | ((\"from\" in l) & (\"import\" in l)):\n", - " ISIMPORT = True\n", - " if (\"def\" in l) & (\"(self\" in l) & (\"):\" in l):\n", - " ISMETHOD = True\n", - " if (\"class\" in l) & (\":\" in l):\n", - " ISCLASS = True\n", - " new_position_class = cpt\n", - " if (\"def\" in l) & (\"__init__\" in l):\n", - " ISINIT = True\n", - " if (\"(...)\" in l):\n", - " PARTIAL = True\n", - " new_position_partial = cpt" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "\n", - " file = open(filename, \"r\")\n", - " print_file = False\n", - " for line in file: # Loop over all the lines of the file\n", - " if \".. label::\" in line: # Detect the label \"start\" and label \"end\"\n", - " label = line.split(\".. label:: \")[1] # Look for label in the line\n", - " if label[:6] == \"start_\": # Detect starting label\n", - " class_name_i = label.split(\"start_\")[1].split(\"_class\")[0]\n", - " if \"test\" not in class_name_i:\n", - " list_files.append(class_name_i+\".py\")\n", - " print_file = True\n", - " # create file\n", - " myclass = open(folder+class_name_i+\".py\", \"w\")\n", - " elif label[:4] == \"end_\": # Detect ending label\n", - " class_name_f = label.split(\"end_\")[1].split(\"_class\")[0]\n", - " if \"test\" not in class_name_f:\n", - " assert class_name_f == class_name_i, \"\"\"Different class closed, inconsistency in rst file?\"\"\"\n", - " print_file = False\n", - " # close file\n", - " myclass.close()\n", - " else:\n", - " if print_file: # Print the content of the label into files\n", - " if \".. code-block::\" not in line: # Ignore code block line\n", - " if len(line) > 1: # Remove the indentation\n", - " myclass.write(line[4:])\n", - " else:\n", - " myclass.write(line)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "for chapter_id in np.arange(2, 10):\n", - " filename = path_to_docs + \"chapter\"+str(chapter_id)+\".rst\"\n", - " list_classes = []\n", - " if os.path.exists(filename):\n", - " # saving folder\n", - " folder = \"generated-codes/chapter\"+str(chapter_id)+\"/\"\n", - " previous_folder = \"generated-codes/chapter\"+str(chapter_id-1)+\"/\"\n", - " if os.path.exists(folder) is False:\n", - " os.mkdir(folder)\n", - " # copy all the files from the previous chapter\n", - " for file in list_files:\n", - " shutil.copyfile(previous_folder+\"/\"+file, folder+\"/\"+file)\n", - " print_file = False\n", - " for line in open(filename, \"r\"): # Loop over all the lines of the file\n", - " if \".. label::\" in line: # Detect the label \"start\" and label \"end\"\n", - " label = line.split(\".. label:: \")[1] # Look for label in the line\n", - " if label[:6] == \"start_\": # Detect starting label\n", - " class_name_i = label.split(\"start_\")[1].split(\"_class\")[0]\n", - " if \"test\" not in class_name_i:\n", - " list_classes.append(class_name_i)\n", - " print_file = True\n", - " myclass = []\n", - " elif label[:4] == \"end_\": # Detect ending label\n", - " class_name_f = label.split(\"end_\")[1].split(\"_class\")[0]\n", - " if \"test\" not in class_name_f:\n", - " assert class_name_f == class_name_i, \"\"\"Different class closed, inconsistency in rst file?\"\"\"\n", - " print_file = False\n", - " # myclass.close()\n", - " if (len(myclass) > 0) & (\"test\" not in class_name_f):\n", - " # detect the type of code\n", - " ISIMPORT = False\n", - " ISMETHOD = False\n", - " ISCLASS = False\n", - " ISINIT = False\n", - " PARTIAL = False\n", - " new_position_class = None\n", - " new_position_partial = None\n", - " for cpt, l in enumerate(myclass):\n", - " if ((\"import\" in l) & (\"as\" in l)) | ((\"from\" in l) & (\"import\" in l)):\n", - " ISIMPORT = True\n", - " if (\"def\" in l) & (\"(self\" in l) & (\"):\" in l):\n", - " ISMETHOD = True\n", - " if (\"class\" in l) & (\":\" in l):\n", - " ISCLASS = True\n", - " new_position_class = cpt\n", - " if (\"def\" in l) & (\"__init__\" in l):\n", - " ISINIT = True\n", - " if (\"(...)\" in l):\n", - " PARTIAL = True\n", - " new_position_partial = cpt\n", - "\n", - " original_class = open(folder+class_name_i+\".py\", \"r\")\n", - " original_content = []\n", - " original_position_class = None\n", - " original_empty_lines = []\n", - " for cpt, l in enumerate(original_class):\n", - " original_content.append(l)\n", - " if \"__init__\" in l:\n", - " original_position_init = cpt\n", - " elif \"class\" in l:\n", - " original_position_class = cpt\n", - " elif \"\\n\" == l:\n", - " original_empty_lines.append(cpt)\n", - " original_empty_lines = np.array(original_empty_lines)\n", - " if ISIMPORT:\n", - " new_class = open(folder+class_name_i+\".py\", \"w\")\n", - " for l in myclass:\n", - " new_class.write(l)\n", - " for l in original_content:\n", - " new_class.write(l)\n", - " new_class.close()\n", - " elif ISMETHOD:\n", - " new_class = open(folder+class_name_i+\".py\", \"w\")\n", - " for l in original_content:\n", - " new_class.write(l)\n", - " for l in myclass:\n", - " new_class.write(\" \"+l)\n", - " new_class.close()\n", - " elif (ISCLASS) & (PARTIAL is False):\n", - " new_class = open(folder+class_name_i+\".py\", \"w\")\n", - " REPLACE = False\n", - " REPLACED = False\n", - " for cpt, l in enumerate(original_content):\n", - " if cpt == original_position_class: # start class\n", - " REPLACE = True\n", - " elif (l == \"\\n\") & REPLACE:\n", - " REPLACE = False\n", - "\n", - " if REPLACE:\n", - " if REPLACED is False:\n", - " REPLACED = True\n", - " for ll in myclass:\n", - " new_class.write(ll)\n", - " else:\n", - " new_class.write(l)\n", - " new_class.close()\n", - " elif (ISINIT) & (PARTIAL):\n", - " end_init = original_empty_lines[original_empty_lines\n", - " > original_position_class][0]\n", - " new_class = open(folder+class_name_i+\".py\", \"w\")\n", - " for cpt, l in enumerate(original_content):\n", - " new_class.write(l)\n", - " if cpt == end_init+1:\n", - " for ll in myclass:\n", - " # Make sure the line is not already in\n", - " # before writting it\n", - " # ALREADYIN = False\n", - " # for lll in original_content:\n", - " # if ll in lll:\n", - " # ALREADYIN = True\n", - " if (\"def __init__\" not in ll) & (\"(...)\" not in ll): # & (ALREADYIN is False):\n", - " new_class.write(\" \"+ll)\n", - "\n", - " new_class.close()\n", - "\n", - " # remove space in empty lines\n", - " original_class = open(folder+class_name_i+\".py\", \"r\")\n", - " original_content = []\n", - " for cpt, l in enumerate(original_class):\n", - " original_content.append(l)\n", - " original_class.close()\n", - " new_class = open(folder+class_name_i+\".py\", \"w\")\n", - " for l in original_content:\n", - " if l == \" \\n\":\n", - " l = \"\\n\"\n", - " new_class.write(l)\n", - " new_class.close()\n", - " else:\n", - " if print_file: # Print the content of the label into files\n", - " if \".. code-block::\" not in line: # Ignore code block line\n", - " if len(line) > 1: # Remove the indentation\n", - " # myclass.write(line[4:])\n", - " myclass.append(line[4:])\n", - " else:\n", - " # myclass.write(line)\n", - " myclass.append(line)\n", - "\n", - " for class_name in np.unique(list_classes):\n", - "\n", - " # Remove doublons in init functions\n", - " original_class = open(folder+class_name+\".py\", \"r\")\n", - " original_start_init = None\n", - " original_end_init = []\n", - " original_content = []\n", - " for cpt, l in enumerate(original_class):\n", - " original_content.append(l)\n", - " if (\"def\" in l) & (\"__init__\" in l):\n", - " original_start_init = cpt\n", - " elif (\":\" in l) & (\"def\" in l) & (\"__init__\" not in l):\n", - " original_end_init.append(cpt) \n", - " original_end_init = original_end_init[0]\n", - " original_class.close()\n", - "\n", - " # detect unique lines\n", - " _, idx = np.unique(original_content[original_start_init:original_end_init], return_index=True)\n", - " new_bloc = []\n", - " for i in np.sort(idx):\n", - " new_bloc.append(original_content[original_start_init:original_end_init][i])\n", - "\n", - " REPLACED = False\n", - " new_class = open(folder+class_name+\".py\", \"w\")\n", - " for cpt, l in enumerate(original_content):\n", - " if (cpt < original_start_init) | (cpt > original_end_init-2):\n", - " new_class.write(l)\n", - " else:\n", - " if REPLACED is False:\n", - " REPLACED = True\n", - " for ll in new_bloc:\n", - " new_class.write(ll)\n", - "\n", - " new_class.close()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Detect test files\n", - "for chapter_id in np.arange(1, 10):\n", - " filename = path_to_docs + \"chapter\"+str(chapter_id)+\".rst\"\n", - " test_files = []\n", - " if os.path.exists(filename):\n", - " # saving folder\n", - " folder = \"generated-codes/chapter\"+str(chapter_id)+\"/\"\n", - " if os.path.exists(folder) is False:\n", - " os.mkdir(folder)\n", - " file = open(filename, \"r\")\n", - " print_file = False\n", - " for line in file: # Loop over all the lines of the file\n", - " if \".. label::\" in line: # Detect the label \"start\" and label \"end\"\n", - " label = line.split(\".. label:: \")[1] # Look for label in the line\n", - " if label[:6] == \"start_\": # Detect starting label\n", - " class_name_i = label.split(\"start_\")[1].split(\"_class\")[0]\n", - " if \"test\" in class_name_i:\n", - " test_files.append(class_name_i+\".py\")\n", - " print_file = True\n", - " # create file\n", - " myclass = open(folder+class_name_i+\".py\", \"w\")\n", - " elif label[:4] == \"end_\": # Detect ending label\n", - " class_name_f = label.split(\"end_\")[1].split(\"_class\")[0]\n", - " if \"test\" in class_name_f:\n", - " assert class_name_f == class_name_i, \"\"\"Different class closed, inconsistency in rst file?\"\"\"\n", - " print_file = False\n", - " # close file\n", - " myclass.close()\n", - " else:\n", - " if print_file: # Print the content of the label into files\n", - " if \".. code-block::\" not in line: # Ignore code block line\n", - " if len(line) > 1: # Remove the indentation\n", - " myclass.write(line[4:])\n", - " else:\n", - " myclass.write(line)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Run test files\n", - "for chapter_id in np.arange(1, 10):\n", - " # Test\n", - " mycwd = os.getcwd()\n", - " os.chdir(folder)\n", - " for test_file in test_files:\n", - " print(\"TEST\", \"chapter\"+str(chapter_id)+\".rst\", test_file)\n", - " subprocess.call([\"python3\", test_file])\n", - " os.chdir(mycwd)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/tests/build-documentation.py b/tests/build-documentation.py index 39d28fd..8982cb9 100644 --- a/tests/build-documentation.py +++ b/tests/build-documentation.py @@ -1,11 +1,14 @@ -#!/usr/bin/env python -# coding: utf-8 import sys, os, git import shutil import numpy as np import subprocess +from utilities import detect_saving_folder, try_to_copy_file, \ + return_file_content, detect_block_code, \ + detect_block_types, create_file, detect_last_matching_line, \ + append_content + current_path = os.getcwd() git_repo = git.Repo(current_path, search_parent_directories=True) git_path = git_repo.git.rev_parse("--show-toplevel") @@ -18,256 +21,23 @@ if os.path.exists("generated-codes/") is False: os.mkdir("generated-codes/") -# In chapter 1, all the files are simply created. -chapter_id = 1 -filename = path_to_docs + "chapter"+str(chapter_id)+".rst" -list_files = [] -if os.path.exists(filename): - # saving folder - folder = "generated-codes/chapter"+str(chapter_id)+"/" - if os.path.exists(folder) is False: - os.mkdir(folder) - file = open(filename, "r") - print_file = False - for line in file: # Loop over all the lines of the file - if ".. label::" in line: # Detect the label "start" and label "end" - label = line.split(".. label:: ")[1] # Look for label in the line - if label[:6] == "start_": # Detect starting label - class_name_i = label.split("start_")[1].split("_class")[0] - if "test" not in class_name_i: - list_files.append(class_name_i+".py") - print_file = True - # create file - myclass = open(folder+class_name_i+".py", "w") - elif label[:4] == "end_": # Detect ending label - class_name_f = label.split("end_")[1].split("_class")[0] - if "test" not in class_name_f: - assert class_name_f == class_name_i, """Different class closed, inconsistency in rst file?""" - print_file = False - # close file - myclass.close() - else: - if print_file: # Print the content of the label into files - if ".. code-block::" not in line: # Ignore code block line - if len(line) > 1: # Remove the indentation - myclass.write(line[4:]) - else: - myclass.write(line) - -for chapter_id in np.arange(2, 10): - filename = path_to_docs + "chapter"+str(chapter_id)+".rst" - list_classes = [] - if os.path.exists(filename): - # saving folder - folder = "generated-codes/chapter"+str(chapter_id)+"/" - previous_folder = "generated-codes/chapter"+str(chapter_id-1)+"/" - if os.path.exists(folder) is False: - os.mkdir(folder) - # copy all the files from the previous chapter - for file in list_files: - shutil.copyfile(previous_folder+"/"+file, folder+"/"+file) - print_file = False - for line in open(filename, "r"): # Loop over all the lines of the file - if ".. label::" in line: # Detect the label "start" and label "end" - label = line.split(".. label:: ")[1] # Look for label in the line - if label[:6] == "start_": # Detect starting label - class_name_i = label.split("start_")[1].split("_class")[0] - if "test" not in class_name_i: - list_classes.append(class_name_i) - print_file = True - myclass = [] - elif label[:4] == "end_": # Detect ending label - class_name_f = label.split("end_")[1].split("_class")[0] - if "test" not in class_name_f: - assert class_name_f == class_name_i, """Different class closed, inconsistency in rst file?""" - print_file = False - # myclass.close() - if (len(myclass) > 0) & ("test" not in class_name_f): - # detect the type of code - ISIMPORT = False - ISMETHOD = False - ISCLASS = False - ISINIT = False - PARTIAL = False - new_position_class = None - new_position_partial = None - for cpt, l in enumerate(myclass): - if (("import" in l) & ("as" in l)) | (("from" in l) & ("import" in l)): - ISIMPORT = True - if ("def" in l) & ("(self" in l) & ("):" in l): - ISMETHOD = True - if ("class" in l) & (":" in l): - ISCLASS = True - new_position_class = cpt - if ("def" in l) & ("__init__" in l): - ISINIT = True - if ("(...)" in l): - PARTIAL = True - new_position_partial = cpt - - original_class = open(folder+class_name_i+".py", "r") - original_content = [] - original_position_class = None - original_empty_lines = [] - for cpt, l in enumerate(original_class): - original_content.append(l) - if "__init__" in l: - original_position_init = cpt - elif "class" in l: - original_position_class = cpt - elif "\n" == l: - original_empty_lines.append(cpt) - original_empty_lines = np.array(original_empty_lines) - if ISIMPORT: - new_class = open(folder+class_name_i+".py", "w") - for l in myclass: - new_class.write(l) - for l in original_content: - new_class.write(l) - new_class.close() - elif ISMETHOD: - new_class = open(folder+class_name_i+".py", "w") - for l in original_content: - new_class.write(l) - for l in myclass: - new_class.write(" "+l) - new_class.close() - elif (ISCLASS) & (PARTIAL is False): - new_class = open(folder+class_name_i+".py", "w") - REPLACE = False - REPLACED = False - for cpt, l in enumerate(original_content): - if cpt == original_position_class: # start class - REPLACE = True - elif (l == "\n") & REPLACE: - REPLACE = False - - if REPLACE: - if REPLACED is False: - REPLACED = True - for ll in myclass: - new_class.write(ll) - else: - new_class.write(l) - new_class.close() - elif (ISINIT) & (PARTIAL): - end_init = original_empty_lines[original_empty_lines - > original_position_class][0] - new_class = open(folder+class_name_i+".py", "w") - for cpt, l in enumerate(original_content): - new_class.write(l) - if cpt == end_init+1: - for ll in myclass: - # Make sure the line is not already in - # before writting it - # ALREADYIN = False - # for lll in original_content: - # if ll in lll: - # ALREADYIN = True - if ("def __init__" not in ll) & ("(...)" not in ll): # & (ALREADYIN is False): - new_class.write(" "+ll) - - new_class.close() - - # remove space in empty lines - original_class = open(folder+class_name_i+".py", "r") - original_content = [] - for cpt, l in enumerate(original_class): - original_content.append(l) - original_class.close() - new_class = open(folder+class_name_i+".py", "w") - for l in original_content: - if l == " \n": - l = "\n" - new_class.write(l) - new_class.close() - else: - if print_file: # Print the content of the label into files - if ".. code-block::" not in line: # Ignore code block line - if len(line) > 1: # Remove the indentation - # myclass.write(line[4:]) - myclass.append(line[4:]) - else: - # myclass.write(line) - myclass.append(line) - - for class_name in np.unique(list_classes): - - # Remove doublons in init functions - original_class = open(folder+class_name+".py", "r") - original_start_init = None - original_end_init = [] - original_content = [] - for cpt, l in enumerate(original_class): - original_content.append(l) - if ("def" in l) & ("__init__" in l): - original_start_init = cpt - elif (":" in l) & ("def" in l) & ("__init__" not in l): - original_end_init.append(cpt) - original_end_init = original_end_init[0] - original_class.close() - - # detect unique lines - _, idx = np.unique(original_content[original_start_init:original_end_init], return_index=True) - new_bloc = [] - for i in np.sort(idx): - new_bloc.append(original_content[original_start_init:original_end_init][i]) - - REPLACED = False - new_class = open(folder+class_name+".py", "w") - for cpt, l in enumerate(original_content): - if (cpt < original_start_init) | (cpt > original_end_init-2): - new_class.write(l) - else: - if REPLACED is False: - REPLACED = True - for ll in new_bloc: - new_class.write(ll) - - new_class.close() - -# Detect test files -for chapter_id in np.arange(1, 10): +mycwd = os.getcwd() +for chapter_id in np.arange(10): filename = path_to_docs + "chapter"+str(chapter_id)+".rst" - test_files = [] + created_files, created_tests = [], [] if os.path.exists(filename): - # saving folder - folder = "generated-codes/chapter"+str(chapter_id)+"/" - if os.path.exists(folder) is False: - os.mkdir(folder) - file = open(filename, "r") - print_file = False - for line in file: # Loop over all the lines of the file - if ".. label::" in line: # Detect the label "start" and label "end" - label = line.split(".. label:: ")[1] # Look for label in the line - if label[:6] == "start_": # Detect starting label - class_name_i = label.split("start_")[1].split("_class")[0] - if "test" in class_name_i: - test_files.append(class_name_i+".py") - print_file = True - # create file - myclass = open(folder+class_name_i+".py", "w") - elif label[:4] == "end_": # Detect ending label - class_name_f = label.split("end_")[1].split("_class")[0] - if "test" in class_name_f: - assert class_name_f == class_name_i, """Different class closed, inconsistency in rst file?""" - print_file = False - # close file - myclass.close() - else: - if print_file: # Print the content of the label into files - if ".. code-block::" not in line: # Ignore code block line - if len(line) > 1: # Remove the indentation - myclass.write(line[4:]) - else: - myclass.write(line) - -# Run test files -for chapter_id in np.arange(1, 10): - # Test - mycwd = os.getcwd() - os.chdir(folder) - for test_file in test_files: - print("TEST", "chapter"+str(chapter_id)+".rst", test_file) - subprocess.call(["python3", test_file]) - os.chdir(mycwd) \ No newline at end of file + folder = detect_saving_folder(chapter_id) + created_files = try_to_copy_file(chapter_id, created_files) + file_content = return_file_content(filename) + block_contents, block_names = detect_block_code(file_content) + block_types = detect_block_types(block_contents) + created_files, created_tests = create_file(block_contents, block_names, + created_files, created_tests, folder) + for content, name, type in zip(block_contents, block_names, block_types): + append_content(folder, name, content, type) + # Run the tests + os.chdir(folder) + for test_file in created_tests: + print("TEST --", "chapter"+str(chapter_id)+".rst", "--", test_file) + subprocess.call(["python3", test_file]) + os.chdir(mycwd) \ No newline at end of file