Skip to content

Commit 963cffe

Browse files
committed
Refactory
1 parent a475c9c commit 963cffe

File tree

4 files changed

+26
-34
lines changed

4 files changed

+26
-34
lines changed

codec/validate.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
#!/usr/bin/env python
22

3-
import asn1tools
4-
import glob
53
import os
64
import sys
75

86
script_dir = os.path.dirname(os.path.abspath(__file__))
97
sys.path.append(os.path.abspath(os.path.join(script_dir, '../jam-types-asn')))
108

11-
from utils import get_schema_files, validate # noqa: E402
9+
from utils import validate_group # noqa: E402
1210

1311
os.chdir(script_dir)
1412

15-
def validate_spec(spec_name):
16-
print(f"[Validating '{spec_name}' spec]")
17-
schema = asn1tools.compile_files(get_schema_files(spec_name == "full"), codec="jer")
18-
for json_file in glob.glob(f"{spec_name}/*.json"):
19-
validate(schema, json_file)
20-
21-
validate_spec("tiny")
22-
validate_spec("full")
13+
for spec in ["tiny", "full"]:
14+
validate_group("codec", None, spec)

jam-types-asn/utils.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def make_asn1_parsable(json_str, json_tweaks_callback):
2727
return json_str
2828

2929

30-
def path_to_schema_name(path):
30+
def path_to_root_type(path):
3131
# Strip the directory path and extension
3232
name = os.path.splitext(os.path.basename(path))[0]
3333
# Remove "_X" if it ends with a number
@@ -39,22 +39,22 @@ def path_to_schema_name(path):
3939
return name
4040

4141

42-
def validate(schema, path, schema_name = None, json_tweaks_callback = None):
43-
print("* Validating: ", path)
42+
def validate(schema, json_file, root_type = None, json_tweaks_callback = None):
43+
print("* Validating: ", json_file)
4444

45-
if schema_name is None:
46-
schema_name = path_to_schema_name(path)
45+
if root_type is None:
46+
root_type = path_to_root_type(json_file)
4747

4848
# Decode from json using the schema
49-
json_bytes = open(path, "rb").read()
49+
json_bytes = open(json_file, "rb").read()
5050
json_str_org = json_bytes.decode('utf-8')
5151
json_str_org = make_asn1_parsable(json_str_org, json_tweaks_callback)
5252

5353
json_bytes = json_str_org.encode('utf-8')
54-
decoded = schema.decode(schema_name, json_bytes, check_constraints=True)
54+
decoded = schema.decode(root_type, json_bytes, check_constraints=True)
5555

5656
# Encode to json using the schema
57-
encoded = schema.encode(schema_name, decoded, check_constraints=True)
57+
encoded = schema.encode(root_type, decoded, check_constraints=True)
5858
# Original json uses snake case, asn1 requires kebab case
5959
json_str = encoded.decode('utf-8')
6060
json_obj = json.loads(json_str)
@@ -66,7 +66,15 @@ def validate(schema, path, schema_name = None, json_tweaks_callback = None):
6666

6767
def validate_group(group_name, group_schema, spec_name):
6868
print(f"\n[Validating {group_name} ({spec_name})]")
69-
schema = asn1tools.compile_files(get_schema_files(spec_name == "full") + [group_schema], codec="jer")
69+
schema_files = get_schema_files(spec_name == "full")
70+
if group_schema is not None:
71+
schema_files += [group_schema]
72+
schema = asn1tools.compile_files(schema_files, codec="jer")
73+
# A bit of a hack. Consider passing the schema root type as param
74+
if group_name == "codec":
75+
root_type = None
76+
else:
77+
root_type = "TestCase"
7078
for json_file in glob.glob(f"{spec_name}/*.json"):
71-
validate(schema, json_file, "TestCase")
79+
validate(schema, json_file, root_type)
7280

safrole/validate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
os.chdir(script_dir)
1212

13-
validate_group("safrole", "safrole.asn", "tiny")
14-
validate_group("safrole", "safrole.asn", "full")
13+
for spec in ["tiny", "full"]:
14+
validate_group("safrole", "safrole.asn", spec)

statistics/validate.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
#!/usr/bin/env python
22

3-
import asn1tools
4-
import glob
53
import os
64
import sys
75

86
script_dir = os.path.dirname(os.path.abspath(__file__))
97
sys.path.append(os.path.abspath(os.path.join(script_dir, '../jam-types-asn')))
108

11-
from utils import get_schema_files, validate # noqa: E402
9+
from utils import validate_group # noqa: E402
1210

1311
os.chdir(script_dir)
1412

15-
def validate_spec(spec_name):
16-
print(f"[Validating '{spec_name}' spec]")
17-
schema = asn1tools.compile_files(get_schema_files(spec_name == "full") + ["statistics.asn"], codec="jer")
18-
for json_file in glob.glob(f"{spec_name}/*.json"):
19-
validate(schema, json_file, "TestCase")
20-
21-
validate_spec("tiny")
22-
validate_spec("full")
13+
for spec in ["tiny", "full"]:
14+
validate_group("statistics", "statistics.asn", spec)

0 commit comments

Comments
 (0)