Skip to content

Commit

Permalink
Use attrs class for report
Browse files Browse the repository at this point in the history
Signed-off-by: Kévin Commaille <[email protected]>
  • Loading branch information
zecakeh committed Oct 11, 2023
1 parent 785a81c commit 635035d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
40 changes: 23 additions & 17 deletions scripts/check-json-schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def import_error(module, package, debian, error):
import_error("jsonpath", "python-jsonpath", "jsonpath", e)
raise

try:
import attrs
except ImportError as e:
import_error("attrs", "attrs", "attrs", e)
raise

@attrs.define
class SchemaDirReport:
files: int = 0
errors: int = 0

def add(self, other_report):
self.files += other_report.files
self.errors += other_report.errors

def load_file(path):
if not path.startswith("file://"):
Expand Down Expand Up @@ -134,16 +148,12 @@ def check_schema_file(schema_path):
# Check schema examples are valid.
check_schema_examples(schema_path, schema)

def check_schema_dir(schemadir):
report = {
"files": 0,
"errors": 0,
}
def check_schema_dir(schemadir: str) -> SchemaDirReport:
report = SchemaDirReport()
for root, dirs, files in os.walk(schemadir):
for schemadir in dirs:
dir_report = check_schema_dir(os.path.join(root, schemadir))
report["files"] += dir_report["files"]
report["errors"] += dir_report["errors"]
report.add(dir_report)
for filename in files:
if filename.startswith("."):
# Skip over any vim .swp files.
Expand All @@ -152,10 +162,10 @@ def check_schema_dir(schemadir):
# Skip over any explicit examples (partial event definitions)
continue
try:
report["files"] += 1
report.files += 1
check_schema_file(os.path.join(root, filename))
except Exception as e:
report["errors"] += 1
report.errors += 1
return report

# The directory that this script is residing in.
Expand All @@ -174,17 +184,13 @@ def check_schema_dir(schemadir):
"schemas",
]

report = {
"files": 0,
"errors": 0,
}
report = SchemaDirReport()
for schema_dir in schema_dirs:
dir_report = check_schema_dir(os.path.join(project_dir, "data", schema_dir))
report["files"] += dir_report["files"]
report["errors"] += dir_report["errors"]
report.add(dir_report)

print(f"Found {report['errors']} errors in {report['files']} files")
print(f"Found {report.errors} errors in {report.files} files")

if report["errors"]:
if report.errors:
sys.exit(1)

1 change: 1 addition & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
jsonschema == 4.17.3
python-jsonpath == 0.9.0

attrs >= 23.1.0
PyYAML >= 3.12
requests >= 2.18.4
towncrier == 23.6.0

0 comments on commit 635035d

Please sign in to comment.