Skip to content

Commit

Permalink
Merge pull request strictdoc-project#1941 from strictdoc-project/stan…
Browse files Browse the repository at this point in the history
…islaw/code_climate2

Code climate: SDocNode: requirement_type -> node_type
  • Loading branch information
stanislaw authored Aug 24, 2024
2 parents a6a0bda + 81153f3 commit 631f9b6
Show file tree
Hide file tree
Showing 33 changed files with 84 additions and 94 deletions.
4 changes: 2 additions & 2 deletions strictdoc/backend/excel/import_/excel_to_sdoc_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def create_requirement(

template_requirement = SDocObjectFactory.create_requirement(
document,
requirement_type="REQUIREMENT",
node_type="REQUIREMENT",
title=title,
uid=uid,
level=None,
Expand Down Expand Up @@ -214,7 +214,7 @@ def create_requirement(
template_requirement.relations = [reference]
requirement = SDocNode(
parent=template_requirement.parent,
requirement_type=template_requirement.requirement_type,
node_type=template_requirement.node_type,
fields=list(template_requirement.enumerate_fields()),
relations=template_requirement.relations,
)
Expand Down
4 changes: 2 additions & 2 deletions strictdoc/backend/reqif/p01_sdoc/reqif_to_sdoc_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def create_section_from_spec_object(
)
node: SDocNode = SDocNode(
parent=section,
requirement_type="TEXT",
node_type="TEXT",
fields=[node_field],
relations=[],
)
Expand Down Expand Up @@ -533,7 +533,7 @@ def create_requirement_from_spec_object(
)
requirement = SDocNode(
parent=parent_section,
requirement_type=grammar_element.tag,
node_type=grammar_element.tag,
fields=fields,
relations=[],
)
Expand Down
4 changes: 2 additions & 2 deletions strictdoc/backend/reqif/p01_sdoc/sdoc_to_reqif_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def _convert_requirement_to_spec_object(
else:
requirement_identifier = generate_unique_identifier("REQUIREMENT")

grammar_element = grammar.elements_by_type[requirement.requirement_type]
grammar_element = grammar.elements_by_type[requirement.node_type]

attributes: List[SpecObjectAttribute] = []
for field in requirement.fields_as_parsed:
Expand Down Expand Up @@ -574,7 +574,7 @@ def _convert_requirement_to_spec_object(
spec_object_type: ReqIFSpecObjectType = (
context.map_grammar_node_tags_to_spec_object_type[
requirement.document
][requirement.requirement_type]
][requirement.node_type]
)
spec_object = ReqIFSpecObject.create(
identifier=requirement_identifier,
Expand Down
26 changes: 12 additions & 14 deletions strictdoc/backend/sdoc/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def __init__(
self.file_path = filename

@staticmethod
def unknown_requirement_type(node: SDocNode, path_to_sdoc_file: str):
def unknown_node_type(node: SDocNode, path_to_sdoc_file: str):
return StrictDocSemanticError(
title=f"Invalid requirement type: {node.requirement_type}.",
title=f"Invalid node type: {node.node_type}.",
hint=None,
example=None,
line=node.ng_line_start,
Expand All @@ -54,14 +54,12 @@ def unregistered_field(
document_grammar: DocumentGrammar,
path_to_sdoc_file: str,
):
grammar_dump = document_grammar.dump_fields(
requirement.requirement_type
)
grammar_dump = document_grammar.dump_fields(requirement.node_type)
return StrictDocSemanticError(
title=f"Invalid requirement field: {field_name}",
hint=(
f"Compare with the document grammar: [{grammar_dump}] "
f"for type: {requirement.requirement_type}."
f"for type: {requirement.node_type}."
),
example=None,
line=requirement.ng_line_start,
Expand All @@ -76,7 +74,7 @@ def missing_required_field(
document_grammar: DocumentGrammar,
path_to_sdoc_file: str,
):
grammar_fields = document_grammar.dump_fields(node.requirement_type)
grammar_fields = document_grammar.dump_fields(node.node_type)
return StrictDocSemanticError(
title=(
f"Requirement is missing a field that is required by "
Expand All @@ -99,7 +97,7 @@ def unexpected_field_outside_grammar(
document_grammar: DocumentGrammar,
path_to_sdoc_file: str,
):
grammar_fields = document_grammar.dump_fields(node.requirement_type)
grammar_fields = document_grammar.dump_fields(node.node_type)
return StrictDocSemanticError(
title=(
f"Unexpected field outside grammar: "
Expand All @@ -126,13 +124,13 @@ def wrong_field_order(
problematic_field, SDocNodeField
), f"{problematic_field}"
requirement_dump = node.dump_fields_as_parsed()
grammar_dump = document_grammar.dump_fields(node.requirement_type)
grammar_dump = document_grammar.dump_fields(node.node_type)
return StrictDocSemanticError(
title=f"Wrong field order for requirement: [{requirement_dump}].",
hint=(
f"Problematic field: {problematic_field.field_name}. "
f"Compare with the document grammar: [{grammar_dump}] "
f"for type: {node.requirement_type}."
f"for type: {node.node_type}."
),
example=None,
line=node.ng_line_start,
Expand All @@ -156,9 +154,9 @@ def invalid_choice_field(
f"Problematic field: {requirement_field.field_name}. "
f"Compare with the document grammar: "
f"["
f"{document_grammar.dump_fields(node.requirement_type)}"
f"{document_grammar.dump_fields(node.node_type)}"
f"] "
f"for type: {node.requirement_type}."
f"for type: {node.node_type}."
),
example=None,
line=node.ng_line_start,
Expand All @@ -182,9 +180,9 @@ def invalid_multiple_choice_field(
f"Problematic field: {requirement_field.field_name}. "
f"Compare with the document grammar: "
f"["
f"{document_grammar.dump_fields(node.requirement_type)}"
f"{document_grammar.dump_fields(node.node_type)}"
f"] "
f"for type: {node.requirement_type}."
f"for type: {node.node_type}."
),
example=None,
line=node.ng_line_start,
Expand Down
4 changes: 2 additions & 2 deletions strictdoc/backend/sdoc/grammar/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
;
SDocNode[noskipws]:
'[' !'SECTION' !SDocCompositeNodeTagName requirement_type = RequirementType ']' '\n'
'[' !'SECTION' !SDocCompositeNodeTagName node_type = RequirementType ']' '\n'
fields *= SDocNodeField
(
'RELATIONS:' '\n'
Expand Down Expand Up @@ -210,7 +210,7 @@
;
SDocCompositeNode[noskipws]:
'[COMPOSITE_' requirement_type = RequirementType ']' '\n'
'[COMPOSITE_' node_type = RequirementType ']' '\n'
fields *= SDocNodeField
(
Expand Down
2 changes: 1 addition & 1 deletion strictdoc/backend/sdoc/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def has_any_requirements(self) -> bool:
return True
continue
if section_or_requirement.is_requirement:
if section_or_requirement.requirement_type == "TEXT":
if section_or_requirement.node_type == "TEXT":
continue
return True
assert section_or_requirement.is_section, section_or_requirement
Expand Down
4 changes: 2 additions & 2 deletions strictdoc/backend/sdoc/models/document_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ def get_element_by_mid(self, element_mid: str):
f"Could not find a grammar element with MID: {element_mid}"
)

def dump_fields(self, requirement_type) -> str:
def dump_fields(self, node_type) -> str:
return ", ".join(
list(
map(
lambda g: g.title,
self.elements_by_type[requirement_type].fields,
self.elements_by_type[node_type].fields,
)
)
)
Expand Down
6 changes: 3 additions & 3 deletions strictdoc/backend/sdoc/models/document_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def __init__(
self.hidden_tags: Optional[List[ViewElementHiddenTag]] = hidden_tags
self.name: Optional[str] = name

def includes_field(self, requirement_type: str, field_name: str) -> bool:
def includes_field(self, node_type: str, field_name: str) -> bool:
for tag_ in self.tags:
if tag_.object_type == requirement_type:
if tag_.object_type == node_type:
field_: ViewElementField
for field_ in tag_.visible_fields:
if field_.name == field_name:
Expand All @@ -43,7 +43,7 @@ class DefaultViewElement(ViewElement):
works fine for now.
"""

def includes_field(self, requirement_type: str, field_name: str) -> bool: # noqa: ARG002
def includes_field(self, node_type: str, field_name: str) -> bool: # noqa: ARG002
return True


Expand Down
32 changes: 15 additions & 17 deletions strictdoc/backend/sdoc/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ class SDocNode(SDocObject):
def __init__(
self,
parent: Union[SDocDocument, SDocSection, "SDocCompositeNode"],
requirement_type: str,
node_type: str,
fields: List[SDocNodeField],
relations: List[Reference],
requirements: Optional[List["SDocNode"]] = None,
basic_free_text: bool = False,
) -> None:
assert parent
assert isinstance(requirement_type, str)
assert isinstance(node_type, str)
assert isinstance(relations, list), relations

self.parent: Union[SDocDocument, SDocSection, SDocCompositeNode] = (
parent
)

self.requirement_type: str = requirement_type
self.node_type: str = node_type

ordered_fields_lookup: OrderedDict[str, List[SDocNodeField]] = (
OrderedDict()
Expand Down Expand Up @@ -178,17 +178,17 @@ def get_type_string() -> str:
return "requirement"

def get_node_type_string(self) -> Optional[str]:
return self.requirement_type
return self.node_type

def get_node_type_string_lower(self) -> Optional[str]:
return self.requirement_type.lower()
return self.node_type.lower()

def get_display_title(self) -> str:
if self.reserved_title is not None:
return self.reserved_title
if self.reserved_uid is not None:
return self.reserved_uid
return f"{self.requirement_type} with no title/UID"
return f"{self.node_type} with no title/UID"

@property
def is_root_included_document(self) -> bool:
Expand Down Expand Up @@ -233,14 +233,14 @@ def reserved_title(self) -> Optional[str]:

def has_reserved_statement(self) -> bool:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
return element.content_field[0] in self.ordered_fields_lookup

@property
def reserved_statement(self) -> Optional[str]:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
return self._get_cached_field(
element.content_field[0], singleline_only=False
Expand All @@ -259,7 +259,7 @@ def is_requirement(self) -> bool:
return True

def is_text_node(self) -> bool:
return self.requirement_type == "TEXT"
return self.node_type == "TEXT"

# FIXME: Remove @property.
@property
Expand Down Expand Up @@ -335,13 +335,13 @@ def get_requirement_style_mode(self) -> str:

def get_content_field_name(self) -> str:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
return element.content_field[0]

def get_content_field(self) -> SDocNodeField:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
return self.ordered_fields_lookup[element.content_field[0]][0]

Expand Down Expand Up @@ -456,7 +456,7 @@ def enumerate_meta_fields(
self, skip_single_lines: bool = False, skip_multi_lines: bool = False
) -> Generator[Tuple[str, SDocNodeField], None, None]:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
grammar_field_titles = list(map(lambda f: f.title, element.fields))
statement_field_index: int = element.content_field[1]
Expand Down Expand Up @@ -489,14 +489,14 @@ def get_meta_field_value_by_title(self, field_title: str) -> Optional[str]:

def get_field_human_title(self, field_name: str) -> str:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
field_human_title = element.fields_map[field_name]
return field_human_title.get_field_human_name()

def get_field_human_title_for_statement(self) -> str:
element: GrammarElement = self.document.grammar.elements_by_type[
self.requirement_type
self.node_type
]
field_human_title = element.fields_map[element.content_field[0]]
return field_human_title.get_field_human_name()
Expand Down Expand Up @@ -567,9 +567,7 @@ def set_field_value(
assert grammar_or_none is not None
grammar: DocumentGrammar = grammar_or_none

element: GrammarElement = grammar.elements_by_type[
self.requirement_type
]
element: GrammarElement = grammar.elements_by_type[self.node_type]
grammar_field_titles = list(map(lambda f: f.title, element.fields))
field_index = grammar_field_titles.index(field_name)

Expand Down
6 changes: 3 additions & 3 deletions strictdoc/backend/sdoc/models/object_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_document(title: Optional[str]):
@staticmethod
def create_requirement(
parent,
requirement_type: Optional[str] = "REQUIREMENT",
node_type: Optional[str] = "REQUIREMENT",
uid: Optional[str] = None,
level: Optional[str] = None,
title: Optional[str] = None,
Expand Down Expand Up @@ -127,7 +127,7 @@ def create_requirement(
)
requirement = SDocNode(
parent=parent,
requirement_type=requirement_type,
node_type=node_type,
fields=fields,
relations=[],
)
Expand Down Expand Up @@ -175,7 +175,7 @@ def create_requirement_from_dict(requirement_dict, parent, level):

requirement = SDocObjectFactory.create_requirement(
parent=parent,
requirement_type="REQUIREMENT",
node_type="REQUIREMENT",
uid=uid,
level=None,
title=title,
Expand Down
2 changes: 1 addition & 1 deletion strictdoc/backend/sdoc/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def rewrite_free_text_to_text_node_if_needed(
]
text_node = SDocNode(
parent=parent_node,
requirement_type="TEXT",
node_type="TEXT",
fields=fields,
relations=[],
requirements=None,
Expand Down
11 changes: 4 additions & 7 deletions strictdoc/backend/sdoc/validations/sdoc_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,13 @@ def validate_node(
path_to_sdoc_file: str,
auto_uid_mode: bool = True,
):
if (
requirement.requirement_type
not in document_grammar.registered_elements
):
raise StrictDocSemanticError.unknown_requirement_type(
if requirement.node_type not in document_grammar.registered_elements:
raise StrictDocSemanticError.unknown_node_type(
requirement, path_to_sdoc_file
)

grammar_element = document_grammar.elements_by_type[
requirement.requirement_type
requirement.node_type
]
registered_fields: Set[str] = set(grammar_element.get_field_titles())

Expand All @@ -181,7 +178,7 @@ def validate_node(
)

grammar_element: GrammarElement = document_grammar.elements_by_type[
requirement.requirement_type
requirement.node_type
]
grammar_fields_iterator: Iterator[GrammarElementField] = iter(
grammar_element.fields
Expand Down
Loading

0 comments on commit 631f9b6

Please sign in to comment.