def process_file(source: str, result_filename: str = '') -> str: """Compiles the source and writes the serialized results back to disk, unless any fatal errors have occurred. Error and Warning messages are written to a file with the same name as `result_filename` with an appended "_ERRORS.txt" or "_WARNINGS.txt" in place of the name's extension. Returns the name of the error-messages file or an empty string, if no errors of warnings occurred. """ source_filename = source if is_filename(source) else '' result, errors = compile_src(source) if not has_errors(errors, FATAL): if os.path.abspath(source_filename) != os.path.abspath( result_filename): with open(result_filename, 'w') as f: f.write(serialize_result(result)) else: errors.append( Error( 'Source and destination have the same name "%s"!' % result_filename, 0, FATAL)) if errors: err_ext = '_ERRORS.txt' if has_errors(errors, ERROR) else '_WARNINGS.txt' err_filename = os.path.splitext(result_filename)[0] + err_ext with open(err_filename, 'w') as f: f.write('\n'.join(canonical_error_strings(errors))) return err_filename return ''
batch_processing = False if batch_processing: if not os.path.exists(out): os.mkdir(out) elif not os.path.isdir(out): print('Output directory "%s" exists and is not a directory!' % out) sys.exit(1) error_files = batch_process(file_names, out, log_func=print if args.verbose else None) if error_files: category = "ERRORS" if any(f.endswith('_ERRORS.txt') for f in error_files) \ else "warnings" print("There have been %s! Please check files:" % category) print('\n'.join(error_files)) if category == "ERRORS": sys.exit(1) else: result, errors = compile_src(file_names[0]) if errors: for err_str in canonical_error_strings(errors): print(err_str) if has_errors(errors, ERROR): sys.exit(1) print( result.serialize(how='default' if args.xml is None else 'xml' ) if isinstance(result, Node) else result)