-
Notifications
You must be signed in to change notification settings - Fork 61
convert.py: Don't exit on error #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import os.path | ||
| import stat | ||
| import sys | ||
| from typing import Dict | ||
| import lxml.etree as ET | ||
|
|
||
|
|
||
|
|
@@ -52,54 +53,76 @@ def write_config(outData, time, filename=None): | |
| print("Writing %s" % filename) | ||
| file = codecs.open(filename, "wb") | ||
| file.write(outData) | ||
| file.write(b'\n') | ||
| file.write(b"\n") | ||
| file.close() | ||
|
|
||
|
|
||
| def write_domains(doc, time, output_dir="."): | ||
| outData = doc_to_bytestring(doc) | ||
| for d in doc.findall("//domain"): | ||
| for d in doc.getroot().findall(".//domain"): | ||
| write_config(outData, time, output_dir + "/" + d.text) | ||
|
|
||
|
|
||
| def main(): | ||
| # parse command line options | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("-d", metavar="dir", | ||
| help="output directory") | ||
| parser.add_argument("-a", action="store_true", | ||
| help="write configuration files for all domains") | ||
| parser.add_argument("file", nargs="*", | ||
| help="input file(s) to process, wildcards allowed") | ||
| parser.add_argument("-d", metavar="dir", help="output directory") | ||
| parser.add_argument( | ||
| "-a", action="store_true", help="write configuration files for all domains" | ||
| ) | ||
| parser.add_argument( | ||
| "file", nargs="*", help="input file(s) to process, wildcards allowed" | ||
| ) | ||
|
Comment on lines
+69
to
+75
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like VS Code's formatter kicked in. No big deal. |
||
| args = parser.parse_args(sys.argv[1:]) | ||
|
|
||
| # process arguments | ||
| convertTime = os.stat(sys.argv[0]).st_mtime | ||
| is_dir = stat.S_ISDIR | ||
|
|
||
| # Record the files that failed to be processed and the errors related to | ||
| # them. | ||
| failed_files: Dict[str, Exception] = {} | ||
|
|
||
| for f in args.file: | ||
| if is_dir(os.stat(f).st_mode): | ||
| continue | ||
| try: | ||
| if is_dir(os.stat(f).st_mode): | ||
| continue | ||
|
|
||
| if f == "README": | ||
| continue | ||
|
|
||
| print(f"Processing {f}") | ||
|
|
||
| doc, time = read_config(f, convertTime) | ||
|
|
||
| if args.a: | ||
| if args.d: | ||
| write_domains(doc, time, args.d) | ||
| else: | ||
| print("When you want to write domain files you") | ||
| print("should also specify an output directory") | ||
| print("using -d dir") | ||
| parser.print_usage() | ||
| exit(2) | ||
| elif args.d: | ||
| outData = doc_to_bytestring(doc) | ||
| write_config(outData, time, args.d + "/" + os.path.basename(f)) | ||
| else: | ||
| print_config(doc) | ||
| except Exception as e: | ||
| print(f"File {f} could not be processed: {e}") | ||
| failed_files[f] = e | ||
|
|
||
| if f == "README": | ||
| continue | ||
| if len(failed_files) > 0: | ||
| # Print the failed files, preceded by an empty line to separate them | ||
| # from the previous logs. | ||
| print() | ||
| print("Processing the following file(s) has failed:") | ||
|
|
||
| doc, time = read_config(f, convertTime) | ||
| for file, exc in failed_files.items(): | ||
| print(f"{file}: {exc}") | ||
|
|
||
| if args.a: | ||
| if args.d: | ||
| write_domains(doc, time, args.d) | ||
| else: | ||
| print("When you want to write domain files you") | ||
| print("should also specify an output directory") | ||
| print("using -d dir") | ||
| parser.print_usage() | ||
| exit(2) | ||
| elif args.d: | ||
| outData = doc_to_bytestring(doc) | ||
| write_config(outData, time, args.d + "/" + os.path.basename(f)) | ||
| else: | ||
| print_config(doc) | ||
| exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was printing a deprecation error, so I fixed it while I was there.