Skip to content

Commit

Permalink
Move AttributeType to models
Browse files Browse the repository at this point in the history
Signed-off-by: romanodanilo <[email protected]>
  • Loading branch information
romanodanilo committed Jul 11, 2024
1 parent a662908 commit dbc0fb1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
7 changes: 7 additions & 0 deletions qc_openscenario/checks/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from lxml import etree
from typing import Union
from enum import Enum

from qc_baselib import Configuration, Result

Expand All @@ -12,3 +13,9 @@ class CheckerData:
result: Result
schema_version: str
xodr_root: Union[None, etree._ElementTree]


class AttributeType(Enum):
VALUE = 0
EXPRESSION = 1
PARAMETER = 2
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def check_rule(checker_data: models.CheckerData) -> None:
for attr_name, attr_value in node_with_parameter_attribute.attrib.items():
if (
utils.get_attribute_type(attr_value)
== utils.AttributeType.PARAMETER
== models.AttributeType.PARAMETER
and attr_value[1:] not in defined_parameters_with_default
):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def check_rule(checker_data: models.CheckerData) -> None:
# Check if entityRef points to a declared param
if (
utils.get_attribute_type(current_entity_ref)
== utils.AttributeType.PARAMETER
== models.AttributeType.PARAMETER
):
current_entity_param_name = current_entity_ref[1:]
current_entity_param_value = utils.get_parameter_value(
Expand Down
20 changes: 7 additions & 13 deletions qc_openscenario/checks/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
from lxml import etree
from typing import Union
from enum import Enum
from qc_openscenario.checks import models
import re

EXPRESSION_PATTERN = re.compile("[$][{][ A-Za-z0-9_\+\-\*/%$\(\)\.,]*[\}]")
PARAMETER_PATTERN = re.compile("[$][A-Za-z_][A-Za-z0-9_]*")


class AttributeType(Enum):
STRING = 0
EXPRESSION = 1
PARAMETER = 2


def get_standard_schema_version(root: etree._ElementTree) -> Union[str, None]:
header = root.find("FileHeader")
if header is None:
Expand Down Expand Up @@ -104,7 +98,7 @@ def get_xodr_road_network(root: etree._ElementTree) -> Union[etree._ElementTree,
return None

# If filepath is specified using param, get all param declaration and update the filepath
if get_attribute_type(filepath) == AttributeType.PARAMETER:
if get_attribute_type(filepath) == models.AttributeType.PARAMETER:
filepath_param = filepath[1:]
filepath = get_parameter_value(root, filepath_param)
if filepath is None:
Expand All @@ -113,22 +107,22 @@ def get_xodr_road_network(root: etree._ElementTree) -> Union[etree._ElementTree,
return etree.parse(filepath)


def get_attribute_type(attribute_value: str) -> AttributeType:
def get_attribute_type(attribute_value: str) -> models.AttributeType:
"""Given attribute value as input, checks if it is an expression, a parameter or a plain string
Args:
attribute_value (str): the attribute value to check
Returns:
AttributeType: enum representing whether attribute value is
models.AttributeType: enum representing whether attribute value is
- expression (AttributeType.EXPRESSION),
- parameter (AttributeType.PARAMETER)
- a plain string (AttributeType.STRING)
"""

if EXPRESSION_PATTERN.match(attribute_value):
return AttributeType.EXPRESSION
return models.AttributeType.EXPRESSION
elif PARAMETER_PATTERN.match(attribute_value):
return AttributeType.PARAMETER
return models.AttributeType.PARAMETER

return AttributeType.STRING
return models.AttributeType.VALUE

0 comments on commit dbc0fb1

Please sign in to comment.