def __init__(self, **kwds): self.parent = kwds.pop('parent', None) self._widget = None mode_name, name_value = check_mutually_exclusive(kwds, 'name', 'filepath') mode_model, model_value = check_mutually_exclusive(kwds, 'model', 'data') _, code = check_mutually_exclusive(kwds, 'code', 'content') if name_value and model_value: raise ValueError('model/data and filename/name/content/code are mutually exclusive') if mode_name or mode_model: self._type = Model else: self._type = Data if name_value and self._type == Model: self._obj = ModelFactory(name=name_value, mimetype=self.mimetype_model, code=code) elif name_value and self._type == Data: self._obj = DataFactory(path=name_value, mimetype=self.mimetype_data, default_content=code) elif model_value: self._obj = model_value else: self._obj = ModelFactory(name='NewModel', mimetype=self.mimetype_model) self._type = Model if self._type == Model: self._model = self._obj else: self._model = to_model(self._obj)
def test_constructor(): model = ModelFactory(mimetype='text/x-python') model.inputs_info = [InputObj('a'), InputObj('b')] model.outputs_info = [OutputObj('c')] model.set_step_code('c = a + b') # Use a non existing directory to be sure data is not written on disk data = DataFactory('/tmp/donotexists/test.py', default_content="print('I am a python file')") c1 = "print('hello, I am a new model')" c2 = "print('hello, I am a new data')" kwds = [ dict(name='Model1', content=c1), dict(filepath='/tmp/donotexists/test.py', content=c2), dict(model=model), dict(data=data), ] controller = PythonModelController(**kwds[0]) assert controller.value() == c1 assert hasattr(controller.model, 'run') controller = PythonModelController(**kwds[1]) assert controller.value() == c2 assert hasattr(controller.model, 'run') assert hasattr(controller._obj, 'run') is False controller = PythonModelController(**kwds[2]) assert controller._obj is model assert hasattr(controller.model, 'run') controller = PythonModelController(**kwds[3]) assert controller._obj is data assert hasattr(controller.model, 'run')
def test_path_incompatibility(self): model = DataFactory(path=get_data('model.py')) model2 = self.project.add('model', model) events = self.ev.events self.check_events(events, ['data_added', 'project_changed']) assert model is not model2 assert model2.path == self.project.path / 'model' / 'model.py'
def new_paradigm(self, dtype): p, filters = getsavefilename(self, "New file") if p: if p.exists(): p.remove() data = DataFactory(p, dtype=dtype) self.open_data(data)
def test_new_data(self): # Create new data from scratch (image_1.tiff doesn't exist) filepath = self.tmpdir / 'new_image.tiff' default_content = b'blob' d1 = DataFactory(path=filepath, default_content=default_content) assert d1.path.name == 'new_image.tiff' assert filepath.isfile() assert d1.exists() is True # To check if memory has been freed assert d1._content is None f = filepath.open('rb') content2 = f.read() f.close() assert default_content == content2 # Embed existing data (image.tiff exists) d2 = DataFactory(path=get_data('image.tiff')) assert d2.path.name == 'image.tiff' # Cannot set content because data yet exists # Case mimetype is not defined with self.assertRaises(ValueError) as cm: d3 = DataFactory(path=get_data('image.tiff'), default_content=b'') msg = "got multiple values for content (parameter and 'image.tiff')" self.assertEqual(cm.exception.message, msg) # Path is a directory and not a file empty_dir = self.tmpdir / "empty" empty_dir.mkdir() # No need to remove it after test. Parent dir will be removed in tearDown with self.assertRaises(ValueError) as cm: d4 = DataFactory(path=empty_dir, default_content=b'') msg = "'%s' exists but is not a file" % empty_dir self.assertEqual(cm.exception.message, msg) d5 = DataFactory(path=self.tmpdir / "doesnotexist" / "image.tiff", default_content=default_content) assert d5.exists() == False assert d5._content == default_content
def test_DataClass(self): from openalea.core.data import Data, PythonFile d = DataFactory(path=self.tmpdir / 'test.dat') self.assertIsInstance(d, Data) d = DataFactory(path=self.tmpdir / 'test.py') self.assertIsInstance(d, PythonFile) d = DataFactory(path=self.tmpdir / 'test.py', dtype='data') self.assertIsInstance(d, Data) with self.assertRaises(ValueError) as cm: DataFactory(self.tmpdir / 'model.py', mimetype='image/tiff', dtype='Python') msg = "dtype 'Python' (text/x-python) and mimetype 'image/tiff' are not compatible" self.assertEqual(cm.exception.args[0], msg) # Case file do not exists on disk ... # Only dtype is available d = DataFactory(path=self.tmpdir / 'test.py', dtype='Python') self.assertEqual(d.mimetype, 'text/x-python') # Only mimetype is available d = DataFactory(path=self.tmpdir / 'test.py', mimetype='text/x-python') self.assertEqual(d.mimetype, 'text/x-python')
def test_new_data(self): # Create new data from scratch (image_1.tiff doesn't exist) filepath = self.tmpdir / 'new_image.tiff' default_content = b'blob' d1 = DataFactory(path=filepath, default_content=default_content) assert d1.path.name == 'new_image.tiff' assert filepath.isfile() assert d1.exists() is True # To check if memory has been freed assert d1._content is None f = filepath.open('rb') content2 = f.read() f.close() assert default_content == content2 # Embed existing data (image.tiff exists) d2 = DataFactory(path=get_data('image.tiff')) assert d2.path.name == 'image.tiff' # Cannot set content because data yet exists # Case mimetype is not defined with self.assertRaises(ValueError) as cm: d3 = DataFactory(path=get_data('image.tiff'), default_content=b'') msg = "got multiple values for content (parameter and 'image.tiff')" self.assertEqual(cm.exception.args[0], msg) # Path is a directory and not a file empty_dir = self.tmpdir / "empty" empty_dir.mkdir( ) # No need to remove it after test. Parent dir will be removed in tearDown with self.assertRaises(ValueError) as cm: d4 = DataFactory(path=empty_dir, default_content=b'') msg = "'%s' exists but is not a file" % empty_dir self.assertEqual(cm.exception.args[0], msg) d5 = DataFactory(path=self.tmpdir / "doesnotexist" / "image.tiff", default_content=default_content) assert d5.exists() == False assert d5._content == default_content
def open_file(self, filepath=None): if filepath in (None, True, False): filepath, filters = getopenfilename(self, u"Select file") if filepath is None: return filepath = path(filepath).normpath().abspath() # check if a data in container yet correspond to this path found = None for data in self._open_objects: if filepath == data.path.normpath().abspath(): found = data # If not, create a new data if found: data = found else: data = DataFactory(filepath) self.open_data(data) return data
def test(): model1 = DataFactory('data/model.py') pmw.open_data(model1)
def _add_item(self, category, obj=None, **kwargs): mode = kwargs.pop('mode', self.MODE_COPY) if obj and isinstance(obj, Data): # TODO: Check obj follow Data or Model interface ?? new_path = self.path / category / obj.path.name if obj.path != new_path and mode == self.MODE_COPY: # TODO: use Data.copy instead return self._add_item(category, path=obj.path, **kwargs) category_dict = getattr(self, category) if obj.filename not in category_dict: category_dict[str(obj.filename)] = obj else: raise ValueError("data '%s' already exists in project '%s'" % (obj.filename, self.alias)) elif obj: category_dict = getattr(self, category) if obj.name not in category_dict: category_dict[str(obj.name)] = obj else: raise ValueError("data '%s' already exists in project '%s'" % (obj.name, self.alias)) else: filename = Path( kwargs.pop('filename')) if 'filename' in kwargs else None content = kwargs.pop('content', None) dtype = kwargs.pop('dtype', None) mimetype = kwargs.pop('mimetype', None) path = Path(kwargs.pop('path')) if 'path' in kwargs else None # If project path exists, ie project exists on disk, # Create category dir if necessary category_path = self.path / category if self.path.exists() and not category_path.exists(): category_path.makedirs() if filename: new_path = self.path / category / filename.name elif path: if not path.exists(): raise ErrorInvalidItem("path '%s' doesn't exists" % path) filename = path.name new_path = self.path / category / filename else: raise ValueError("path or filename required") if path is None: path = new_path # If data was outside project, we try to fix it. # If mode is "prefer copy", we try to copy file inside project # If copy fails, we get original content and pass it to new data # If mode is "prefer link", we just keep original path (keep outside project) # TODO: Move to Data.copy data_obj = None if new_path.abspath() != path.abspath() and mode == self.MODE_COPY: try: path.copyfile(new_path) except IOError: data_obj = DataFactory(path, mimetype, dtype=dtype, default_content=content) content = data_obj.read() else: content = None elif new_path.abspath() != path.abspath( ) and mode == self.MODE_LINK: new_path = path else: pass # Nothing to do, data is yet in the right place data_obj = DataFactory(new_path, mimetype, dtype=dtype, default_content=content) obj = self._add_item(category, data_obj, **kwargs) obj.package = self return obj
def _add_item(self, category, obj=None, **kwargs): mode = kwargs.pop('mode', self.MODE_COPY) if obj and isinstance(obj, Data): # TODO: Check obj follow Data or Model interface ?? new_path = self.path / category / obj.path.name if obj.path != new_path and mode == self.MODE_COPY: # TODO: use Data.copy instead return self._add_item(category, path=obj.path, **kwargs) category_dict = getattr(self, category) if obj.filename not in category_dict: category_dict[str(obj.filename)] = obj else: raise ValueError("data '%s' already exists in project '%s'" % (obj.filename, self.alias)) elif obj: category_dict = getattr(self, category) if obj.name not in category_dict: category_dict[str(obj.name)] = obj else: raise ValueError("data '%s' already exists in project '%s'" % (obj.name, self.alias)) else: filename = Path(kwargs.pop('filename')) if 'filename' in kwargs else None content = kwargs.pop('content', None) dtype = kwargs.pop('dtype', None) mimetype = kwargs.pop('mimetype', None) path = Path(kwargs.pop('path')) if 'path' in kwargs else None # If project path exists, ie project exists on disk, # Create category dir if necessary category_path = self.path / category if self.path.exists() and not category_path.exists(): category_path.makedirs() if filename: new_path = self.path / category / filename.name elif path: if not path.exists(): raise ErrorInvalidItem("path '%s' doesn't exists" % path) filename = path.name new_path = self.path / category / filename else: raise ValueError("path or filename required") if path is None: path = new_path # If data was outside project, we try to fix it. # If mode is "prefer copy", we try to copy file inside project # If copy fails, we get original content and pass it to new data # If mode is "prefer link", we just keep original path (keep outside project) # TODO: Move to Data.copy data_obj = None if new_path.abspath() != path.abspath() and mode == self.MODE_COPY: try: path.copyfile(new_path) except IOError: data_obj = DataFactory(path, mimetype, dtype=dtype, default_content=content) content = data_obj.read() else: content = None elif new_path.abspath() != path.abspath() and mode == self.MODE_LINK: new_path = path else: pass # Nothing to do, data is yet in the right place data_obj = DataFactory(new_path, mimetype, dtype=dtype, default_content=content) obj = self._add_item(category, data_obj, **kwargs) obj.package = self return obj
def open(self): filepath = showOpenFileDialog() if filepath: filepath = path(filepath) obj = DataFactory(path=filepath) self.open_data(obj)
def open_file(self, path): # This applet use "call applet" approach paradigm_container = self.paradigm_container if paradigm_container: paradigm_container.open_data(DataFactory(path))