6
6
@File : mermaid.py
7
7
"""
8
8
import asyncio
9
- import subprocess
9
+ import os
10
10
from pathlib import Path
11
11
12
12
from metagpt .config import CONFIG
13
13
from metagpt .const import PROJECT_ROOT
14
14
from metagpt .logs import logger
15
15
from metagpt .utils .common import check_cmd_exists
16
- import os
17
- import sys
16
+
18
17
19
18
async def mermaid_to_file (mermaid_code , output_file_without_suffix , width = 2048 , height = 2048 ) -> int :
20
19
"""suffix: png/svg/pdf
@@ -28,41 +27,39 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048,
28
27
# Write the Mermaid code to a temporary file
29
28
dir_name = os .path .dirname (output_file_without_suffix )
30
29
if dir_name and not os .path .exists (dir_name ):
31
- os .makedirs (dir_name )
30
+ os .makedirs (dir_name )
32
31
tmp = Path (f"{ output_file_without_suffix } .mmd" )
33
32
tmp .write_text (mermaid_code , encoding = "utf-8" )
34
-
33
+
35
34
engine = CONFIG .mermaid_engine .lower ()
36
35
if engine == "nodejs" :
37
- if check_cmd_exists (" mmdc" ) != 0 :
36
+ if check_cmd_exists (CONFIG . mmdc ) != 0 :
38
37
logger .warning ("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc" )
39
38
return - 1
40
-
39
+
41
40
for suffix in ["pdf" , "svg" , "png" ]:
42
41
output_file = f"{ output_file_without_suffix } .{ suffix } "
43
42
# Call the `mmdc` command to convert the Mermaid code to a PNG
44
43
logger .info (f"Generating { output_file } .." )
45
44
46
45
if CONFIG .puppeteer_config :
47
- commands = [
48
- CONFIG .mmdc ,
49
- "-p" ,
50
- CONFIG .puppeteer_config ,
51
- "-i" ,
52
- str (tmp ),
53
- "-o" ,
54
- output_file ,
55
- "-w" ,
56
- str (width ),
57
- "-H" ,
58
- str (height ),
59
- ]
46
+ commands = [
47
+ CONFIG .mmdc ,
48
+ "-p" ,
49
+ CONFIG .puppeteer_config ,
50
+ "-i" ,
51
+ str (tmp ),
52
+ "-o" ,
53
+ output_file ,
54
+ "-w" ,
55
+ str (width ),
56
+ "-H" ,
57
+ str (height ),
58
+ ]
60
59
else :
61
- commands = [CONFIG .mmdc , "-i" , str (tmp ), "-o" , output_file , "-w" , str (width ), "-H" , str (height )]
62
- process = await asyncio .create_subprocess_exec (
63
- * commands ,
64
- stdout = asyncio .subprocess .PIPE ,
65
- stderr = asyncio .subprocess .PIPE
60
+ commands = [CONFIG .mmdc , "-i" , str (tmp ), "-o" , output_file , "-w" , str (width ), "-H" , str (height )]
61
+ process = await asyncio .create_subprocess_shell (
62
+ " " .join (commands ), stdout = asyncio .subprocess .PIPE , stderr = asyncio .subprocess .PIPE
66
63
)
67
64
68
65
stdout , stderr = await process .communicate ()
@@ -71,15 +68,17 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048,
71
68
if stderr :
72
69
logger .error (stderr .decode ())
73
70
else :
74
-
75
- if engine == 'playwright' :
71
+ if engine == "playwright" :
76
72
from metagpt .utils .mmdc_playwright import mermaid_to_file
73
+
77
74
return await mermaid_to_file (mermaid_code , output_file_without_suffix , width , height )
78
- elif engine == ' pyppeteer' :
75
+ elif engine == " pyppeteer" :
79
76
from metagpt .utils .mmdc_pyppeteer import mermaid_to_file
77
+
80
78
return await mermaid_to_file (mermaid_code , output_file_without_suffix , width , height )
81
- elif engine == ' ink' :
79
+ elif engine == " ink" :
82
80
from metagpt .utils .mmdc_ink import mermaid_to_file
81
+
83
82
return await mermaid_to_file (mermaid_code , output_file_without_suffix )
84
83
else :
85
84
logger .warning (f"Unsupported mermaid engine: { engine } " )
@@ -137,9 +136,8 @@ class KnowledgeBase {
137
136
SE-->>M: return summary"""
138
137
139
138
140
-
141
139
if __name__ == "__main__" :
142
140
loop = asyncio .new_event_loop ()
143
- result = loop .run_until_complete (mermaid_to_file (MMC1 , PROJECT_ROOT / f"{ CONFIG .mermaid_engine } /1" ))
144
- result = loop .run_until_complete (mermaid_to_file (MMC2 , PROJECT_ROOT / f"{ CONFIG .mermaid_engine } /1" ))
141
+ result = loop .run_until_complete (mermaid_to_file (MMC1 , PROJECT_ROOT / f"{ CONFIG .mermaid_engine } /1" ))
142
+ result = loop .run_until_complete (mermaid_to_file (MMC2 , PROJECT_ROOT / f"{ CONFIG .mermaid_engine } /1" ))
145
143
loop .close ()
0 commit comments