Replace submodule functions with Module objects.
authorJock Tanner <tanner.of.kha@gmail.com>
Thu, 26 Mar 2020 06:50:00 +0000 (06:50 +0000)
committerJock Tanner <tanner.of.kha@gmail.com>
Thu, 26 Mar 2020 07:02:18 +0000 (07:02 +0000)
experiments7/utils.py

index 153923114b9387fe209a7c43edad5b8470812332..82f68797820c54b7de3c3a0745d7be376c6887a2 100644 (file)
@@ -9,10 +9,22 @@ from Hurricane import (
     DbU, DataBase, UpdateSession, Box, Transformation, Instance,  Pad, Pin,
     NetExternalComponents,
 )
-from plugins import RSavePlugin
 
 
-class Module:
+class SessionManager(object):
+    """
+    Context manager for a GO update session. See Hurricane reference manual
+    for an info on Hurricane::UpdateSession class.
+    """
+
+    def __enter__(self):
+        UpdateSession.open()
+
+    def __exit__(self, *args):
+        UpdateSession.close()
+
+
+class Module(object):
 
     _layer_cache = {}
     _instances = None
@@ -74,6 +86,10 @@ class Module:
     def name(self):
         return self.cell.getName()
 
+    @name.setter
+    def name(self, name):
+        self.cell.setName(name)
+
     def __str__(self):
         return self.name
 
@@ -117,13 +133,13 @@ class Module:
         """
         return DbU.toLambda(dbu)
 
-    def place_and_route(self):
-        """ Places and routes. """
-        UpdateSession.open()
-
+    def place(self):
+        """ Places the current cell. """
         etesian = Etesian.EtesianEngine.create(self.cell)
         etesian.place()
 
+    def route(self):
+        """ Routes the current cell. """
         katana = Katana.KatanaEngine.create(self.cell)
         katana.digitalInit()
         katana.runGlobalRouter(Katana.Flags.NoFlags)
@@ -133,10 +149,13 @@ class Module:
         katana.finalizeLayout()
         result = katana.isDetailedRoutingSuccess()
         katana.destroy()
-
-        UpdateSession.close()
         return result
 
+    def place_and_route(self):
+        """ Places and routes. """
+        self.place()
+        return self.route()
+
     def create_pin_series(self, net, direction, name=None,
                           status=Pin.PlacementStatus.FIXED, layer=None,
                           x=None, y=None, width=2.0, height=2.0,
@@ -197,9 +216,7 @@ class Module:
     def init_abutment_box(self):
         """ Create the abutment box with object's initial values. """
 
-        UpdateSession.open()
         self.ab = Box(0, 0, self.to_dbu(self.width), self.to_dbu(self.height))
-        UpdateSession.close()
 
     def create_pins(self):
         """ Creates all pins set on Module object creation. """
@@ -218,8 +235,6 @@ class Module:
                 return 0
             return last_y
 
-        UpdateSession.open()
-
         for pins, direction in (
             (self.north_pins, Pin.Direction.NORTH),
             (self.east_pins, Pin.Direction.EAST),
@@ -237,8 +252,6 @@ class Module:
                     net, direction, name=name, layer=layer, x=x, y=y,
                     **pin_config)
 
-        UpdateSession.close()
-
     def create_pads_for_net(self, net, layers):
         """
         Creates a series of pads for a given net.
@@ -260,18 +273,17 @@ class Module:
     def create_pads(self):
         """ Create all pads for a given Module object. """
 
-        UpdateSession.open()
-
         for net, layers in self.pads.items():
             self.create_pads_for_net(net, layers)
 
-        UpdateSession.close()
-
     def do_submodules(self):
-        """ Execute submodules and gather their status. """
+        """
+        Execute submodules and gather their status.
+
+        :return: True if all submodules executed successfully, False otherwise.
+        """
 
         for submodule, x, y in self.submodules:
-            # execute submodule
             if not submodule.do():
                 return False
 
@@ -297,19 +309,4 @@ class Module:
     def do(self):
         """ Main routine. """
 
-        if not self.do_submodules():
-            return False
-
-        self.init_abutment_box()
-
-        self.create_pins()
-
-        if self.editor:
-            self.editor.setCell(self.cell)
-
-        result = self.place_and_route()
-
-        self.create_pads()
-
-        RSavePlugin.ScriptMain(editor=self.editor, cell=self.cell)
-        return result
+        raise NotImplementedError('You need to implement the `do` method.')