Conditions | 13 |
Total Lines | 63 |
Code Lines | 34 |
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 | """ |
||
130 | def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG): |
||
131 | """ |
||
132 | Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig |
||
133 | |||
134 | initLogging is to be called by OCR-D/core once, i.e. |
||
135 | - for the ``ocrd`` CLI |
||
136 | - for the processor wrapper methods |
||
137 | |||
138 | Other processes that use OCR-D/core as a library can, but do not have to, use this functionality. |
||
139 | |||
140 | Keyword Args: |
||
141 | - builtin_only (bool, False): Whether to search for logging configuration |
||
142 | on-disk (``False``) or only use the |
||
143 | hard-coded config (``True``). For testing |
||
144 | - force_reinit (bool, False): Whether to ignore the module-level |
||
145 | ``_initialized_flag``. For testing only. |
||
146 | - silent (bool, True): Whether to log logging behavior by printing to stderr |
||
147 | """ |
||
148 | global _initialized_flag |
||
149 | if _initialized_flag and not force_reinit: |
||
150 | return |
||
151 | # disableLogging() |
||
152 | |||
153 | # https://docs.python.org/3/library/logging.html#logging.disable |
||
154 | # If logging.disable(logging.NOTSET) is called, it effectively removes this |
||
155 | # overriding level, so that logging output again depends on the effective |
||
156 | # levels of individual loggers. |
||
157 | logging.disable(logging.NOTSET) |
||
158 | |||
159 | # remove all handlers for the ocrd root loggers |
||
160 | for logger_name in ROOT_OCRD_LOGGERS: |
||
161 | for handler in logging.getLogger(logger_name).handlers[:]: |
||
162 | logging.getLogger(logger_name).removeHandler(handler) |
||
163 | |||
164 | config_file = None |
||
165 | if not builtin_only: |
||
166 | CONFIG_PATHS = [ |
||
167 | Path.cwd(), |
||
168 | Path.home(), |
||
169 | Path('/etc'), |
||
170 | ] |
||
171 | config_file = [f for f \ |
||
172 | in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \ |
||
173 | if f.exists()] |
||
174 | if config_file: |
||
175 | if len(config_file) > 1 and not silent: |
||
176 | print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr) |
||
177 | config_file = config_file[0] |
||
178 | if not silent: |
||
179 | print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr) |
||
180 | logging.config.fileConfig(config_file) |
||
181 | else: |
||
182 | if not silent: |
||
183 | print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr) |
||
184 | # Default logging config |
||
185 | ocrd_handler = logging.StreamHandler(stream=sys.stderr) |
||
186 | ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT)) |
||
187 | ocrd_handler.setLevel(logging.DEBUG) |
||
188 | for logger_name in ROOT_OCRD_LOGGERS: |
||
189 | logging.getLogger(logger_name).addHandler(ocrd_handler) |
||
190 | for logger_name, logger_level in LOGGING_DEFAULTS.items(): |
||
191 | logging.getLogger(logger_name).setLevel(logger_level) |
||
192 | _initialized_flag = True |
||
193 | |||
224 |