Skip to content

Mkdocs control

Starting and stopping (and test if running) of mkdocs

Starts, stops and assesses state of mkdocs serve

Classes:

Name Description
MkdocsControl

manage mkdocs server

MkdocsControl

Source code in app/dcsp/app/functions/mkdocs_control.py
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
class MkdocsControl:
    def __init__(
        self,
        project_id: int | str,
        projects_folder: str = c.PROJECTS_FOLDER,
    ) -> None:
        """Initialises the MkDocsControl class

        Args:
            cwd_sh (str): the current working directory for the shell script
        """
        self.project_id: int = 0
        self.process_name: str = "mkdocs"
        self.process_arg1: str = "serve"
        self.documentation_pages: str = ""
        self.project_folder: str = ""

        if not isinstance(project_id, int):
            if not project_id.isdigit():
                raise ValueError(
                    f"'project_id of '{ project_id }' is not an integer"
                )

        self.project_id = int(project_id)

        # TODO should check in model if project ID exists
        if self.project_id <= 0:
            raise ValueError(
                f"'project_id of '{ project_id }' must be 1 or more"
            )

        self.documentation_pages = (
            f"{ c.DOCUMENTATION_PAGES }/project_{ self.project_id }"
        )

        if not Path(self.documentation_pages).is_dir():
            Path(self.documentation_pages).mkdir(parents=True, exist_ok=True)

        self.project_folder = f"{ projects_folder }project_{ project_id }/"

        self.documents_directory = (
            f"{ self.project_folder }{ c.CLINICAL_SAFETY_FOLDER }"
        )

        # TODO #56 - was this meant to check projects_folder or project_folder (not the s)
        if not Path(projects_folder).is_dir():
            raise FileExistsError(f"'{ projects_folder }' does not exist")

        return

    def preprocessor(self, entry_type: str = "hazard") -> str:
        """Adds all entry to the entries-summary page

        Returns:
            str: a string of outcomes from function, formatted for html
        """
        entries_dir: str = f"{ self.documents_directory }docs/{ entry_type }s/"
        entries_entries_dir: str = f"{ entries_dir }{ entry_type }s/"
        entry_template_dir: str = f"{ self.documents_directory }templates/"
        files_to_check: list[str] = []
        docstring: DocstringManipulation
        # file
        # entry_file
        contents_str: str = ""
        # file_name
        # entry_name
        # entry_number
        # entry_name_match
        warnings: str = ""
        entries: list[dict[str, Any]] = []
        project: ProjectBuilder
        contents_list: list[dict[str, Any]] = []
        entry_form: dict[str, Any]
        icon_html: str = ""
        code_html: str = ""
        referenced_hazards: list[dict[str, Any]] = []
        function_hazards: list[str] = []

        if not Path(entries_dir).is_dir():
            return (
                "<b>Failed preprocessor</b>"
                "<br><hr>"
                f"'{ entries_dir }' directory does not exist"
                "<br><hr>"
            )

        if not Path(entries_entries_dir).is_dir():
            Path(entries_entries_dir).mkdir(parents=True, exist_ok=True)
            return (
                "<b>Preprocessor passed with INFO</b>"
                "<br><hr>"
                f"INFO - '{ entries_entries_dir }' directory did not exist, now created"
                "<br><hr>"
            )

        for path, _, files in os.walk(entries_entries_dir):
            for name in files:
                if fnmatch(name, "*.md"):
                    files_to_check.append(os.path.join(path, name))

        # Sort the list of file paths based on the file numbers
        files_to_check = sorted(files_to_check, reverse=True)

        if not len(files_to_check):
            return (
                "<b>Preprocessor passed with INFO</b>"
                "<br><hr>"
                f"INFO - No entries found in '{ entries_entries_dir }' folder."
                "Entries summary page could not be created"
                "<br><hr>"
            )

        # TODO - should check there are no files with same entry number (eg hazard-1 and hazard-01 and hazard-001)

        docstring = DocstringManipulation(self.project_id)
        referenced_hazards = docstring.docstring_all()

        for file in files_to_check:
            icon_html = ""
            function_hazards = []

            entry_file = open(file, "r")
            contents_str = entry_file.read()
            file_name = Path(file).stem

            try:
                entry_number = file_name.split("-")[1]
                if not entry_number.isdigit():
                    entry_number = "[Non-digit entry number]"
                    warnings += f"WARNING - A non-digit 'number' in entry file name '{ file_name }'"
            except:
                entry_number = "[Number not defined]"

            for function_info in referenced_hazards:
                for hazard in function_info["hazards"]:
                    if hazard["hazard_number"] == entry_number:
                        function_hazards.append(function_info["mk_file_path"])

                        # print(entry_number)
                        # print(function_hazards)

            project = ProjectBuilder(self.project_id)
            # TODO - need to figure out which entry_types are preprocessed
            contents_list = project.entry_read_with_field_types("hazard", file)

            for field in contents_list:
                if field["field_type"] == "icon":
                    env = Environment(
                        loader=FileSystemLoader(entry_template_dir),
                        autoescape=True,
                    )
                    template = env.get_template(f"{ entry_type }-icons.md")

                    context = {"contents_list": contents_list}
                    icon_html = template.render(context)
                    icon_html = f"{ icon_html }\n<!-- [iconend] -->"

                elif field["field_type"] == "code":
                    code_html = ""

                    for function in referenced_hazards:
                        for hazard in function["hazards"]:
                            if hazard["hazard_number"] == entry_number:
                                code_html += (
                                    f"[{ function['code_file'].replace('.py', '') }"
                                    f".{ hazard['sub_routine']}](../../{ function['mk_file_path'] }"
                                    f"#{ hazard['sub_routine']}_hazard)\n\n"
                                )

                    code_html = code_html.rstrip("\n\n")

                    if not code_html:
                        code_html = "Hazard not mentioned in source code"

            entry_form = project.entry_file_read_to_form(
                contents_list,
                icon_html,
                code_html,
            )

            project.entry_update(
                entry_form,
                entry_type,
                entry_number,
            )
            pattern = re.compile(
                r"<!--\s*\[icon\]\s*-->.*?<!--\s*\[iconend\]\s*-->",
                re.DOTALL,
            )
            contents_str = re.sub(pattern, "", contents_str)
            contents_str = contents_str.replace("../../", "../")
            icon_html = icon_html.replace("../../", "../")
            icon_html = icon_html.replace(
                'class="icon-large"',
                'class="icon-small"',
            )

            entries.append(
                {
                    "number": entry_number,
                    "contents_str": contents_str,
                    "contents_list": contents_list,
                    "icon_html": icon_html,
                }
            )

        # Creating the summary
        env = Environment(
            loader=FileSystemLoader(entry_template_dir), autoescape=True
        )
        template = env.get_template(
            f"{ entry_type }{ c.ENTRY_SUMMARY_SUFFIX }"
        )
        context = {"entries": entries}
        md_content = template.render(context)
        summary_file = open(
            f"{ entries_dir }{ entry_type }{ c.ENTRY_SUMMARY_SUFFIX }",
            "w",
        )
        summary_file.write(md_content)
        summary_file.close()

        if warnings:
            return (
                "<b>Successful preprocessor step with WARNINGS</b>"
                "<br><hr>"
                f"{ warnings }"
                "<br><hr>"
            )
        else:
            return "<b>Successful preprocessor step</b>" "<br><hr>"

    def build(self) -> str:
        """ """
        command: list[str] = []
        command_output_dir: str = (
            f"{ c.DOCUMENTATION_PAGES }/project_{ self.project_id }"
        )
        command_output: CompletedProcess[str]
        command_output_html: str = ""
        stdout_result: str = ""
        stderr_result: str = ""

        if not Path(command_output_dir).is_dir():
            raise FileExistsError(
                f"'{ command_output_dir }' directory does not exist"
            )

        command = [
            "/usr/local/bin/mkdocs",
            "build",
            "-d",
            command_output_dir,
        ]

        command_output = subprocess.run(
            command,
            shell=False,
            check=False,
            cwd=self.documents_directory,
            capture_output=True,
            text=True,
        )  # nosec B603

        if command_output.returncode == 0:
            command_output_html += "<b>Successful mkdocs build</b>"
        else:
            command_output_html += "<b>Mkdocs build errors!</b>"

        command_output_html += "<br><hr>"

        stdout_result = command_output.stdout.replace("\n", "<br>")
        command_output_html += f"<b>Stdout:</b> { stdout_result }"

        command_output_html += "<br><hr>"

        stderr_result = command_output.stderr.replace("\n", "<br>")
        command_output_html += f"<b>Stderr:</b> { stderr_result }"

        return command_output_html

    def build_documents(self, force: bool = False) -> str:
        """Build the documents static pages

        Builds the documents static pages if any documents have been modified since
        last build.

        Args:
            project_id (str): the primary key for the project
        """
        project: Project
        time_now = timezone.now()
        build_output: str = ""
        last_build: Optional[datetime] = None
        last_modified: Optional[datetime] = None
        preprocessor_output: str = ""

        project = project = Project.objects.get(id=self.project_id)
        last_build = project.last_built
        last_modified = project.last_modified

        if (
            isinstance(last_modified, datetime)
            and isinstance(last_build, datetime)
            and not force
        ):
            if last_modified < last_build:
                return ""

        preprocessor_output = self.preprocessor()
        if preprocessor_output == "":
            return "Preprocessor error!"

        build_output = self.build()
        if build_output == "":
            return "mkdocs build error!"

        build_output = f"{ preprocessor_output } {build_output}"

        project.last_built = time_now
        project.build_output = build_output
        project.save()

        return build_output

