Anonymous avatar Anonymous committed 51f9284

[plugins] plantrisk items adapted to new persister

Comments (0)

Files changed (6)

plugins/plantrisk_change.py

 from limma.items.item import AbstractItem, Resource
 from limma.items.global_item_type_dict import ITEM_TYPE_TO_CLASS
 from limma.elements.elements import TextBox, PathBox, SubprocessBox
+from limma.persister.persister import PersistentObject
 
 class PlantriskChange(AbstractItem):
     "Define a PlantRisk master workbook for a case"
                               provides=provides)
 
         # first element: case name
-        self._case_name = self._container.add_or_get_object('case_name')
+        self._case_name = PersistentObject()
         self._case_box = TextBox('case', "Case",
-                                 self._case_name.get(), 
+                                 self._case_name, 
                                  editable=True)
 
         # second element: workbook with the changes
-        self._input_path = self._container.add_or_get_object('input_path')
+        self._input_path = PersistentObject()
         self._input_path_box = PathBox('input_path',
                                        "Path to PlantRisk file with changes",
-                                       self._input_path.get(),
+                                       self._input_path,
                                        editable=True)
 
         # third element: wortkbook with the merge
-        self._output_dir = self._container.add_or_get_object('output_dir')
+        self._output_dir = PersistentObject()
         self._output_dir_box = PathBox('output_dir',
                                        "Directory of the PlantRisk workbook "
                                        "to be created",
                                        editable=True)
         
         # forth element: file name to be created
-        self._output_fname = self._container.add_or_get_object('output_fname')
+        self._output_fname = PersistentObject()
         self._output_fname_box = PathBox('output_fname',
                                          "File name of the PlantRisk workbook "
                                          "to be created",
-                                         self._output_fname.get(),
+                                         self._output_fname,
                                          editable=True)
 
         # fifth element: subprocess
