Skip to content

Commit

Permalink
example: basic Excel-to-ReqIF conversion example
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislaw committed Jun 16, 2024
1 parent 87cbf05 commit 3c38819
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 9 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies = [
"lxml >= 4.6.2",
"jinja2 >= 2.11.2",
"xmlschema >= 2.4.0",
"openpyxl",
]

[project.optional-dependencies]
Expand Down
39 changes: 39 additions & 0 deletions reqif/helpers/reqif_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import time
from datetime import datetime, timedelta, timezone


def create_reqif_datetime_now() -> str:
"""
FIXME: Maybe there is an easier way of create this.
"""

# Get the local time and UTC offset
local_time = time.localtime()
utc_offset = timedelta(
seconds=-time.timezone if local_time.tm_isdst == 0 else -time.altzone
)

# Create a timezone object
local_timezone = timezone(utc_offset)

# Get the current time in the local timezone
now = datetime.now(local_timezone)

# Format the datetime in the ReqIF format
formatted_time = reqif_datetime_format(now)

return formatted_time


def reqif_datetime_format(datetime_obj: datetime) -> str:
"""
Formats a date object to this format:
2024-06-16T22:39:18.543+02:00
FIXME: Maybe there is an easier way of create this.
"""

formatted_time = datetime_obj.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
formatted_time = (
formatted_time[:23] + formatted_time[-5:-2] + ":" + formatted_time[-2:]
)
return formatted_time
21 changes: 19 additions & 2 deletions reqif/models/reqif_namespace_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@


@auto_described
class ReqIFNamespaceInfo: # pylint: disable=too-many-instance-attributes
def __init__( # pylint: disable=too-many-arguments
class ReqIFNamespaceInfo:
REQIF_XSD = "http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"

def __init__(
self,
original_reqif_tag_dump: Optional[str],
doctype_is_present: bool,
Expand Down Expand Up @@ -46,3 +48,18 @@ def empty(
schema_location=None,
language=None,
)

@staticmethod
def create_default():
return ReqIFNamespaceInfo(
original_reqif_tag_dump=None,
doctype_is_present=True,
encoding="UTF-8",
namespace=ReqIFNamespaceInfo.REQIF_XSD,
configuration=None,
namespace_id=None,
namespace_xhtml="http://www.w3.org/1999/xhtml",
schema_namespace=None,
schema_location=None,
language=None,
)
4 changes: 2 additions & 2 deletions reqif/models/reqif_reqif_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class EmptyTag:


@auto_described
class ReqIFReqIFHeader: # pylint: disable=too-many-instance-attributes
def __init__( # pylint: disable=too-many-arguments
class ReqIFReqIFHeader:
def __init__(
self,
identifier: Optional[str] = None,
comment: Optional[str] = None,
Expand Down
2 changes: 1 addition & 1 deletion reqif/parsers/attribute_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def unparse_attribute_values(
output += " <VALUES>\n"
for attribute in attribute_values:
if attribute.attribute_type == SpecObjectAttributeType.STRING:
assert isinstance(attribute.value, str)
assert isinstance(attribute.value, str), attribute.value
output += ATTRIBUTE_STRING_TEMPLATE.format(
definition_ref=attribute.definition_ref,
value=html.escape(attribute.value),
Expand Down
4 changes: 2 additions & 2 deletions reqif/reqif_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


@auto_described
class ReqIFBundle: # pylint: disable=too-many-instance-attributes
class ReqIFBundle:
@staticmethod
def create_empty(
namespace: Optional[str],
Expand All @@ -44,7 +44,7 @@ def __init__(
tool_extensions_tag_exists: bool,
lookup: ReqIFObjectLookup,
exceptions: List[ReqIFSchemaError],
): # pylint: disable=too-many-arguments
):
self.namespace_info: ReqIFNamespaceInfo = namespace_info
self.req_if_header: Optional[ReqIFReqIFHeader] = req_if_header
self.core_content: Optional[ReqIFCoreContent] = core_content
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/examples/01_create_reqif_objects/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ExampleIdentifiers:


string_data_type = ReqIFDataTypeDefinitionString(
identifier=(ExampleIdentifiers.STRING_DATATYPE_ID),
identifier=ExampleIdentifiers.STRING_DATATYPE_ID,
last_change=date_now,
long_name="String type",
max_length="50",
Expand All @@ -51,7 +51,7 @@ class ExampleIdentifiers:
requirement_text_attribute = SpecAttributeDefinition(
attribute_type=SpecObjectAttributeType.STRING,
identifier=ExampleIdentifiers.SPEC_ATTRIBUTE_ID,
datatype_definition=(ExampleIdentifiers.STRING_DATATYPE_ID),
datatype_definition=ExampleIdentifiers.STRING_DATATYPE_ID,
last_change=date_now,
long_name="Requirement text",
)
Expand Down
Binary file not shown.
Loading

0 comments on commit 3c38819

Please sign in to comment.