Use attrs class for report

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille 2023-10-11 10:32:50 +02:00
parent 785a81cd5e
commit 635035d74b
No known key found for this signature in database
GPG key ID: 29A48C1F03620416
2 changed files with 24 additions and 17 deletions

View file

@ -53,6 +53,20 @@ except ImportError as e:
import_error("jsonpath", "python-jsonpath", "jsonpath", e) import_error("jsonpath", "python-jsonpath", "jsonpath", e)
raise 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): def load_file(path):
if not path.startswith("file://"): if not path.startswith("file://"):
@ -134,16 +148,12 @@ def check_schema_file(schema_path):
# Check schema examples are valid. # Check schema examples are valid.
check_schema_examples(schema_path, schema) check_schema_examples(schema_path, schema)
def check_schema_dir(schemadir): def check_schema_dir(schemadir: str) -> SchemaDirReport:
report = { report = SchemaDirReport()
"files": 0,
"errors": 0,
}
for root, dirs, files in os.walk(schemadir): for root, dirs, files in os.walk(schemadir):
for schemadir in dirs: for schemadir in dirs:
dir_report = check_schema_dir(os.path.join(root, schemadir)) dir_report = check_schema_dir(os.path.join(root, schemadir))
report["files"] += dir_report["files"] report.add(dir_report)
report["errors"] += dir_report["errors"]
for filename in files: for filename in files:
if filename.startswith("."): if filename.startswith("."):
# Skip over any vim .swp files. # Skip over any vim .swp files.
@ -152,10 +162,10 @@ def check_schema_dir(schemadir):
# Skip over any explicit examples (partial event definitions) # Skip over any explicit examples (partial event definitions)
continue continue
try: try:
report["files"] += 1 report.files += 1
check_schema_file(os.path.join(root, filename)) check_schema_file(os.path.join(root, filename))
except Exception as e: except Exception as e:
report["errors"] += 1 report.errors += 1
return report return report
# The directory that this script is residing in. # The directory that this script is residing in.
@ -174,17 +184,13 @@ schema_dirs = [
"schemas", "schemas",
] ]
report = { report = SchemaDirReport()
"files": 0,
"errors": 0,
}
for schema_dir in schema_dirs: for schema_dir in schema_dirs:
dir_report = check_schema_dir(os.path.join(project_dir, "data", schema_dir)) dir_report = check_schema_dir(os.path.join(project_dir, "data", schema_dir))
report["files"] += dir_report["files"] report.add(dir_report)
report["errors"] += dir_report["errors"]
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) sys.exit(1)

View file

@ -5,6 +5,7 @@
jsonschema == 4.17.3 jsonschema == 4.17.3
python-jsonpath == 0.9.0 python-jsonpath == 0.9.0
attrs >= 23.1.0
PyYAML >= 3.12 PyYAML >= 3.12
requests >= 2.18.4 requests >= 2.18.4
towncrier == 23.6.0 towncrier == 23.6.0