Conditions | 14 |
Total Lines | 59 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ocrd_utils.logging.initLogging() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
145 | def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG): |
||
146 | """ |
||
147 | Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig |
||
148 | |||
149 | initLogging is to be called by OCR-D/core once, i.e. |
||
150 | - for the ``ocrd`` CLI |
||
151 | - for the processor wrapper methods |
||
152 | |||
153 | Other processes that use OCR-D/core as a library can, but do not have to, use this functionality. |
||
154 | |||
155 | Keyword Args: |
||
156 | - builtin_only (bool, False): Whether to search for logging configuration |
||
157 | on-disk (``False``) or only use the |
||
158 | hard-coded config (``True``). For testing |
||
159 | - force_reinit (bool, False): Whether to ignore the module-level |
||
160 | ``_initialized_flag``. For testing only. |
||
161 | - silent (bool, True): Whether to log logging behavior by printing to stderr |
||
162 | """ |
||
163 | global _initialized_flag |
||
164 | if _initialized_flag and not force_reinit: |
||
165 | return |
||
166 | # disableLogging() |
||
167 | |||
168 | # https://docs.python.org/3/library/logging.html#logging.disable |
||
169 | # If logging.disable(logging.NOTSET) is called, it effectively removes this |
||
170 | # overriding level, so that logging output again depends on the effective |
||
171 | # levels of individual loggers. |
||
172 | logging.disable(logging.NOTSET) |
||
173 | |||
174 | # remove all handlers for the ocrd root loggers |
||
175 | for logger_name in ROOT_OCRD_LOGGERS: |
||
176 | for handler in logging.getLogger(logger_name).handlers[:]: |
||
177 | logging.getLogger(logger_name).removeHandler(handler) |
||
178 | |||
179 | config_file = None |
||
180 | if not builtin_only: |
||
181 | config_file = get_logging_config_files() |
||
182 | if config_file: |
||
183 | if len(config_file) > 1 and not silent: |
||
184 | print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr) |
||
185 | config_file = config_file[0] |
||
186 | if not silent: |
||
187 | print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr) |
||
188 | logging.config.fileConfig(config_file) |
||
189 | else: |
||
190 | if not silent: |
||
191 | print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr) |
||
192 | # Default logging config |
||
193 | ocrd_handler = logging.StreamHandler(stream=sys.stderr) |
||
194 | ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT)) |
||
195 | ocrd_handler.setLevel(logging.DEBUG) |
||
196 | for logger_name in ROOT_OCRD_LOGGERS: |
||
197 | logger = logging.getLogger(logger_name) |
||
198 | logger.addHandler(ocrd_handler) |
||
199 | if logger_name: |
||
200 | logger.propagate = False # avoid duplication (from root handler) |
||
201 | for logger_name, logger_level in LOGGING_DEFAULTS.items(): |
||
202 | logging.getLogger(logger_name).setLevel(logger_level) |
||
203 | _initialized_flag = True |
||
204 | |||
237 |