def test_load_and_resave_project(self):
        """ Does load and resaving a project work correctly ?
        """
        project_name = create_unique_project_name(self.dir_name,
                                                  'test_load_save_project')
        project_dir = os.path.join(self.dir_name, project_name+'_files')
        project_file = os.path.join(self.dir_name, project_name+'.prj')

        script_path = os.path.join(project_dir, project_name+'.py')
        context_path = os.path.join(project_dir, project_name+'.pickle')
        layout_path = os.path.join(project_dir, 'layout.pickle')
        script = '\n'.join(('SCRIPT_PATH = '+script_path,
                            'CONTEXT_PATH = '+context_path,
                            'LAYOUT_PATH = '+layout_path))

        os.makedirs(project_dir)

        file_object = open(project_file, 'wb')
        file_object.write(script)
        file_object.close()
        file_object = open(script_path, 'w')
        file_object.write(self.code)
        file_object.close()
        d = DataContext(name = 'dummy_context',
                        _bindings = {'a': 1, 'b':2, 'e': 0.01})
        d.save_context_to_file(context_path)


        # Check if the setup is correct
        self.assertTrue([os.path.exists(project_file),
                         os.path.exists(project_dir),
                         os.path.exists(script_path),
                         os.path.exists(context_path)])

        # Load the project from the file.
        self.app.block_unit = BlockUnit()
        self.app.load_project_from_file(project_file)
        self.app.block_unit.codeblock.code = self.app.block_unit.codeblock.code + '\nd = add(a,c)'
        self.app.save_loaded_project()

        # The key 'context' should get removed while saving the project.
        expected_keys = set(['a', 'b', 'c', 'd', 'e'])
        self.assertEqual(set(self.app.block_unit.data_context.keys()),
                         expected_keys)
        self.assertEqual(self.app.block_unit.data_context['d'], 4)

        # Check if the new code contains the new line
        file_object = open(script_path, 'r')
        code = file_object.read()
        a,b = 1,2
        exec code
        file_object.close()
        self.assertEqual(d, 4)

        # Clean up after loading
        os.remove(project_file)
        os.remove(script_path)
        os.remove(context_path)
        os.remove(layout_path)
        os.rmdir(project_dir)
    def test_save_new_project(self):
        """ Does saving a new project to file work correctly ?
        """
        context = DataContext(name = 'dummy_context',
                              _bindings = {'a':1, 'b':2})
        self.app.block_unit = BlockUnit(code=self.code, data_context = context)

        project_name = create_unique_project_name(self.dir_name,
                                                  'test_save_project')
        project_file = os.path.join(self.dir_name,project_name+'.prj')
        self.app.save_project_to_file(project_file)

        # Check if the new files exist
        project_dir = os.path.join(self.dir_name,project_name+'_files')
        context_file = os.path.join(project_dir, project_name+'.pickle')
        script_file = os.path.join(project_dir, project_name+'.py')
        layout_file = os.path.join(project_dir, 'layout.pickle')

#        print project_file, project_dir, context_file, script_file
        self.assertTrue([os.path.exists(project_file),
                         os.path.exists(project_dir),
                         os.path.exists(context_file),
                         os.path.exists(script_file)])

        # Check if the code was saved out correctly
        file_object = open(script_file, 'r')
        check_code = file_object.read()
        file_object.close()
        a, b = 1,2
        exec check_code
        self.assertTrue(c, 3)

        # Check if the project file was written out correctly
        file_object = open(project_file, 'rb')
        check_code = file_object.read()
        file_object.close()
        actual_result = [line[line.find('=')+1:].strip() for line in
                         check_code.split('\n')]

        self.assertTrue(actual_result==[script_file, context_file, layout_file])

        # Check if the context is correct
        retrieved_context = DataContext.load_context_from_file(context_file)
        expected_keys = set(['a', 'b', 'c'])
        self.assertEqual(retrieved_context.name, 'dummy_context')
        self.assertEqual(set(retrieved_context.keys()), expected_keys)
        self.assertEqual(retrieved_context['c'], 3)

        # Cleanup
        os.remove(project_file)
        os.remove(script_file)
        os.remove(context_file)
        os.remove(layout_file)
        os.rmdir(project_dir)
    def closed(self, info, is_ok):
        if is_ok:
            pfui = info.object
            project_name = create_unique_project_name(pfui.base_dir, pfui.project_name)
            if project_name != pfui.project_name:
                logger.debug(
                    "Since %s already exists, the project is " "named as %s" % (pfui.project_name, project_name)
                )
                pfui.project_name = project_name

        return
 def _on_save_project_as(self, info):
     """ Saving project as a folder containing pickled data, and script
     """
     project = info.object.project
     base_dir = self._get_current_project_dir(project)
     project_name = create_unique_project_name(base_dir, 'Converge Project')
     pfui = ProjectFolderUI(base_dir = base_dir,
                            project_name = project_name)
     ui = pfui.edit_traits(kind = 'livemodal')
     if ui.result:
         project_dir = os.path.join(pfui.base_dir, pfui.project_name)
         self._save_project_graphically(info.ui.control, project, project_dir)
     return
    def test_load_project_from_file(self):
        """ Does loading a project from file work correctly ?
        """
        # Preparing the test case
        # Make project.py, project.pickle, project.prj
        project_name = create_unique_project_name(self.dir_name,
                                                  'test_load_project')
        project_dir = os.path.join(self.dir_name, project_name+'_files')
        project_file = os.path.join(self.dir_name, project_name+'.prj')

        script_path = os.path.join(project_dir, project_name+'.py')
        context_path = os.path.join(project_dir, project_name+'.pickle')
        script = '\n'.join(('SCRIPT_PATH = '+script_path,
                            'CONTEXT_PATH = '+context_path))

        os.makedirs(project_dir)

        file_object = open(project_file, 'wb')
        file_object.write(script)
        file_object.close()
        file_object = open(script_path, 'w')
        file_object.write(self.code)
        file_object.close()
        d = DataContext(name = 'dummy_context',
                        _bindings = {'a': 1, 'b':2, 'd': 0.01})
        d.save_context_to_file(context_path)


        # Check if the setup is correct
        self.assertTrue([os.path.exists(project_file),
                         os.path.exists(project_dir),
                         os.path.exists(script_path),
                         os.path.exists(context_path)])

        # Load the project from the file.
        self.app.block_unit = BlockUnit()
        self.app.load_project_from_file(project_file)

        # Clean up after loading
        os.remove(project_file)
        os.remove(script_path)
        os.remove(context_path)
        os.rmdir(project_dir)

        expected_keys = set(['a', 'b', 'c', 'd', 'context'])
        self.assertEqual(set(self.app.block_unit.data_context.keys()),
                         expected_keys)
        self.assertTrue(self.app.block_unit.data_context.has_key('c'))
        self.assertEqual(self.app.block_unit.data_context['c'], 3)