__init__(project_id, projects_folder=c.PROJECTS_FOLDER)

Initialises the MkDocsControl class

Parameters:

Name Type Description Default
cwd_sh str

the current working directory for the shell script

required
Source code in app/dcsp/app/functions/mkdocs_control.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def __init__(
    self,
    project_id: int | str,
    projects_folder: str = c.PROJECTS_FOLDER,
) -> None:
    """Initialises the MkDocsControl class

    Args:
        cwd_sh (str): the current working directory for the shell script
    """
    self.project_id: int = 0
    self.process_name: str = "mkdocs"
    self.process_arg1: str = "serve"
    self.documentation_pages: str = ""
    self.project_folder: str = ""

    if not isinstance(project_id, int):
        if not project_id.isdigit():
            raise ValueError(
                f"'project_id of '{ project_id }' is not an integer"
            )

    self.project_id = int(project_id)

    # TODO should check in model if project ID exists
    if self.project_id <= 0:
        raise ValueError(
            f"'project_id of '{ project_id }' must be 1 or more"
        )

    self.documentation_pages = (
        f"{ c.DOCUMENTATION_PAGES }/project_{ self.project_id }"
    )

    if not Path(self.documentation_pages).is_dir():
        Path(self.documentation_pages).mkdir(parents=True, exist_ok=True)

    self.project_folder = f"{ projects_folder }project_{ project_id }/"

    self.documents_directory = (
        f"{ self.project_folder }{ c.CLINICAL_SAFETY_FOLDER }"
    )

    # TODO #56 - was this meant to check projects_folder or project_folder (not the s)
    if not Path(projects_folder).is_dir():
        raise FileExistsError(f"'{ projects_folder }' does not exist")

    return

