Skip to content
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

Add --reporter parameter #10

Merged
merged 7 commits into from Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 26 additions & 7 deletions xiblint/__main__.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python
from xiblint import __version__
import argparse
import json
import os
import sys

Expand Down Expand Up @@ -31,7 +32,10 @@ def main():
)
parser.add_argument("-v", "--version", action="version",
version=__version__)
parser.parse_args()
parser.add_argument("--reporter", choices=("raw", "json"),
default="raw",
help="custom reporter to use")
args = parser.parse_args()

try:
config = Config()
Expand All @@ -47,25 +51,40 @@ def main():
#
# Process paths
#
success = True
errors = []
for path in config.include_paths:
for root, _, files in os.walk(path):
for file_path in [os.path.join(root, file) for file in files]:
checkers = config.checkers(file_path)
success = process_file(file_path, checkers) and success
errors += process_file(file_path, checkers, args.reporter)

sys.exit(0 if success else 1)
print_errors(errors, args.reporter)

sys.exit(len(errors) == 0)
Copy link
Contributor

@ikonst ikonst Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks incorrect. You want to exit with zero for success and non-zero for failure. You can also simply say:

sys.exit(1 if errors else 0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, my mistake. Was so ashamed of this line that I quickly copied/pasted this one and wanted to forget about it 😓 Now tested the statement using the python interpreter and it works for an empty array as well 🤔 Didn't know that it worked that way ;-)


def process_file(file_path, checkers):

def process_file(file_path, checkers, reporter):
_, ext = os.path.splitext(file_path)
if ext.lower() not in [u".storyboard", u".xib"]:
return True
return []
context = XibContext(file_path)
for rule_name, checker in checkers.items():
context.rule_name = rule_name
checker(context)
return context.success
return context.errors


def print_errors(errors, reporter):
if reporter == "raw":
for error_dict in errors:
print("{}:{}: error: {} [rule: {}]".format(
error_dict["file"],
error_dict["line"],
error_dict["error"],
error_dict["rule"],
))
elif reporter == "json":
print(json.dumps(errors))


if __name__ == '__main__':
Expand Down
17 changes: 12 additions & 5 deletions xiblint/xibcontext.py
Expand Up @@ -9,6 +9,7 @@ def __init__(self, path):
self.success = True
self.tree = parse_xml(path)
self.rule_name = None
self.errors = []

@staticmethod
def _get_moniker(view):
Expand All @@ -29,11 +30,17 @@ def error(self, element, message, *args):
self.success = False
object_id = get_object_id(element)
moniker = self._get_moniker(element)
print("{}:{}: error: {}{}{} [rule: {}]".format(
self.path,
element.line,

new_error = "{}{}{}".format(
"{}: ".format(object_id) if object_id else '',
"'{}': ".format(moniker) if moniker else '',
message.format(*args),
self.rule_name,
))
)
new_error_dict = {
"file": self.path,
"line": element.line,
"error": new_error,
"rule": self.rule_name,
}

self.errors.append(new_error_dict)