| Conditions | 7 |
| Total Lines | 66 |
| Code Lines | 30 |
| 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 | """ |
||
| 95 | def initLogging(builtin_only=False, basic_config=True, force_reinit=False): |
||
| 96 | """ |
||
| 97 | Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig |
||
| 98 | |||
| 99 | initLogging is to be called by OCR-D/core once, i.e. |
||
| 100 | - for the ``ocrd`` CLI |
||
| 101 | - for the processor wrapper methods |
||
| 102 | |||
| 103 | Other processes that use OCR-D/core as a library can, but do not have to, use this functionality. |
||
| 104 | |||
| 105 | Keyword Args: |
||
| 106 | - basic_config (bool, False): Whether to attach the handler to the |
||
| 107 | root logger instead of just the ``ocrd`` logger |
||
| 108 | like ``logging.basicConfig`` does. |
||
| 109 | - builtin_only (bool, False): Whether to search for logging configuration |
||
| 110 | on-disk (``False``) or only use the |
||
| 111 | hard-coded config (``True``). For testing |
||
| 112 | - force_reinit (bool, False): Whether to ignore the module-level |
||
| 113 | ``_initialized_flag``. For testing only. |
||
| 114 | """ |
||
| 115 | global _initialized_flag |
||
| 116 | if _initialized_flag and not force_reinit: |
||
| 117 | return |
||
| 118 | |||
| 119 | # https://docs.python.org/3/library/logging.html#logging.disable |
||
| 120 | # If logging.disable(logging.NOTSET) is called, it effectively removes this |
||
| 121 | # overriding level, so that logging output again depends on the effective |
||
| 122 | # levels of individual loggers. |
||
| 123 | logging.disable(logging.NOTSET) |
||
| 124 | |||
| 125 | # remove all handlers for the ocrd logger |
||
| 126 | for handler in logging.getLogger('ocrd').handlers[:]: |
||
| 127 | logging.getLogger('ocrd').removeHandler(handler) |
||
| 128 | |||
| 129 | config_file = None |
||
| 130 | if not builtin_only: |
||
| 131 | CONFIG_PATHS = [ |
||
| 132 | Path.cwd(), |
||
| 133 | Path.home(), |
||
| 134 | Path('/etc'), |
||
| 135 | ] |
||
| 136 | config_file = next((f for f \ |
||
|
|
|||
| 137 | in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \ |
||
| 138 | if f.exists()), |
||
| 139 | None) |
||
| 140 | if config_file: |
||
| 141 | logging.config.fileConfig(config_file) |
||
| 142 | logging.getLogger('ocrd.logging').debug("Picked up logging config at %s", config_file) |
||
| 143 | else: |
||
| 144 | # Default logging config |
||
| 145 | ocrd_handler = logging.StreamHandler(stream=sys.stderr) |
||
| 146 | ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT)) |
||
| 147 | if basic_config: |
||
| 148 | logging.getLogger('').addHandler(ocrd_handler) |
||
| 149 | else: |
||
| 150 | logging.getLogger('ocrd').addHandler(ocrd_handler) |
||
| 151 | logging.getLogger('ocrd').setLevel('INFO') |
||
| 152 | # logging.getLogger('ocrd.resolver').setLevel(logging.INFO) |
||
| 153 | # logging.getLogger('ocrd.resolver.download_to_directory').setLevel(logging.INFO) |
||
| 154 | # logging.getLogger('ocrd.resolver.add_files_to_mets').setLevel(logging.INFO) |
||
| 155 | logging.getLogger('PIL').setLevel(logging.INFO) |
||
| 156 | # To cut back on the `Self-intersection at or near point` INFO messages |
||
| 157 | logging.getLogger('shapely.geos').setLevel(logging.ERROR) |
||
| 158 | logging.getLogger('tensorflow').setLevel(logging.ERROR) |
||
| 159 | |||
| 160 | _initialized_flag = True |
||
| 161 | |||
| 186 |