Skip to content

Commit 273e0ce

Browse files
authored
Merge pull request #2192 from strictdoc-project/stanislaw/code_climate
backend/sdoc: groundwork for new composite node syntax
2 parents d284ec0 + 8f3b7df commit 273e0ce

File tree

5 files changed

+88
-21
lines changed

5 files changed

+88
-21
lines changed

strictdoc/backend/sdoc/grammar/grammar.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
;
152152
153153
SectionOrRequirement[noskipws]:
154-
'\n' (SDocSection | SDocNode | SDocCompositeNode | DocumentFromFile)
154+
'\n' (SDocSection | SDocCompositeNodeNew | SDocNode | SDocCompositeNode | DocumentFromFile)
155155
;
156156
157157
DocumentFromFile[noskipws]:
@@ -176,6 +176,21 @@
176176
)?
177177
;
178178
179+
SDocCompositeNodeNew[noskipws]:
180+
'[[' node_type = RequirementType ']]' '\n'
181+
182+
fields *= SDocNodeField
183+
(
184+
'RELATIONS:' '\n'
185+
(relations += Reference)
186+
)?
187+
188+
requirements *= SpaceThenRequirement
189+
190+
'\n'
191+
'[[/' node_type_close = RequirementType ']]' '\n'
192+
;
193+
179194
SDocCompositeNodeTagName[noskipws]:
180195
'COMPOSITE_'
181196
;

strictdoc/backend/sdoc/models/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from strictdoc.backend.sdoc.models.inline_link import InlineLink
1414
from strictdoc.backend.sdoc.models.node import (
1515
SDocCompositeNode,
16+
SDocCompositeNodeNew,
1617
SDocNode,
1718
SDocNodeField,
1819
)
@@ -43,6 +44,7 @@
4344
SDocNode,
4445
SDocNodeField,
4546
SDocCompositeNode,
47+
SDocCompositeNodeNew,
4648
Reference,
4749
ParentReqReference,
4850
ChildReqReference,

strictdoc/backend/sdoc/models/node.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def __init__(
106106
node_type: str,
107107
fields: List[SDocNodeField],
108108
relations: List[Reference],
109+
is_composite: bool = False,
109110
requirements: Optional[List["SDocNode"]] = None,
111+
node_type_close: Optional[str] = None,
110112
) -> None:
111113
assert parent
112114
assert isinstance(node_type, str)
@@ -117,6 +119,12 @@ def __init__(
117119
] = parent
118120

119121
self.node_type: str = node_type
122+
# FIXME: MERGE NODES
123+
if node_type_close is not None and len(node_type_close) > 0:
124+
assert node_type == node_type_close
125+
assert is_composite
126+
127+
self.is_composite: bool = is_composite
120128

