(args)
| 244 | |
| 245 | |
| 246 | def handle_file_input(args): |
| 247 | config = {} |
| 248 | |
| 249 | # Fix the names first. |
| 250 | filename = make_tmp_file_with_renamed_fields(args.file) |
| 251 | with open(filename, "r+") as f: |
| 252 | lines = f.readlines() |
| 253 | num_lines = len(lines) |
| 254 | for idx in progressbar(range(num_lines), 40, mute=args.mute): |
| 255 | line = lines[idx] |
| 256 | if '#' == line[0] or '\n' == line[0] or ' ' == line[0]: |
| 257 | idx = idx + 1 |
| 258 | continue |
| 259 | s = line.split(' ', maxsplit=3) # keep values all together |
| 260 | name = s[1] |
| 261 | type = s[2] |
| 262 | value = s[3] |
| 263 | |
| 264 | track_info = (idx + 1, name) # in case we want to show any error. rec name is always handy. |
| 265 | # We ignore the prefix and work away from there. |
| 266 | if name.startswith("proxy.config."): |
| 267 | name = name[len("proxy.config."):] |
| 268 | elif name.startswith("local.config."): |
| 269 | name = name[len("local.config."):] |
| 270 | elif args.node and name.startswith("proxy.node."): |
| 271 | name = name[len("proxy."):] |
| 272 | |
| 273 | # Build the object (use rstrip to handle files without trailing newline) |
| 274 | add_object(config, name, value.rstrip('\n'), type, track_info) |
| 275 | idx = idx + 1 |
| 276 | |
| 277 | ts = {} |
| 278 | ts['records'] = config |
| 279 | |
| 280 | if args.schema: |
| 281 | err = validate_schema(ts, args.schema) |
| 282 | if err: |
| 283 | raise Exception("Schema failed.") |
| 284 | |
| 285 | save_to_file(args.output, args.json, args.typerepr, args.no_backup, ts) |
| 286 | |
| 287 | f.close() |
| 288 | |
| 289 | # Clean up the temp file we've created with the renamed fields. |
| 290 | try: |
| 291 | os.remove(filename) |
| 292 | except OSError as e: |
| 293 | raise Exception(f"Error removing temporary file {e.filename} - {e.strerror}. YAML file was created") |
| 294 | |
| 295 | return |
| 296 | |
| 297 | |
| 298 | if __name__ == '__main__': |
nothing calls this directly
no test coverage detected