Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
warnings_filter.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file warnings_filter.py
1 """
2 (taken from https://github.com/zephyrproject-rtos/zephyr/blob/main/doc/_extensions/zephyr/warnings_filter.py)
3 
4 Warnings filter extension
5 #########################
6 
7 Copyright (c) 2021 Nordic Semiconductor ASA
8 SPDX-License-Identifier: Apache-2.0
9 
10 Introduction
11 ============
12 
13 This Sphinx plugin can be used to filter out warnings that are known to be false
14 positives. The warnings are filtered out based on a set of regular expressions
15 given via an configuration file. The format of the configuration file is a
16 plain-text file where each line consists of a regular expression. Any lines
17 starting with ``#`` will be ignored.
18 
19 Configuration options
20 =====================
21 
22 - ``warnings_filter_config``: Configuration file.
23 - ``warnings_filter_silent``: Silent flag. If True, warning is hidden. If False
24  the warning is converted to an information message and displayed.
25 """
26 
27 import logging
28 import re
29 from typing import Dict, Any, List
30 
31 from sphinx.application import Sphinx
32 from sphinx.util.logging import NAMESPACE
33 
34 
35 __version__ = "0.1.0"
36 
37 
38 class WarningsFilter(logging.Filter):
39  """Warnings filter.
40 
41  Args:
42  expressions: List of regular expressions.
43  silent: If true, warning is hidden, otherwise it is shown as INFO.
44  name: Filter name.
45  """
46 
47  def __init__(self, expressions: List[str], silent: bool, name: str = "") -> None:
48  super().__init__(name)
49 
50  self._expressions = expressions
51  self._silent = silent
52 
53  def filter(self, record: logging.LogRecord) -> bool:
54  for expression in self._expressions:
55  try:
56  if re.match(expression, str(record.msg)):
57  if self._silent:
58  return False
59  else:
60  record.levelno = logging.INFO
61  record.msg = f"Filtered warning: {record.msg}"
62  return True
63  except:
64  print("ERROR??", expression, record.msg, type(record.msg))
65  raise
66 
67  return True
68 
69 
70 def configure(app: Sphinx) -> None:
71  """Entry point.
72 
73  Args:
74  app: Sphinx application instance.
75  """
76 
77  # load expressions from configuration file
78  with open(app.config.warnings_filter_config) as f:
79  expressions = list()
80  for line in f.readlines():
81  if not line.startswith("#"):
82  expressions.append(line.rstrip())
83 
84  # install warnings filter to all the Sphinx logger handlers
85  filter = WarningsFilter(expressions, app.config.warnings_filter_silent)
86  logger = logging.getLogger(NAMESPACE)
87  for handler in logger.handlers:
88  handler.filters.insert(0, filter)
89 
90 
91 def setup(app: Sphinx) -> Dict[str, Any]:
92  app.add_config_value("warnings_filter_config", "", "")
93  app.add_config_value("warnings_filter_silent", True, "")
94 
95  app.connect("builder-inited", configure)
96 
97  return {
98  "version": __version__,
99  "parallel_read_safe": True,
100  "parallel_write_safe": True,
101  }