fix_broken_doxygen_formulae Namespace Reference

Functions

str load_file (Path fname)
 
str process_doxygen_formula_block (str text)
 
int find_number_of_leading_whitespaces (str text, int index)
 
None process_file (Path fname, str pattern, bool add_newline, bool in_place=False)
 
bool has_broken_formula (Path fname, str pattern)
 
List[Path] find_all_cpp_files (Path base_dir)
 
None run (str base_dir, bool in_place=False, bool just_list_files=False)
 

Variables

 parser = argparse.ArgumentParser(description='Fixes Doxygen formulae.')
 
 type
 
 default
 
 help
 
 action
 
 False
 
 args = parser.parse_args()
 
 base_dir
 
 in_place
 
 silent
 
 list_files
 
 stdout
 
 just_list_files
 

Function Documentation

◆ find_all_cpp_files()

List[Path] fix_broken_doxygen_formulae.find_all_cpp_files ( Path  base_dir)
Finds all the headers and source files in this directory and below.

:param base_dir: the path to the directory to search in and below.
:type base_dir: str
:return: an array of paths to header/source files.
:rtype: List[str]
165 def find_all_cpp_files(base_dir: Path) -> List[Path]:
166  """Finds all the headers and source files in this directory and below.
167 
168  :param base_dir: the path to the directory to search in and below.
169  :type base_dir: str
170  :return: an array of paths to header/source files.
171  :rtype: List[str]
172  """
173  headers = glob.glob(os.path.join(base_dir, "**/*.h"), recursive=True)
174  sources = glob.glob(os.path.join(base_dir, "**/*.cc"), recursive=True)
175  headers = [Path(h) for h in headers]
176  sources = [Path(s) for s in sources]
177  return headers + sources
178 
179 
List[Path] find_all_cpp_files(Path base_dir)
Definition: fix_broken_doxygen_formulae.py:165

Referenced by run().

◆ find_number_of_leading_whitespaces()

int fix_broken_doxygen_formulae.find_number_of_leading_whitespaces ( str  text,
int  index 
)
Given the index of a position in a text, it finds the corresponding line
in the text and the number of leading whitespaces on that line.

:param text: a block of text.
:type text: str
:param index: the index in 'text' to determine the number of leading
              whitespaces from.
:type index: int
:return: the number of leading whitespaces.
:rtype: int
49 def find_number_of_leading_whitespaces(text: str, index: int) -> int:
50  """Given the index of a position in a text, it finds the corresponding line
51  in the text and the number of leading whitespaces on that line.
52 
53  :param text: a block of text.
54  :type text: str
55  :param index: the index in 'text' to determine the number of leading
56  whitespaces from.
57  :type index: int
58  :return: the number of leading whitespaces.
59  :rtype: int
60  """
61  assert index < len(text)
62  line_number = text[:index].count("\n")
63  line = text.splitlines()[line_number]
64  n_leading_whitespace = len(line) - len(line.lstrip(" "))
65  return n_leading_whitespace
66 
67 
int find_number_of_leading_whitespaces(str text, int index)
Definition: fix_broken_doxygen_formulae.py:49

Referenced by process_file().

◆ has_broken_formula()

bool fix_broken_doxygen_formulae.has_broken_formula ( Path  fname,
str  pattern 
)
Identifies whether the file contains a broken Doxygen formula, i.e. the
formula is split over more than one line.


:param fname: the absolute path to a file.
:type fname: Path
:return: True if the file is "broken" and False otherwise.
:rtype: bool
145 def has_broken_formula(fname: Path, pattern: str) -> bool:
146  """Identifies whether the file contains a broken Doxygen formula, i.e. the
147  formula is split over more than one line.
148 
149 
150  :param fname: the absolute path to a file.
151  :type fname: Path
152  :return: True if the file is "broken" and False otherwise.
153  :rtype: bool
154  """
155  is_broken = False
156  regex_iterator = re.finditer(pattern, load_file(fname))
157  if regex_iterator:
158  for match in regex_iterator:
159  if "\n" in match.group():
160  is_broken = True
161  break
162  return is_broken
163 
164 
bool has_broken_formula(Path fname, str pattern)
Definition: fix_broken_doxygen_formulae.py:145
str load_file(Path fname)
Definition: fix_broken_doxygen_formulae.py:12

References load_file().

Referenced by run().

◆ load_file()

str fix_broken_doxygen_formulae.load_file ( Path  fname)
Reads in the data stored in the input file.

