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