-        self._output = self._container.add_or_get_object('output')
+        self._output = PersistentObject()
         self._subprocess_box = SubprocessBox('subprocess', "Running console",
                                              None,
-                                             self._output.get(),
-                                             visible=(self._output.get()
+                                             self._output,
+                                             visible=(self._output
                                                       is not None))
 
-        self.result_path = self._container.add_or_get_object('result_path')
+        self.result_path = PersistentObject()
         
         self.elements = [
             self._case_box,
         
         # edit change cmd
         if element == self._case_box:
-            self._case_name.set(value)
+            self._case_name = value
             self._case_box.text = value
 
         elif element == self._input_path_box:
-            self._input_path.set(value)
+            self._input_path = value
             self._input_path_box.path = value
             if not os.path.exists(value):
                 raise ValueError("File %s does not exist" % value)
 
         elif element == self._output_dir_box:
-            self._output_dir.set(value)
+            self._output_dir = value
             self._output_dir_box.path = value
             if not os.path.exists(value):
                 raise ValueError("Directory %s does not exist" % value)
                 raise ValueError("%s is not a directory" % value)
 
         elif element == self._output_fname_box:
-            self._output_fname.set(value)
+            self._output_fname = value
             self._output_fname_box.path = value
 
         else:
 
     def ui_will_execute(self, i_element, data):
         # method call by the ui to get the command to launch the subprocess
-        case = self._case_name.get()
+        case = self._case_name
         if case is None:
             raise ValueError("case name not set")
         print data
         master_path = data[(case, 'plantrisk_workbook_path')]
-        changes_path = self._input_path.get()
+        changes_path = self._input_path
         if changes_path is None:
             raise ValueError("workbook with changes not set not set")
-        if self._output_dir.get() is None:
+        if self._output_dir is None:
             raise ValueError("Directory for new workbook not set")
-        if self._output_fname.get() is None:
+        if self._output_fname is None:
             raise ValueError("File name for new workbook not set")
-        result_path = os.path.join(self._output_dir.get(),
-                                   self._output_fname.get())
+        result_path = os.path.join(self._output_dir,
+                                   self._output_fname)
 
         script_path = os.path.join(PACKAGE_DIR, 'util', 'merge.py')
         # is it a good idea to use a single line?
             raise ValueError("invalid execution of element %d", i_element)
 
         if execution.process.returncode == 0:
-            self.processed.set(True)
-            self.failed.set(False)
+            self.processed = True
+            self.failed = False
         else:
-            self.processed.set(False)
-            self.failed.set(True)
-        self.cancelled.set(execution.cancelled)
+            self.processed = False
+            self.failed = True
+        self.cancelled = execution.cancelled
 
     def execute(self):
         raise NotImplementedError()

plugins/plantrisk_master.py

 from limma.items.item import AbstractItem, Resource
 from limma.items.global_item_type_dict import ITEM_TYPE_TO_CLASS
 from limma.elements.elements import TextBox, PathBox
+from limma.persister.persister import PersistentObject
 
 class PlantriskMaster(AbstractItem):
     "Define a PlantRisk master workbook for a case"
-    def __init__(self, name, parent_container):
+    def __init__(self, name, sheet_persister):
         provides = [Resource('plantrisk_workbook_path', '_path')]
-        AbstractItem.__init__(self, name, parent_container,
+        AbstractItem.__init__(self, name, sheet_persister,
                               provides=provides)
 
         # first element is the case
-        self._case_name = self._container.add_or_get_object('case_name')
+        self._case_name = PersistentObject()
         self._case_box = TextBox('case', "Case",
-                                 self._case_name.get(), 
+                                 self._case_name, 
                                  editable=True)
 
         # second element is the file name (path)
-        self._path = self._container.add_or_get_object('path')
+        self._path = PersistentObject()
         self._path_box = PathBox('path', "Path to PlantRisk file", 
-                                 self._path.get(),
+                                 self._path,
                                  editable=True)
 
         self.elements = [
 
         # edit case box
         if element == self._case_box:
-            self._case_name.set(value)
+            self._case_name = value
             self._case_box.text = value
 
         # edit path
         elif element == self._path_box:
-            self._path.set(value)
+            self._path = value
             self._path_box.path = value
 
             if not os.path.exists(value):
         # cursor is automatically updated up to the next item?
         if self._path_box.path is None:
             raise ValueError("Path not defined")
-        self.processed.set(True)
+        self.processed = True
 
     def ui_has_executed(self, i_element, process):
         # not needed since this item has no element to be executed by the ui

tests/test_item_plantrisk_master.py

 import os
 
 from limma.tests.helpers import StructureDirTestCase, DATA_DIR
-from limma.persister.persister import PersistentContainer
+from limma.persister.persister import FileSystemPersister
 from limma.plugins.plantrisk_master import PlantriskMaster
 
 # the indices of the elements of the PlantriskMaster item
 class Test(StructureDirTestCase):
 
     def test_shell(self):
-        container = PersistentContainer('plantrisk_master', None, 
-                                        parent_dir=self.structure_dir, 
+
+        persister = FileSystemPersister('plantrisk_master', None, 
+                                        data_dir=self.structure_dir, 
                                         reset=True)
 
         # create item
-        master = PlantriskMaster('master wb', container)
+        master = PlantriskMaster('master_wb', persister)
 
         # set case
         self.assertRaises(ValueError, master.execute)
         master.edit_element(I_ELEMENT_CASE, 'case 1')
-        self.assertFalse(master.processed.get())
+        self.assertFalse(master.processed)
 
         # set path
         self.assertRaises(ValueError, master.execute)
         test_dir = os.path.join(DATA_DIR, 'item_plantrisk_master')
         good_file = os.path.join(test_dir, 'toto.xlsm')
         master.edit_element(I_ELEMENT_PATH, good_file)        
-        self.assertFalse(master.processed.get())
+        self.assertFalse(master.processed)
 
         # execute
         master.execute()

tests/test_item_shell_no_cancel.py

 import os
 
 from limma.tests.helpers import StructureDirTestCase
-from limma.persister.persister import PersistentContainer
+from limma.persister.persister import FileSystemPersister
 from limma.plugins.shell_no_cancel import ShellNoCancelItem
 
 # the indices of the elements of the ShellNoCancelItem item
 class TestShellItem(StructureDirTestCase):
 
     def test_shell(self):
-        container = PersistentContainer('shell_no_cancel', None, 
-                                        parent_dir=self.structure_dir, 
+        persister = FileSystemPersister('shell_no_cancel', None, 
+                                        data_dir=self.structure_dir, 
                                         reset=True)
 
         # create item
-        item = ShellNoCancelItem('shell', container)
+        item = ShellNoCancelItem('shell', persister)
 
         # set first element
         if os.name == 'nt':
         else:
             cmd = 'st="eggs" && echo spam$st'  # for posix
         item.edit_element(I_ELEMENT_CMD, cmd)
-        self.assertFalse(item.processed.get())
+        self.assertFalse(item.processed)
         self.assertEqual(item.elements[I_ELEMENT_CMD].text, cmd)
 
         # execute
         item.execute()
-        self.assertTrue(item.processed.get())
-        self.assertFalse(item.failed.get())
+        self.assertTrue(item.processed)
+        self.assertFalse(item.failed)
 
         # check output
         expected_output = "spameggs"

tests/test_item_shell_with_cancel.py

 
 
 from limma.tests.helpers import StructureDirTestCase
-from limma.persister.persister import PersistentContainer
 from limma.plugins.shell_with_cancel import ShellWithCancelItem
 from limma.util.execution import Execution
+from limma.persister.persister import FileSystemPersister
 
 # the indices of the elements of the ShellNoCancelItem item
 I_ELEMENT_CMD = 0
 
 class TestShellItem(StructureDirTestCase):
 
-    # list of methods in the API (probably out-of-date as you read this...)
-    #def __init__(self, name, parent_container, widgets):
-    #def edit(self):
-    #def execute(self):
-    #def display(self):
-    #    if self.is_processed.get():
-
     def test_shell(self):
-        container = PersistentContainer('item-shell-with-cancel', None, 
-                                        parent_dir=self.structure_dir, 
+        persister = FileSystemPersister('item-shell-with-cancel', None, 
+                                        data_dir=self.structure_dir, 
                                         reset=True)
-        item = ShellWithCancelItem('shell', container)
+        item = ShellWithCancelItem('shell', persister)
 
         # edit
         if os.name == 'nt':
         else:
             cmd = 'st="eggs" && echo spam$st'
         item.edit_element(I_ELEMENT_CMD, cmd)
-        self.assertFalse(item.processed.get())
+        self.assertFalse(item.processed)
 
         # execute with cancel
         cmd_list = [cmd]
         cancelled = False
         execution = Execution(cmd_list, process, output, cancelled)
         item.ui_has_executed(I_ELEMENT_OUTPUT, execution)
-        self.assertFalse(item.processed.get())
-        self.assertTrue(item.failed.get())
-        self.assertFalse(item.cancelled.get())
+        self.assertFalse(item.processed)
+        self.assertTrue(item.failed)
+        self.assertFalse(item.cancelled)
 
         # execute until the end
         process = subprocess.Popen(cmd_list,
         cancelled = False
         execution = Execution(cmd_list, process, output, False)
         item.ui_has_executed(I_ELEMENT_OUTPUT, execution)
-        self.assertTrue(item.processed.get())
-        self.assertFalse(item.failed.get())
-        self.assertFalse(item.cancelled.get())
+        self.assertTrue(item.processed)
+        self.assertFalse(item.failed)
+        self.assertFalse(item.cancelled)
 
 if __name__ == '__main__':
     unittest.main()

tests/test_sheet.py

 from limma.tests.helpers import IOTestCase
 from limma.structure.sheet import Sheet
+from limma.persister.persister import FileSystemPersister
 
 # the indices of the elements of the upper_case item
 I_ELEMENT_ENTERED = 0
 
     def test_sheet_convering_most_code(self):
 
-        sheet = Sheet(self.structure_dir)
+        persister = FileSystemPersister('item-shell-with-cancel', None, 
+                                        data_dir=self.structure_dir, 
+                                        reset=True)
+        
+        sheet = Sheet('test-sheet', persister)
 
         # upper case is the item used for tests
         self.assertTrue('upper_case' in sheet.list_all_item_types())
 
         # add 1 item
-        self.assertEqual(sheet.get_size(), 0)
-        self.assertEqual(sheet.get_cursor(), 0)
+        self.assertEqual(len(sheet), 0)
+        self.assertEqual(sheet.cursor, 0)
 
         # add and edit first item
         sheet.add_item('upper_case')
 
         # check content
         i_item = 0
-        item = sheet.get_item(i_item)
+        item = sheet.items[i_item]
         element = item.elements[I_ELEMENT_ENTERED]
         self.assertEqual(element.text, text_0)
         i_item = 1
-        item = sheet.get_item(i_item)
+        item = sheet.items[i_item]
         element = item.elements[I_ELEMENT_ENTERED]
         self.assertEqual(element.text, text_1)
 
         # execute 2 items, the cursor is updated
-        self.assertEqual(sheet.get_cursor(), 0)
-        sheet.execute_item(sheet.get_cursor())
-        self.assertEqual(sheet.get_cursor(), 1)
-        sheet.execute_item(sheet.get_cursor())
-        self.assertEqual(sheet.get_cursor(), 2)
+        self.assertEqual(sheet.cursor, 0)
+        sheet.execute_item(sheet.cursor)
+        self.assertEqual(sheet.cursor, 1)
+        sheet.execute_item(sheet.cursor)
+        self.assertEqual(sheet.cursor, 2)
 
         # edit 0th item, the cursor must change
         i_item = 0
         text_0_new = 'tomatoes'
         sheet.edit_element(i_item, I_ELEMENT_ENTERED, text_0_new)
-        item = sheet.get_item(i_item)
+        item = sheet.items[i_item]
         self.assertEqual(item.elements[0].text, text_0_new)
-        self.assertEqual(sheet.get_cursor(), 0)
+        self.assertEqual(sheet.cursor, 0)
 
         # execute 1 item
-        self.assertEqual(sheet.get_cursor(), 0)
-        sheet.execute_item(sheet.get_cursor())
-        self.assertEqual(sheet.get_cursor(), 1)
+        self.assertEqual(sheet.cursor, 0)
+        sheet.execute_item(sheet.cursor)
+        self.assertEqual(sheet.cursor, 1)
 
         # check aux methods
         self.assertTrue('upper_case' in sheet.list_all_item_types())
         i_item = 0
-        item = sheet.get_item(i_item)
+        item = sheet.items[i_item]
         self.assertEqual('upper_case', sheet.item_type_name(item))
         self.assertRaises(TypeError,  sheet.item_type_name, 'just a string')
         
         # delete second item: cursor does not change
         sheet.delete_item(1)
-        self.assertEqual(sheet.get_size(), 1)
-        self.assertEqual(sheet.get_cursor(), 1)
+        self.assertEqual(len(sheet), 1)
+        self.assertEqual(sheet.cursor, 1)
 
         # delete first item: cursor changes
         sheet.delete_item(0)
-        self.assertEqual(sheet.get_size(), 0)
-        self.assertEqual(sheet.get_cursor(), 0)
+        self.assertEqual(len(sheet), 0)
+        self.assertEqual(sheet.cursor, 0)
 
         # try to delete once more: error
         self.assertRaises(ValueError, sheet.delete_item, 0)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.