:param fname: the path to a file.
:type fname: Path
:return: the contents of 'fname'.
:rtype: str
12 def load_file(fname: Path) -> str:
13  """Reads in the data stored in the input file.
14 
15  :param fname: the path to a file.
16  :type fname: Path
17  :return: the contents of 'fname'.
18  :rtype: str
19  """
20  contents = None
21  with open(fname, "r") as f:
22  contents = f.read()
23  return contents
24 
25 

Referenced by has_broken_formula(), and process_file().

◆ process_doxygen_formula_block()

str fix_broken_doxygen_formulae.process_doxygen_formula_block ( str  text)
Removes newlines and comments from the passed Doxygen formula block.

:param text: a Doxygen formula block, possibly broken over several lines.
:type text: str
:return: the processed formula text, as a single line of text (no newlines).
:rtype: str
26 def process_doxygen_formula_block(text: str) -> str:
27  """Removes newlines and comments from the passed Doxygen formula block.
28 
29  :param text: a Doxygen formula block, possibly broken over several lines.
30  :type text: str
31  :return: the processed formula text, as a single line of text (no newlines).
32  :rtype: str
33  """
34  # Remove triple forward slashes first
35  processed_text = text.replace("///", "")
36 
37  # Remove any double forward slashes
38  processed_text = processed_text.replace("//", "")
39 
40  # Replace newlines with spaces
41  processed_text = processed_text.replace("\n", " ")
42 
43  # Squeeze whitespaces
44  processed_text = re.sub(" +", " ", processed_text)
45 
46  return processed_text
47 
48 
str process_doxygen_formula_block(str text)
Definition: fix_broken_doxygen_formulae.py:26

Referenced by process_file().

◆ process_file()

None fix_broken_doxygen_formulae.process_file ( Path  fname,
str  pattern,
bool  add_newline,
bool   in_place = False 
)
Performs regex magic to fix broken Doxygen formulae.

:param fname: the path to the file to fix.
:type fname: Path
:param add_newline: flag indicating whether new lines should be added around
                    the processed text, e.g. inline code might not but a
                    formula block might.
:type add_newline: bool
:param in_place: [description], defaults to False
:type in_place: bool, optional
68 def process_file(fname: Path, pattern: str, add_newline: bool, in_place: bool = False) -> None:
69  """Performs regex magic to fix broken Doxygen formulae.
70 
71  :param fname: the path to the file to fix.
72  :type fname: Path
73  :param add_newline: flag indicating whether new lines should be added around
74  the processed text, e.g. inline code might not but a
75  formula block might.
76  :type add_newline: bool
77  :param in_place: [description], defaults to False
78  :type in_place: bool, optional
79  """
80 
81  # Expand the "~/" in the filename and read the file in
82  text = load_file(fname)
83 
84  # Search for all matching patterns
85  regex_iterator = re.finditer(pattern, text)
86 
87  # Initialise update text
88  updated_text = ""
89  current_position = 0
90 
91  # If we found matches
92  if regex_iterator:
93  for match in regex_iterator:
94  # Get the (start, end) indices of the match in the original text
95  (posn_start, posn_end) = match.span()
96 
97  # Add on any text that lies between the end of the previous match
98  # and the start of the current match
99  updated_text += text[current_position:posn_start]
100 
101  # Find out the number of leading whitespaces on the line containing
102  # the formula. We can use this to decide how many whitespaces to add
103  # if we add any newlines before or after this block has been
104  # formatted
105  n_leading_whitespace = find_number_of_leading_whitespaces(
106  text, index=posn_start)
107 
108  # The text required to introduce a new comment line with the correct
109  # amount of leading whitespaces
110  new_comment_line = "\n" + (" " * n_leading_whitespace) + "///"
111 
112  # Extract the match as a string and process it into the desired form
113  processed_match = process_doxygen_formula_block(match.group())
114 
115  # Make sure the formula starts on its own line if it doesn't already
116  if add_newline and (updated_text.rstrip(" ")[-2:] != "//"):
117  updated_text += new_comment_line + " "
118 
119  # Place the processed formula on a new line
120  updated_text += processed_match
121 
122  # Make sure there is no text after the formula on the same line
123  if add_newline and (not text[posn_end:].lstrip(" ").startswith("\n")):
124  updated_text += new_comment_line
125 
126  # Continue on from the end of this string
127  current_position = posn_end
128 
129  # Add on the remaining text from the file
130  updated_text += text[current_position:]
131  else:
132  updated_text = text
133 
134  # Replace the original file, if requested. Otherwise, print out the changes
135  if in_place:
136  with open(os.path.expanduser(fname), "w") as f:
137  f.write(updated_text)
138  else:
139  print("\n===========================================================\n")
140  print("File: {}\n".format(fname))
141  print(updated_text)
142  print("\n===========================================================")
143 
144 
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet print(const Packet &a)
Definition: GenericPacketMath.h:1166
None process_file(Path fname, str pattern, bool add_newline, bool in_place=False)
Definition: fix_broken_doxygen_formulae.py:68
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:217

