瀏覽代碼

Add utility function for generating an empty page

Joeri Exelmans 2 年之前
父節點
當前提交
4b94fb45b1
共有 3 個文件被更改,包括 33 次插入31 次删除
  1. 1 12
      drawio2py/shapelib.py
  2. 25 4
      drawio2py/util.py
  3. 7 15
      test/run_tests.py

+ 1 - 12
drawio2py/shapelib.py

@@ -11,17 +11,6 @@ from drawio2py.abstract_syntax import *
 from drawio2py.parser import Parser
 from drawio2py import util
 
-# Generates (more or less globally unique) Cell IDs in a format similar to how drawio does it:
-class DrawioIDGenerator:
-    def __init__(self):
-        self.next_id = 0
-        self.prefix = secrets.token_urlsafe(20) # a random token of similar complexity to Drawio's cell IDs.
-
-    def gen(self) -> str:
-        id = self.next_id
-        self.next_id += 1
-        return self.prefix + "-" + str(id)
-
 def parse_library(path) -> Dict[str, Cell]:
     # A library is at the highest level an XML tree, with only one node: <mxlibrary>
     tree = ET.parse(path)
@@ -45,7 +34,7 @@ def parse_library(path) -> Dict[str, Cell]:
     return library
 
 class ShapeCloner:
-    def __init__(self, id_gen: DrawioIDGenerator):
+    def __init__(self, id_gen: util.DrawioIDGenerator):
         self.id_gen = id_gen
 
     def clone_cell(self, template: Cell, parent: Cell) -> Cell:

+ 25 - 4
drawio2py/util.py

@@ -1,5 +1,6 @@
 from typing import Optional
-from drawio2py.abstract_syntax import Cell
+import secrets
+from drawio2py.abstract_syntax import Cell, Page
 
 def is_descendant(ancestor: Cell, descendant: Cell) -> bool:
     for child in ancestor.children:
@@ -15,11 +16,22 @@ def find_lca(cell1: Cell, cell2: Cell) -> Optional[Cell]:
         lca = lca.parent
     return None
 
+# Generates (more or less globally unique) Cell IDs in a format similar to how drawio does it:
+class DrawioIDGenerator:
+    def __init__(self):
+        self.next_id = 0
+        self.prefix = secrets.token_urlsafe(20) # a random token of similar complexity to Drawio's cell IDs.
+
+    def gen(self) -> str:
+        id = self.next_id
+        self.next_id += 1
+        return self.prefix + "-" + str(id)
+
 def generate_empty_root_and_layer() -> Cell:
     """
     In typical draw.io fashion, generates two Cells, the root (id="0") and one empty layer (id="1")
     """
-    root = dio_as.Cell(
+    root = Cell(
         id="0",
         value="",
         parent=None,
@@ -28,7 +40,7 @@ def generate_empty_root_and_layer() -> Cell:
         style=None,
         attributes={},
     )
-    layer = dio_as.Cell(
+    layer = Cell(
         id="1",
         value="",
         parent=root,
@@ -38,4 +50,13 @@ def generate_empty_root_and_layer() -> Cell:
         attributes={},
     )
     root.children.append(layer)
-    return root
+    return root
+
+def generate_empty_page(id_gen: DrawioIDGenerator, name: str) -> Page:
+    root = generate_empty_root_and_layer()
+    return Page(
+        id = id_gen.gen(),
+        name = name,
+        attributes = {},
+        root = root,
+    )

+ 7 - 15
test/run_tests.py

@@ -5,7 +5,7 @@ import pprint
 import tempfile
 import unittest
 
-from drawio2py import parser, abstract_syntax, generator, shapelib
+from drawio2py import parser, abstract_syntax, generator, shapelib, util
 
 DATADIR = os.path.join(os.path.dirname(__file__), "data")
 
@@ -51,20 +51,12 @@ class Tests(unittest.TestCase):
     def test_shapelib(self):
         common_lib = parse_shapelib("shapelibs/common.xml")
         pm_lib = parse_shapelib("shapelibs/pm.xml")
-
-        root = abstract_syntax.Cell(
-            id="0",
-            value="",
-            parent=None,
-            children=[],
-            properties={},
-            style=None,
-            attributes={},
-        )
-        cloner = shapelib.ShapeCloner(id_gen=shapelib.DrawioIDGenerator())
-        initial = cloner.clone_vertex(pm_lib["(PM) Initial"], root, 100, 100)
-        final = cloner.clone_vertex(pm_lib["(PM) Final"], root, 300, 300)
-        cloner.clone_edge(common_lib["Control Flow"], root, initial, final)
+        id_gen = util.DrawioIDGenerator()
+        page = util.generate_empty_page(id_gen, "MyFancyPage")
+        cloner = shapelib.ShapeCloner(id_gen)
+        initial = cloner.clone_vertex(pm_lib["(PM) Initial"], page.root, 100, 100)
+        final = cloner.clone_vertex(pm_lib["(PM) Final"], page.root, 300, 300)
+        cloner.clone_edge(common_lib["Control Flow"], page.root, initial, final)
 
     def parse_shapelib1(self):
         parse_shapelib("shapelibs/ftg.xml")