| Conditions | 7 |
| Total Lines | 20 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | from pathlib import Path |
||
| 12 | def load_config_file(basedir=None): |
||
| 13 | """ |
||
| 14 | Load the configuration file |
||
| 15 | """ |
||
| 16 | if not basedir: |
||
| 17 | basedir = ocrd_utils.XDG_CONFIG_HOME |
||
| 18 | fpath = Path(basedir, 'ocrd', 'config.yml') |
||
| 19 | if not fpath.parent.exists(): |
||
| 20 | fpath.parent.mkdir(parents=True) |
||
| 21 | obj = DEFAULT_CONFIG |
||
| 22 | if not fpath.exists(): |
||
| 23 | with open(str(fpath), 'w', encoding='utf-8') as f_out: |
||
| 24 | f_out.write("# Generated by OCR-D/core %s on %s\n" % (VERSION, datetime.now())) |
||
| 25 | f_out.write(safe_dump(obj)) |
||
| 26 | with open(str(fpath), 'r', encoding='utf-8') as f_in: |
||
| 27 | obj = {**obj, **safe_load(f_in.read())} |
||
| 28 | report = OcrdConfigValidator.validate(obj) |
||
| 29 | if not report.is_valid: |
||
| 30 | raise ValueError("The configuration is invalid: %s" % report.errors) |
||
| 31 | return OcrdConfig(obj) |
||
| 32 |