References find_number_of_leading_whitespaces(), format(), load_file(), Eigen::internal.print(), and process_doxygen_formula_block().

Referenced by run().

◆ run()

None fix_broken_doxygen_formulae.run ( str  base_dir,
bool   in_place = False,
bool   just_list_files = False 
)
Fixes all of the broken Doxygen formulae in the C++ source files of a
specific directory.

:param base_dir: the directory in and below which to consider.
:type base_dir: str
:param in_place: edits files in-place, defaults to False
:type in_place: bool, optional
:param just_list_files: [description], defaults to False
:type just_list_files: bool, optional
:return: [description]
:rtype: [type]
180 def run(base_dir: str, in_place: bool = False, just_list_files: bool = False) -> None:
181  """Fixes all of the broken Doxygen formulae in the C++ source files of a
182  specific directory.
183 
184  :param base_dir: the directory in and below which to consider.
185  :type base_dir: str
186  :param in_place: edits files in-place, defaults to False
187  :type in_place: bool, optional
188  :param just_list_files: [description], defaults to False
189  :type just_list_files: bool, optional
190  :return: [description]
191  :rtype: [type]
192  """
193  # A tuple of tuples, with each tuple containing (i) the formula to fix,
194  # (ii) the regex pattern to find the formula, and (iii) a flag indicating
195  # whether to add surrounding newlines around the formatted formula.
196  # !!! DO NOT EDIT !!!
197  doxygen_formulae_regex_patterns = (
198  (r"\f[ ... \f]", r"(\\f\[)([\S\s]*?)(\\f\])", True),
199  (r"\f$ ... \f$", r"(\\f\$)([\S\s]*?)(\\f\$)", False)
200  )
201 
202  # Find all header/source files to process
203  cpp_files = find_all_cpp_files(base_dir)
204 
205  # Iterate over the different types of Doxygen formulae patterns
206  for (formula_type, pattern, add_newline) in doxygen_formulae_regex_patterns:
207  def file_has_broken_formula(fname: Path):
208  return has_broken_formula(fname, pattern)
209 
210  # Find the files in which the pattern is broken over one or more lines
211  broken_files = list(filter(file_has_broken_formula, cpp_files))
212 
213  # Tell the user which pattern we're fixing
214  print("\nFixing formula: {}".format(formula_type))
215 
216  if len(broken_files) == 0:
217  print("\n ...but there are no broken files to process!")
218  continue
219 
220  # Spit out which files are broken
221  print("\nBroken files:")
222  for file in broken_files:
223  print(" * {}".format(file.relative_to(Path.cwd())))
224  print()
225 
226  # Now process each broken file
227  if not just_list_files:
228  print("\nProcessing file:")
229  for file in broken_files:
230  print(" * {}".format(file.relative_to(Path.cwd())))
231  process_file(file, pattern=pattern, in_place=in_place,
232  add_newline=add_newline)
233 
234 
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317

References find_all_cpp_files(), format(), has_broken_formula(), Eigen::internal.print(), and process_file().

Variable Documentation

◆ action

fix_broken_doxygen_formulae.action

◆ args

fix_broken_doxygen_formulae.args = parser.parse_args()

◆ base_dir

fix_broken_doxygen_formulae.base_dir

◆ default

fix_broken_doxygen_formulae.default

◆ False

fix_broken_doxygen_formulae.False

◆ help

fix_broken_doxygen_formulae.help

◆ in_place

◆ just_list_files

fix_broken_doxygen_formulae.just_list_files

◆ list_files

fix_broken_doxygen_formulae.list_files

◆ parser

fix_broken_doxygen_formulae.parser = argparse.ArgumentParser(description='Fixes Doxygen formulae.')

◆ silent

fix_broken_doxygen_formulae.silent

◆ stdout

fix_broken_doxygen_formulae.stdout

◆ type

fix_broken_doxygen_formulae.type