Conditions | 13 |
Total Lines | 56 |
Code Lines | 33 |
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 | """ |
||
135 | def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG): |
||
136 | """ |
||
137 | Reset ``ocrd`` logger, read logging configuration if exists, otherwise use :py:meth:`logging.basicConfig` |
||
138 | |||
139 | This is to be called by OCR-D/core only once, i.e. |
||
140 | - for the ``ocrd`` CLI |
||
141 | - for the processor wrapper methods |
||
142 | |||
143 | Other processes that use OCR-D/core as a library can, but do not have to, use this functionality. |
||
144 | |||
145 | Keyword Args: |
||
146 | - builtin_only (bool): Whether to search for logging configuration |
||
147 | on-disk (``False``) or only use the hard-coded config (``True``). |
||
148 | For testing |
||
149 | - force_reinit (bool): Whether to ignore the module-level ``_initialized_flag``. |
||
150 | For testing only |
||
151 | - silent (bool): Whether to log logging behavior by printing to stderr |
||
152 | """ |
||
153 | global _initialized_flag |
||
154 | if _initialized_flag: |
||
155 | if force_reinit: |
||
156 | disableLogging(silent=silent) |
||
157 | else: |
||
158 | return |
||
159 | |||
160 | config_file = None |
||
161 | if not builtin_only: |
||
162 | config_file = get_logging_config_files() |
||
163 | if config_file: |
||
164 | if len(config_file) > 1 and not silent: |
||
165 | print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr) |
||
166 | config_file = config_file[0] |
||
167 | if not silent: |
||
168 | print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr) |
||
169 | logging.config.fileConfig(config_file) |
||
170 | # Set permission of processing-server logfile to 666 to prevent possible permission erros while logging |
||
171 | try: |
||
172 | network_logger = logging.getLogger("ocrd_network") |
||
173 | for handler in network_logger.handlers: |
||
174 | if isinstance(handler, logging.FileHandler): |
||
175 | chmod(handler.baseFilename, 0o666) |
||
176 | except PermissionError: |
||
177 | # if the file exists the permissions are supposed to already fit |
||
178 | pass |
||
179 | else: |
||
180 | if not silent: |
||
181 | print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr) |
||
182 | # Default logging config |
||
183 | ocrd_handler = logging.StreamHandler(stream=sys.stderr) |
||
184 | ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT)) |
||
185 | ocrd_handler.setLevel(logging.DEBUG) |
||
186 | root_logger = logging.getLogger('') |
||
187 | root_logger.addHandler(ocrd_handler) |
||
188 | for logger_name, logger_level in LOGGING_DEFAULTS.items(): |
||
189 | logging.getLogger(logger_name).setLevel(logger_level) |
||
190 | _initialized_flag = True |
||
191 | |||
216 |