build()

Source code in app/dcsp/app/functions/mkdocs_control.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def build(self) -> str:
    """ """
    command: list[str] = []
    command_output_dir: str = (
        f"{ c.DOCUMENTATION_PAGES }/project_{ self.project_id }"
    )
    command_output: CompletedProcess[str]
    command_output_html: str = ""
    stdout_result: str = ""
    stderr_result: str = ""

    if not Path(command_output_dir).is_dir():
        raise FileExistsError(
            f"'{ command_output_dir }' directory does not exist"
        )

    command = [
        "/usr/local/bin/mkdocs",
        "build",
        "-d",
        command_output_dir,
    ]

    command_output = subprocess.run(
        command,
        shell=False,
        check=False,
        cwd=self.documents_directory,
        capture_output=True,
        text=True,
    )  # nosec B603

    if command_output.returncode == 0:
        command_output_html += "<b>Successful mkdocs build</b>"
    else:
        command_output_html += "<b>Mkdocs build errors!</b>"

    command_output_html += "<br><hr>"

    stdout_result = command_output.stdout.replace("\n", "<br>")
    command_output_html += f"<b>Stdout:</b> { stdout_result }"

    command_output_html += "<br><hr>"

    stderr_result = command_output.stderr.replace("\n", "<br>")
    command_output_html += f"<b>Stderr:</b> { stderr_result }"

    return command_output_html

