Commits

Ginés Martínez Sánchez  committed 42ebb30

add a new function: create_random_named_gobj

  • Participants
  • Parent commits e03e0f3

Comments (0)

Files changed (3)

File ginsfsm/gaplic.py

         self.thread_name = 0
         self.gaplic = self
         self.deferred_list = DeferredList()
-
+        self._urandom_name = 0
         logger = settings.get('logger', None)
         if isinstance(logger, string_types):
             self.logger = logging.getLogger(logger)
         gobj = GObj.create_gobj(self, name, gclass, parent, **kw)
         return gobj
 
+    def get_random_name(self):
+        self._urandom_name += 1
+        return 'random-%d' + self._urandom_name
+
+    def create_random_named_gobj(self, name, gclass, parent, **kw):
+        """ Same as :meth:`create_gobj` function,
+        but it generates a random name if name it's not supplied.
+        """
+        if not name:
+            name = self.get_random_name()
+        return self.create_gobj(name, gclass, parent, **kw)
+
     def get_named_gobjs(self):
         """ Return the list of :term:`named-gobj`'s.
         """

File ginsfsm/gobj.py

                         repr(gclass)))
 
         gobj = gclass()
-        if parent is not None:
-            parent._add_child(gobj)
 
         if name:
             gobj.name = name
                     '''ERROR create_gobj(): gobj '%s' '''
                     ''' is the unique named''' % (name))
 
+        if parent is not None:
+            parent._add_child(gobj)
+
         # What must be the first? arguments or global settings??
         gobj.write_parameters(**kw)
         if self.global_settings is not None:

File ginsfsm/tests/test_gobj1.py

         self.assertRaises(DestinationError, self.gobj_parent._event_factory,
             1, 'EV_QUERY_BY_DIRECT')
 
-        event = self.gobj_parent._event_factory(None, 'EV_QUERY_BY_DIRECT',
-                                               data='query1')
+        event = self.gobj_parent._event_factory(
+            'pepe',
+            'EV_QUERY_BY_DIRECT',
+            data='query1')
         self.assertRaises(DestinationError, self.gobj_parent._event_factory,
             1, event)
 
-        event = self.gobj_parent._event_factory(None, event, data2='query2')
-        self.assertEqual(event.destination, None)
+        event = self.gobj_parent._event_factory('pepe', event, data2='query2')
+        self.assertEqual(event.destination, 'pepe')
         self.assertEqual(event.data, 'query1')
         self.assertEqual(event.data2, 'query2')
         event = self.gobj_parent._event_factory(self.gobj_parent, event)
 
     def test_search_gobj(self):
         self.assertTrue(
-            self.gobj_parent.search_unique_gobj(self.gobj_parent) == None)
+            self.gobj_parent.find_unique_gobj(self.gobj_parent) == None)