Skip to content

Commit

Permalink
Changed from using req_prefix to req_regex
Browse files Browse the repository at this point in the history
  • Loading branch information
hpenne committed Aug 12, 2024
1 parent 308fce0 commit 9f0ef4a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 27 deletions.
9 changes: 5 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Tags: V.1;
Child: SW-RMS-REQ-011
----

NOTE: The attribute "req-prefix" (see below) must be set to "UR-RMS-" so that AsciiReqs knows that the term is a requirement and not something else.
NOTE: The attribute "req-regex" (see below) must be set to "UR-RMS-\d+" so that AsciiReqs knows that the term is a requirement and not something else.

This renders as:

Expand Down Expand Up @@ -179,12 +179,13 @@ Asciireqs will parse these documents as well, as child documents in the specific

==== Other document attributes

You also need to define an attribute to tell AsciiReqs the prefix string for your requirement IDs.
The following defines "SW-REQ-" as the prefix for the current document's requirements (meaning that all requirement IDs are "SW-REQ-" followed by one or more digits):
You also need to define an attribute to tell AsciiReqs the pattern for your requirement IDs.
This uses normal "regex" syntax.
The following defines "SW-REQ-" as the prefix for the current document's requirements (all requirement IDs are "SW-REQ-" followed by one or more digits):

[source, asciidoc]
----
:req-prefix: SW-REQ-
:req_regex: SW-REQ-\d+
----

This attribute is necessary in order to identify the requirement IDs and insert cross-links when post-processing the documents.
Expand Down
14 changes: 7 additions & 7 deletions asciireqs/docparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ def get_source_block(lines: Iterable[Tuple[int, str]]) -> Tuple[List[str], int]:

