Skip to content

Commit

Permalink
Fixing Sonarlint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Teester committed Feb 23, 2024
1 parent 752a1a7 commit 583cf50
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
76 changes: 44 additions & 32 deletions comparejsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,32 @@ def compare_properties(self):
if self._start_shape is None:
return properties
for prop in self._props:
utilities = Utilities()
child: dict = {"name": self._names[prop],
"necessity": Utilities.calculate_necessity(prop, self._start_shape)}
"necessity": utilities.calculate_necessity(prop, self._start_shape)}
if prop in claims:
cardinality: str = self._process_cardinalities(claims[prop], self._start_shape)
allowed: str = "allowed"
if "expression" in self._start_shape and "expressions" in self._start_shape["expression"]:
for expression in self._start_shape["expression"]["expressions"]:
allowed = self._process_triple_constraint(claims[prop][0]["mainsnak"],
expression,
allowed)
if cardinality == "correct":
response = allowed
else:
response = cardinality
response = self.check_claims_for_props(claims, prop)
else:
response = "missing"
if response != "":
child["response"] = response
properties[prop] = child
return properties

def check_claims_for_props(self, claims, prop):
cardinality: str = self._process_cardinalities(claims[prop], self._start_shape)
allowed: str = "allowed"
if "expression" in self._start_shape and "expressions" in self._start_shape["expression"]:
for expression in self._start_shape["expression"]["expressions"]:
allowed = self._process_triple_constraint(claims[prop][0]["mainsnak"],
expression,
allowed)
if cardinality == "correct":
response = allowed
else:
response = cardinality
return response

@staticmethod
def _process_cardinalities(claims: dict, shape: dict):
"""
Expand Down Expand Up @@ -256,7 +261,8 @@ def compare_statements(self) -> dict:
property_statement_results: list = []
for statement in claims[claim]:
child: dict = {"property": claim}
necessity = Utilities.calculate_necessity(statement["mainsnak"]["property"], self.start_shape)
utilities = Utilities()
necessity = utilities.calculate_necessity(statement["mainsnak"]["property"], self.start_shape)
if necessity != "absent":
child["necessity"] = necessity
child, allowed = self._process_shape(statement["mainsnak"], self.start_shape, child)
Expand All @@ -280,18 +286,22 @@ def _process_shape(self, statement, shape, child) -> Tuple[Any, str]:
expressions = shape["expression"]["expressions"]
allowed: str = "not in schema"
for expression in expressions:
if expression["type"] == "TripleConstraint" and expression["predicate"].endswith(statement["property"]):
allowed = self._process_triple_constraint(statement,
expression,
allowed)
if "extra" in shape:
for extra in shape["extra"]:
if extra.endswith(statement["property"]) and allowed == "incorrect":
allowed = "allowed"
allowed = self.process_expressions(expression, shape, statement, allowed)
if allowed != "":
child["response"] = allowed
return child, allowed

def process_expressions(self, expression, shape, statement, allowed):
if expression["type"] == "TripleConstraint" and expression["predicate"].endswith(statement["property"]):
allowed = self._process_triple_constraint(statement,
expression,
allowed)
if "extra" in shape:
for extra in shape["extra"]:
if extra.endswith(statement["property"]) and allowed == "incorrect":
allowed = "allowed"
return allowed

@staticmethod
def _process_triple_constraint(statement, expression, allowed):
"""
Expand All @@ -318,8 +328,8 @@ def _process_triple_constraint(statement, expression, allowed):


class Utilities:
@staticmethod
def calculate_necessity(prop: str, shape: dict) -> str:

def calculate_necessity(self, prop: str, shape: dict) -> str:
"""
Check if a property is required, optional or absent from a shape
Expand All @@ -330,15 +340,17 @@ def calculate_necessity(prop: str, shape: dict) -> str:
necessity: str = "absent"
if "expression" in shape and "expressions" in shape["expression"]:
for expression in shape["expression"]["expressions"]:
if "predicate" in expression and \
expression["predicate"].endswith(prop):
necessity = "optional"
if ("min" in expression and expression["min"] > 0) or \
("min" not in expression and "max" not in expression):
necessity = "required"
if "min" in expression and "max" in expression and \
expression["min"] == 0 and expression["max"] == 0:
necessity = "absent"
if "predicate" in expression and expression["predicate"].endswith(prop):
necessity = self.required_or_absent(expression)
return necessity

@staticmethod
def required_or_absent(expression) -> str:
necessity = "optional"
if ("min" in expression and expression["min"] > 0) or ("min" not in expression and "max" not in expression):
necessity = "required"
if "min" in expression and "max" in expression and expression["min"] == 0 and expression["max"] == 0:
necessity = "absent"
return necessity

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _assess_property(self, line: str, child: dict):
child["shape"] = sub_shape_name[1:-1]
if re.search(r"\[.*]", line):
required_parameters_string: str = re.search(r"\[.*]", line).group(0)
required_parameters_string = re.sub(r"wd:", "", required_parameters_string)
required_parameters_string = required_parameters_string.replace("wd:", "")
if "^" in line:
child["not_allowed"] = required_parameters_string[1:-1].split()
else:
Expand Down

0 comments on commit 583cf50

Please sign in to comment.