build_documents(force=False)

Build the documents static pages

Builds the documents static pages if any documents have been modified since last build.

Parameters:

Name Type Description Default
project_id str

the primary key for the project

required
Source code in app/dcsp/app/functions/mkdocs_control.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
def build_documents(self, force: bool = False) -> str:
    """Build the documents static pages

    Builds the documents static pages if any documents have been modified since
    last build.

    Args:
        project_id (str): the primary key for the project
    """
    project: Project
    time_now = timezone.now()
    build_output: str = ""
    last_build: Optional[datetime] = None
    last_modified: Optional[datetime] = None
    preprocessor_output: str = ""

    project = project = Project.objects.get(id=self.project_id)
    last_build = project.last_built
    last_modified = project.last_modified

    if (
        isinstance(last_modified, datetime)
        and isinstance(last_build, datetime)
        and not force
    ):
        if last_modified < last_build:
            return ""

    preprocessor_output = self.preprocessor()
    if preprocessor_output == "":
        return "Preprocessor error!"

    build_output = self.build()
    if build_output == "":
        return "mkdocs build error!"

    build_output = f"{ preprocessor_output } {build_output}"

    project.last_built = time_now
    project.build_output = build_output
    project.save()

    return build_output

preprocessor(entry_type='hazard')

Adds all entry to the entries-summary page

Returns:

Name Type Description
str str

a string of outcomes from function, formatted for html

