class Menu(object):
    def __init__(self, dialog, items, title, caller = None):
        self.d = dialog
        self.caller = caller

        self.entries = []
        self.dispatch_table = {}
        tag = 1

        self.title = title

        for entry, func in items:
            self.entries.append(tuple([str(tag), entry]))
            self.dispatch_table[str(tag)] = func
            tag += 1

    def run(self, ret=None):
        code, tag = self.d.menu(self.title, choices=self.entries)
        if code == self.d.OK: self.dispatch(tag)
        if ret: ret()

    def dispatch(self, tag):
        if tag in self.dispatch_table:
            func = self.dispatch_table[tag]
            if isinstance(func, Menu):
                func.run(ret=self.run)
            else: func()


class MenuItem(object):
    def __init__(self, func=None):
        if func: self.function = func

    # Wrapper for child.function() that creates a call stack
    def run(self, ret=None):
        self.function()
        if ret: ret()

class Menu(MenuItem):
    def __init__(self, dialog, items, title):
        self.d = dialog

        self.entries = []
        self.dispatch_table = {}
        tag = 1

        self.title = title

        for entry, func in items:
            self.entries.append(tuple([str(tag), entry]))
            self.dispatch_table[str(tag)] = func
            tag += 1

    def function(self):
        code, tag = self.d.menu(self.title, choices=self.entries)
        if code == self.d.OK: self._dispatch(tag)

    def _dispatch(self, tag):
        if tag in self.dispatch_table:
            func = self.dispatch_table[tag]
            if isinstance(func, MenuItem):
                func.run(ret=self.run)
            else: func()


Create object MenuItem that wraps functions to create a call stack
Create object MenuItem that wraps functions to create a call stack

- class Menu(object):
+ class MenuItem(object):
+     def __init__(self, func=None):
+         if func: self.function = func
+ 
+     # Wrapper for child.function() that creates a call stack
+     def run(self, ret=None):
+         self.function()
+         if ret: ret()
+ 
+ class Menu(MenuItem):
-     def __init__(self, dialog, items, title, caller = None):
+     def __init__(self, dialog, items, title):
          self.d = dialog
-         self.caller = caller
  
          self.entries = []
          self.dispatch_table = {}
          tag = 1
  
          self.title = title
  
          for entry, func in items:
              self.entries.append(tuple([str(tag), entry]))
              self.dispatch_table[str(tag)] = func
              tag += 1
  
-     def run(self, ret=None):
+     def function(self):
          code, tag = self.d.menu(self.title, choices=self.entries)
-         if code == self.d.OK: self.dispatch(tag)
+         if code == self.d.OK: self._dispatch(tag)
-         if ret: ret()
  
-     def dispatch(self, tag):
+     def _dispatch(self, tag):
          if tag in self.dispatch_table:
              func = self.dispatch_table[tag]
-             if isinstance(func, Menu):
+             if isinstance(func, MenuItem):
                  func.run(ret=self.run)
              else: func()
  
  
Create object MenuItem that wraps functions to create a call stack
## Code Before:
class Menu(object):
    def __init__(self, dialog, items, title, caller = None):
        self.d = dialog
        self.caller = caller

        self.entries = []
        self.dispatch_table = {}
        tag = 1

        self.title = title

        for entry, func in items:
            self.entries.append(tuple([str(tag), entry]))
            self.dispatch_table[str(tag)] = func
            tag += 1

    def run(self, ret=None):
        code, tag = self.d.menu(self.title, choices=self.entries)
        if code == self.d.OK: self.dispatch(tag)
        if ret: ret()

    def dispatch(self, tag):
        if tag in self.dispatch_table:
            func = self.dispatch_table[tag]
            if isinstance(func, Menu):
                func.run(ret=self.run)
            else: func()


## Instruction:
Create object MenuItem that wraps functions to create a call stack
## Code After:
class MenuItem(object):
    def __init__(self, func=None):
        if func: self.function = func

    # Wrapper for child.function() that creates a call stack
    def run(self, ret=None):
        self.function()
        if ret: ret()

class Menu(MenuItem):
    def __init__(self, dialog, items, title):
        self.d = dialog

        self.entries = []
        self.dispatch_table = {}
        tag = 1

        self.title = title

        for entry, func in items:
            self.entries.append(tuple([str(tag), entry]))
            self.dispatch_table[str(tag)] = func
            tag += 1

    def function(self):
        code, tag = self.d.menu(self.title, choices=self.entries)
        if code == self.d.OK: self._dispatch(tag)

    def _dispatch(self, tag):
        if tag in self.dispatch_table:
            func = self.dispatch_table[tag]
            if isinstance(func, MenuItem):
                func.run(ret=self.run)
            else: func()