121129
ordered_fields_lookup: OrderedDict[str, List[SDocNodeField]] = (
122130
OrderedDict()
@@ -174,7 +182,9 @@ def __init__(
174182
self.custom_level = level
175183

176184
# This is always true, unless the node is filtered out with --filter-requirements.
177-
self.ng_whitelisted = True
185+
self.ng_whitelisted: bool = True
186+
187+
self.ng_has_requirements: bool = False
178188

179189
@staticmethod
180190
def get_type_string() -> str:
@@ -646,9 +656,6 @@ def __init__(
646656
**fields: Any,
647657
) -> None:
648658
super().__init__(parent, **fields)
649-
self.ng_document_reference: Optional[DocumentReference] = None
650-
self.ng_including_document_reference: Optional[DocumentReference] = None
651-
self.ng_has_requirements = False
652659

653660
@property
654661
def is_composite_requirement(self) -> bool:
@@ -661,9 +668,12 @@ def document(self) -> SDocDocumentIF:
661668
assert document is not None
662669
return document
663670

664-
def document_is_included(self) -> bool:
665-
assert self.ng_including_document_reference is not None
666-
return self.ng_including_document_reference.get_document() is not None
667671

668-
def get_requirement_prefix(self) -> str:
669-
return self.parent.get_requirement_prefix()
672+
@auto_described
673+
class SDocCompositeNodeNew(SDocNode):
674+
def __init__(
675+
self,
676+
parent: Union[SDocDocumentIF, SDocSectionIF, SDocCompositeNodeIF],
677+
**fields: Any,
678+
) -> None:
679+
super().__init__(parent, **fields, is_composite=True)

strictdoc/backend/sdoc/writer.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
from strictdoc.backend.sdoc.models.node import (
1515
SDocCompositeNode,
16+
SDocCompositeNodeNew,
1617
SDocNode,
1718
)
1819
from strictdoc.backend.sdoc.models.reference import (
@@ -37,10 +38,10 @@
3738

3839

3940
class SDWriter:
40-
def __init__(self, project_config: ProjectConfig):
41+
def __init__(self, project_config: ProjectConfig) -> None:
4142
self.project_config: ProjectConfig = project_config
4243

43-
def write_to_file(self, document: SDocDocument):
44+
def write_to_file(self, document: SDocDocument) -> None:
4445
document_content, fragments_dict = self.write_with_fragments(document)
4546

4647
document_meta: DocumentMeta = assert_cast(document.meta, DocumentMeta)
@@ -62,7 +63,7 @@ def write_to_file(self, document: SDocDocument):
6263
with open(path_to_output_fragment, "w", encoding="utf8") as file_:
6364
file_.write(fragment_content_)
6465

65-
def write(self, document: SDocDocument):
66+
def write(self, document: SDocDocument) -> str:
6667
document_output, _ = self.write_with_fragments(document)
6768
return document_output
6869

@@ -281,7 +282,11 @@ def _print_node(
281282
elif isinstance(root_node, SDocNode):
282283
output = ""
283284

284-
if isinstance(root_node, SDocCompositeNode):
285+
if isinstance(root_node, SDocCompositeNodeNew):
286+
output += "[["
287+
output += root_node.node_type
288+
output += "]]\n"
289+
elif isinstance(root_node, SDocCompositeNode):
285290
output += "[COMPOSITE_"
286291
output += root_node.node_type
287292
output += "]\n"
@@ -295,24 +300,31 @@ def _print_node(
295300
)
296301
output += "\n"
297302

298-
if isinstance(root_node, SDocCompositeNode):
303+
if isinstance(root_node, (SDocCompositeNode, SDocCompositeNodeNew)):
299304
for node_ in root_node.requirements:
300305
if not node_.ng_whitelisted:
301306
continue
302307
output += self._print_node(
303308
node_, document, document_iterator=document_iterator
304309
)
305310

306-
output += "[/COMPOSITE_"
307-
output += root_node.node_type
308-
output += "]\n"
309-
output += "\n"
311+
if isinstance(root_node, SDocCompositeNodeNew):
312+
output += "[[/"
313+
output += root_node.node_type
314+
output += "]]\n"
315+
output += "\n"
316+
else:
317+
output += "[/COMPOSITE_"
318+
output += root_node.node_type
319+
output += "]\n"
320+
output += "\n"
310321

311322
return output
312323

313-
raise AssertionError("Must not reach here")
324+
raise AssertionError("Must not reach here") # pragma: no cover
314325

315-
def _print_document_from_file(self, document_from_file: DocumentFromFile):
326+
@staticmethod
327+
def _print_document_from_file(document_from_file: DocumentFromFile):
316328
assert isinstance(document_from_file, DocumentFromFile)
317329
output = ""
318330
output += "[DOCUMENT_FROM_FILE]"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from strictdoc.backend.sdoc.models.document import SDocDocument
2+
from strictdoc.backend.sdoc.reader import SDReader
3+
from strictdoc.backend.sdoc.writer import SDWriter
4+
5+
6+
def test_001_minimal_doc(default_project_config):
7+
input_sdoc = """\
8+
[DOCUMENT]
9+
TITLE: Test Doc
10+
11+
[[REQUIREMENT]]
12+
TITLE: FOO
13+
14+
[REQUIREMENT]
15+
TITLE: FOO
16+
17+
[[/REQUIREMENT]]
18+
"""
19+
20+
reader = SDReader()
21+
22+
document = reader.read(input_sdoc)
23+
assert isinstance(document, SDocDocument)
24+
25+
writer = SDWriter(default_project_config)
26+
output = writer.write(document)
27+
28+
assert input_sdoc == output

0 commit comments

Comments
 (0)