Reads structured config file defining a project.
(configname)
| 201 | |
| 202 | |
| 203 | def read_config(configname): |
| 204 | """Reads structured config file defining a project.""" |
| 205 | ruamelFile = YAML() |
| 206 | path = Path(configname) |
| 207 | if os.path.exists(path): |
| 208 | try: |
| 209 | with open(path) as f: |
| 210 | cfg = ruamelFile.load(f) |
| 211 | curr_dir = str(Path(configname).parent.resolve()) |
| 212 | |
| 213 | if cfg.get("engine") is None: |
| 214 | cfg["engine"] = Engine.TF.aliases[0] |
| 215 | write_config(configname, cfg) |
| 216 | |
| 217 | if cfg.get("detector_snapshotindex") is None: |
| 218 | cfg["detector_snapshotindex"] = -1 |
| 219 | |
| 220 | if cfg.get("detector_batch_size") is None: |
| 221 | cfg["detector_batch_size"] = 1 |
| 222 | |
| 223 | if cfg["project_path"] != curr_dir: |
| 224 | cfg["project_path"] = curr_dir |
| 225 | write_config(configname, cfg) |
| 226 | except Exception as err: |
| 227 | if len(err.args) > 2: |
| 228 | if err.args[2] == "could not determine a constructor for the tag '!!python/tuple'": |
| 229 | with open(path) as ymlfile: |
| 230 | cfg = yaml.load(ymlfile, Loader=yaml.SafeLoader) |
| 231 | write_config(configname, cfg) |
| 232 | else: |
| 233 | raise |
| 234 | |
| 235 | else: |
| 236 | raise FileNotFoundError( |
| 237 | f"Config file at {path} not found. Please make sure that the file exists and/or that you passed the path of" |
| 238 | f"the config file correctly!" |
| 239 | ) |
| 240 | return cfg |
| 241 | |
| 242 | |
| 243 | def write_config(configname, cfg): |
no test coverage detected