def validate_requirement(req: Requirement, doc: ReqDocument, line_no: int) -> bool:
"""Takes a Requirement and verifies that it contains the required attributes"""
if not doc.req_prefix:
print("Error: Document has no req-prefix attribute")
if not doc.req_regex:
print("Error: Document has no req_regex attribute")
return False
if ID not in req:
print(f"Error: Missing ID attribute on line {line_no}")
return False
if TEXT not in req:
print(f"Error: Missing Text attribute on line {line_no}")
return False
if not req[ID].startswith(doc.req_prefix):
if not re.match(f"{doc.req_regex}", req[ID]):
print(f"Error: Wrong ID format on line {line_no}")
return False
return True
Expand All @@ -91,7 +91,7 @@ def req_from_yaml_lines(
return []

reqs = []
if next(iter(attributes.keys())).startswith(doc.req_prefix):
if re.match(f"{doc.req_regex}", next(iter(attributes.keys()))):
# This is dict of requirements:
for req_id, attrs in attributes.items():
req = {name: str(value).strip(" \n") for name, value in attrs.items()}
Expand Down Expand Up @@ -183,7 +183,7 @@ def req_from_term(
:param doc: The document that is being parsed
:return: The requirement (or None if not found)
"""
match = re.fullmatch(rf"({doc.req_prefix}\d+)::", first_line.strip())
match = re.fullmatch(f"({doc.req_regex})::", first_line.strip())
if match:
req = {ID: match.group(1), LINE_NO: str(line_no)}
try:
Expand Down Expand Up @@ -242,9 +242,9 @@ def parse_doc(lines: Iterable[Tuple[int, str]]) -> ReqDocument:
doc.child_doc_files = [
file_name.strip() for file_name in attribute_value.split(",")
]
attribute_value = get_attribute(text, "req-prefix")
attribute_value = get_attribute(text, "req_regex")
if attribute_value:
doc.req_prefix = attribute_value
doc.req_regex = attribute_value
return doc


Expand Down
4 changes: 2 additions & 2 deletions asciireqs/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def line_numbers_for_requirements(requirements: Requirements) -> Dict[int, str]:

def insert_requirement_links(line: str, doc: ReqDocument) -> str:
"""Takes a line of AsciiDoc text and adds cross-links to requirement IDs"""
if doc.req_prefix:
line = re.sub(rf"({doc.req_prefix}\d+)", f"xref:{doc.name}#\\1[\\1]", line)
if doc.req_regex:
line = re.sub(f"({doc.req_regex})", f"xref:{doc.name}#\\1[\\1]", line)
for child_doc in doc.child_docs:
line = insert_requirement_links(line, child_doc)
return line
Expand Down
4 changes: 2 additions & 2 deletions asciireqs/reqdocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class ReqDocument:
reqs: Requirements
child_doc_files: List[str]
child_docs: List[ReqDocument]
req_prefix: str
req_regex: str

def __init__(self) -> None:
self.name = ""
self.attribute_names: List[str] = []
self.reqs: Requirements = {}
self.child_doc_files: List[str] = []
self.child_docs: List[ReqDocument] = []
self.req_prefix: str = ""
self.req_regex: str = ""

def _add_keys(self, keys: List[str]) -> None:
"""Takes a list of requirement attribute names, and adds new ones to 'attribute_names'"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
setuptools~=65.5.1
setuptools~=72.1.0
PyYAML~=5.4.1
types-PyYAML~=6.0.5
openpyxl
Expand Down
2 changes: 1 addition & 1 deletion testdata/req-tool-sw-reqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:sectnums:
:sectnumlevels: 3
:disclosure: Internal
:req-prefix: SW-RMS-REQ-
:req_regex: SW-RMS-REQ-\d

This is a page after the cover, where approval and history will go.

Expand Down
2 changes: 1 addition & 1 deletion testdata/req-tool-user-reqs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
:sectnumlevels: 3
:disclosure: Internal
:req-children: req-tool-sw-reqs.adoc
:req-prefix: UR-RMS-REQ-
:req_regex: UR-RMS-REQ-\d+

This is a page after the cover, where approval and history will go.

Expand Down
8 changes: 4 additions & 4 deletions tests/test_docparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def empty() -> Tuple[str, int]:

def doc_with_req_prefix() -> ReqDocument:
doc = ReqDocument()
doc.req_prefix = "SR-"
doc.req_regex = r"SR-\d+"
return doc


def test_validate_requirement() -> None:
doc = ReqDocument()
doc.req_prefix = "SR-"
doc.req_regex = r"SR-\d+"
assert validate_requirement({ID: "SR-1", TEXT: "Some text"}, doc, 2)


Expand All @@ -35,7 +35,7 @@ def test_validate_requirement_no_req_prefix_attribute() -> None:

def test_validate_requirement_no_id_field() -> None:
doc = ReqDocument()
doc.req_prefix = "SR-"
doc.req_regex = r"SR-\d+"
assert not validate_requirement({TEXT: "Some text"}, doc, 2)


Expand All @@ -47,7 +47,7 @@ def test_validate_requirement_wrong_id_prefix() -> None:

def test_validate_requirement_no_text_field() -> None:
doc = ReqDocument()
doc.req_prefix = "SR-"
doc.req_prefix = r"SR-\d+"
assert not validate_requirement({ID: "SR-1"}, doc, 2)


Expand Down
10 changes: 5 additions & 5 deletions tests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def doc1_reqs() -> Requirements:

def docs_with_req_prefix() -> ReqDocument:
doc = ReqDocument()
doc.req_prefix = "UR-REQ-"
doc.req_regex = r"UR-REQ-\d+"
doc.name = "ur-reqs.adoc"
child_doc1 = ReqDocument()
child_doc1.req_prefix = "SW-REQ-"
child_doc1.req_regex = r"SW-REQ-\d+"
child_doc1.name = "sw-reqs.adoc"
doc.add_child_doc(child_doc1)
child_doc2 = ReqDocument()
child_doc2.req_prefix = "HW-REQ-"
child_doc2.req_regex = r"HW-REQ-\d+"
child_doc2.name = "hw-reqs.adoc"
doc.add_child_doc(child_doc2)
return doc
Expand Down Expand Up @@ -186,7 +186,7 @@ def test_missing_link_from_parent_two_of_two_downlinks_ok() -> None:

def test_requirement_as_term() -> None:
ur = ReqDocument()
ur.req_prefix = "UR-"
ur.req_regex = r"UR-\d+"
ur.name = "ur.adoc"
ur.reqs["UR-1"] = {
ID: "UR-1",
Expand All @@ -212,7 +212,7 @@ def test_requirement_as_term() -> None:

def test_requirement_as_term_with_title() -> None:
ur = ReqDocument()
ur.req_prefix = "UR-"
ur.req_regex = r"UR-\d+"
ur.name = "ur.adoc"
ur.reqs["UR-1"] = {
ID: "UR-1",
Expand Down

0 comments on commit 9f0ef4a

Please sign in to comment.