Coverage for pyquickhelper/helpgen/stat_helper.py: 77%
56 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1"""
2@file
3@brief Functions about checking, statistics on files used in the documentation.
4"""
5import os
6import re
7from ..filehelper import explore_folder, explore_folder_iterfile
10def enumerate_notebooks_link(nb_folder, nb_rst):
11 """
12 Looks for all links to a notebook in the documentation.
14 @param nb_folder notebook folder
15 @param nb_rst documentation folder
16 @return iterator on *(rst_file, nb_file, link type, pos_start, pos_end, string, title)*
18 The function also outputs unreferenced notebooks.
19 *rst_file* is None in that case.
21 Example of outputs:
23 ::
25 ('...index_class.rst', '...having_a_form_in_a_notebook.ipynb', 'ref', 79880, 79912, ':ref:`havingaforminanotebookrst`')
26 ('...index_module.rst', '...having_a_form_in_a_notebook.ipynb', 'ref', 277928, 277960, ':ref:`havingaforminanotebookrst`')
28 """
29 # We check that all readme.txt follow utf-8.
30 for name in explore_folder_iterfile(nb_folder, "((readme)|(README))[.]txt$",
31 ".*((checkpoints)|(MACOSX)).*", fullname=True):
32 with open(name, "r", encoding="utf-8") as f:
33 try:
34 nbcontent = f.read()
35 except UnicodeDecodeError as e:
36 raise ValueError(
37 f"Issue with file '{name}'") from e
38 reg_title = re.compile("\\\"([#] [^#]+?)\\n")
40 rsts = explore_folder(nb_rst, ".*[.]rst$")[1]
41 crsts = {}
42 for rst in rsts:
43 with open(rst, "r", encoding="utf-8") as f:
44 try:
45 crsts[rst] = f.read()
46 except UnicodeDecodeError as e:
47 raise ValueError(
48 f"Issue with file '{rst}'") from e
50 nbcount = {}
52 for name in explore_folder_iterfile(nb_folder, ".*[.]ipynb$", ".*checkpoints.*", fullname=True):
53 with open(name, "r", encoding="utf-8") as f:
54 try:
55 nbcontent = f.read()
56 except UnicodeDecodeError as e:
57 raise ValueError(
58 f"Issue with file '{name}'") from e
59 reg_title = re.compile("\\\"([#] [^#]+?)\\n")
60 ftitle = reg_title.findall(nbcontent)
61 if len(ftitle) > 0:
62 title = ftitle[0].strip(" \n\r\t")
63 else:
64 title = None
65 sh = os.path.splitext(os.path.split(name)[-1])[0]
66 reg1 = re.compile("[/ ](" + sh + ")\\n")
67 reg2 = re.compile(f"(:ref:`.*? <{sh.replace('_', '')}rst>`)")
68 reg3 = re.compile(f"(:ref:`{sh.replace('_', '')}rst`)")
69 reg4 = re.compile("(<.*?" + sh + ">)\\n")
70 nbcount[name] = 0
71 for rst, content in crsts.items():
72 iter = reg1.finditer(content)
73 for it in iter:
74 nbcount[name] += 1
75 yield (rst, name, "toctree", it.start(0), it.end(0), it.groups(0)[0], title)
76 iter = reg4.finditer(content)
77 for it in iter:
78 nbcount[name] += 1
79 yield (rst, name, "toctreen", it.start(0), it.end(0), it.groups(0)[0], title)
80 iter = reg2.finditer(content)
81 for it in iter:
82 nbcount[name] += 1
83 yield (rst, name, "refn", it.start(0), it.end(0), it.groups(0)[0], title)
84 iter = reg3.finditer(content)
85 for it in iter:
86 nbcount[name] += 1
87 yield (rst, name, "ref", it.start(0), it.end(0), it.groups(0)[0], title)
88 if nbcount[name] == 0:
89 yield (None, name, None, -1, -1, "", title)