Source code in app/dcsp/app/functions/mkdocs_control.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def preprocessor(self, entry_type: str = "hazard") -> str:
    """Adds all entry to the entries-summary page

    Returns:
        str: a string of outcomes from function, formatted for html
    """
    entries_dir: str = f"{ self.documents_directory }docs/{ entry_type }s/"
    entries_entries_dir: str = f"{ entries_dir }{ entry_type }s/"
    entry_template_dir: str = f"{ self.documents_directory }templates/"
    files_to_check: list[str] = []
    docstring: DocstringManipulation
    # file
    # entry_file
    contents_str: str = ""
    # file_name
    # entry_name
    # entry_number
    # entry_name_match
    warnings: str = ""
    entries: list[dict[str, Any]] = []
    project: ProjectBuilder
    contents_list: list[dict[str, Any]] = []
    entry_form: dict[str, Any]
    icon_html: str = ""
    code_html: str = ""
    referenced_hazards: list[dict[str, Any]] = []
    function_hazards: list[str] = []

    if not Path(entries_dir).is_dir():
        return (
            "<b>Failed preprocessor</b>"
            "<br><hr>"
            f"'{ entries_dir }' directory does not exist"
            "<br><hr>"
        )

    if not Path(entries_entries_dir).is_dir():
        Path(entries_entries_dir).mkdir(parents=True, exist_ok=True)
        return (
            "<b>Preprocessor passed with INFO</b>"
            "<br><hr>"
            f"INFO - '{ entries_entries_dir }' directory did not exist, now created"
            "<br><hr>"
        )

    for path, _, files in os.walk(entries_entries_dir):
        for name in files:
            if fnmatch(name, "*.md"):
                files_to_check.append(os.path.join(path, name))

    # Sort the list of file paths based on the file numbers
    files_to_check = sorted(files_to_check, reverse=True)

    if not len(files_to_check):
        return (
            "<b>Preprocessor passed with INFO</b>"
            "<br><hr>"
            f"INFO - No entries found in '{ entries_entries_dir }' folder."
            "Entries summary page could not be created"
            "<br><hr>"
        )

    # TODO - should check there are no files with same entry number (eg hazard-1 and hazard-01 and hazard-001)

    docstring = DocstringManipulation(self.project_id)
    referenced_hazards = docstring.docstring_all()

    for file in files_to_check:
        icon_html = ""
        function_hazards = []

        entry_file = open(file, "r")
        contents_str = entry_file.read()
        file_name = Path(file).stem

        try:
            entry_number = file_name.split("-")[1]
            if not entry_number.isdigit():
                entry_number = "[Non-digit entry number]"
                warnings += f"WARNING - A non-digit 'number' in entry file name '{ file_name }'"
        except:
            entry_number = "[Number not defined]"

        for function_info in referenced_hazards:
            for hazard in function_info["hazards"]:
                if hazard["hazard_number"] == entry_number:
                    function_hazards.append(function_info["mk_file_path"])

                    # print(entry_number)
                    # print(function_hazards)

        project = ProjectBuilder(self.project_id)
        # TODO - need to figure out which entry_types are preprocessed
        contents_list = project.entry_read_with_field_types("hazard", file)

        for field in contents_list:
            if field["field_type"] == "icon":
                env = Environment(
                    loader=FileSystemLoader(entry_template_dir),
                    autoescape=True,
                )
                template = env.get_template(f"{ entry_type }-icons.md")

                context = {"contents_list": contents_list}
                icon_html = template.render(context)
                icon_html = f"{ icon_html }\n<!-- [iconend] -->"

            elif field["field_type"] == "code":
                code_html = ""

                for function in referenced_hazards:
                    for hazard in function["hazards"]:
                        if hazard["hazard_number"] == entry_number:
                            code_html += (
                                f"[{ function['code_file'].replace('.py', '') }"
                                f".{ hazard['sub_routine']}](../../{ function['mk_file_path'] }"
                                f"#{ hazard['sub_routine']}_hazard)\n\n"
                            )

                code_html = code_html.rstrip("\n\n")

                if not code_html:
                    code_html = "Hazard not mentioned in source code"

        entry_form = project.entry_file_read_to_form(
            contents_list,
            icon_html,
            code_html,
        )

        project.entry_update(
            entry_form,
            entry_type,
            entry_number,
        )
        pattern = re.compile(
            r"<!--\s*\[icon\]\s*-->.*?<!--\s*\[iconend\]\s*-->",
            re.DOTALL,
        )
        contents_str = re.sub(pattern, "", contents_str)
        contents_str = contents_str.replace("../../", "../")
        icon_html = icon_html.replace("../../", "../")
        icon_html = icon_html.replace(
            'class="icon-large"',
            'class="icon-small"',
        )

        entries.append(
            {
                "number": entry_number,
                "contents_str": contents_str,
                "contents_list": contents_list,
                "icon_html": icon_html,
            }
        )

    # Creating the summary
    env = Environment(
        loader=FileSystemLoader(entry_template_dir), autoescape=True
    )
    template = env.get_template(
        f"{ entry_type }{ c.ENTRY_SUMMARY_SUFFIX }"
    )
    context = {"entries": entries}
    md_content = template.render(context)
    summary_file = open(
        f"{ entries_dir }{ entry_type }{ c.ENTRY_SUMMARY_SUFFIX }",
        "w",
    )
    summary_file.write(md_content)
    summary_file.close()

    if warnings:
        return (
            "<b>Successful preprocessor step with WARNINGS</b>"
            "<br><hr>"
            f"{ warnings }"
            "<br><hr>"
        )
    else:
        return "<b>Successful preprocessor step</b>" "<br><hr>"