Skip to content

Commit

Permalink
feat: reading mermaid uml from external file
Browse files Browse the repository at this point in the history
  • Loading branch information
tsypuk committed Sep 26, 2023
1 parent db6ab6f commit 8379611
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
53 changes: 53 additions & 0 deletions multicloud_diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,56 @@ def export_to_file(self, file_path):
# Write the prettified XML to a file
with open(file_path, 'w', encoding="utf-8") as file:
file.write(resulting_xml)

def read_uml_from_file(self, file_name):
with open(file_name, 'r') as file:
sequence_diagram = file.read()

actors = self.extract_actors(sequence_diagram)

print("Actors:")
for actor in actors:
print(actor)

self.extract_message(sequence_diagram, actors)

def extract_actors(self, sequence_diagram):
actors = []
lines = sequence_diagram.split('\n')
for line in lines:
strip = line.strip()
if (strip.startswith('participant')) | (strip.startswith('actor')):
actor = line.strip().split()[1]
actors.append(actor)
return actors

def extract_message(self, sequence_diagram, actors):
lines = sequence_diagram.split('\n')
for line in lines:
strip = line.strip()
if any(strip.startswith(actor) for actor in actors):
data = self.extract_info(line)
print(data)

# [Actor][Arrow][Actor]:Message text

# Type Description
# -> Solid line without arrow
# --> Dotted line without arrow
# ->> Solid line with arrowhead
# -->> Dotted line with arrowhead
# -x Solid line with a cross at the end
# --x Dotted line with a cross at the end.
# -) Solid line with an open arrow at the end (async)
# --) Dotted line with a open arrow at the end (async)
def extract_info(self, input_string):
pattern = r'(.*?)(?:-->|->>)(.*?):(.*)'
match = re.match(pattern, input_string)

if match:
actor1 = match.group(1).strip()
actor2 = match.group(2).strip()
message = match.group(3).lstrip()
return actor1, actor2, message
else:
return None
21 changes: 21 additions & 0 deletions samples/samples/aws_mermaid_uml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from multicloud_diagrams import MultiCloudDiagrams
from samples.samples.aws_service_end_2_end import prepare_end2end


def main():
mcd = MultiCloudDiagrams()

prefix = 'prod'
prev_file = f'../output/output.{prefix}.end2end.drawio'
result_file = f'../output/output.{prefix}_uml.end2end.drawio'

mcd.read_coords_from_file(prev_file)
prepare_end2end(mcd)

mcd.read_uml_from_file('uml.mermaid')

mcd.export_to_file(result_file)


if __name__ == "__main__":
main()
15 changes: 15 additions & 0 deletions samples/samples/uml.mermaid
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sequenceDiagram
actor Donald
participant S3Bucket
participant LambdaFunction1
participant LambdaFunction2
participant SNS
participant SQS
participant Storage

Donald->>S3Bucket: Put new file into S3 bucket
S3Bucket->>LambdaFunction1: LifeConfig Rule invokes lambda function
LambdaFunction1->>SNS: Send Event to SNS that file processing is finished
SNS->>SQS: Fanout Event to SQS
SQS->>LambdaFunction2: poll message from SQS and invoke consumer lambda function
LambdaFunction2->>Storage: Update DynamoDB Table. Save image status.

0 comments on commit 8379611

Please sign in to comment.