def with_dataset(self, name=None): """Yield an editable metadata object for a dataset.""" from renku.models._locals import with_reference from renku.models.refs import LinkReference dataset = self.load_dataset(name=name) if dataset is None: identifier = str(uuid.uuid4()) path = (self.renku_datasets_path / identifier / self.METADATA) path.parent.mkdir(parents=True, exist_ok=True) with with_reference(path): dataset = Dataset( identifier=identifier, name=name, client=self ) if name: LinkReference.create(client=self, name='datasets/' + name).set_reference(path) dataset_path = self.path / self.datadir / dataset.name dataset_path.mkdir(parents=True, exist_ok=True) yield dataset # TODO # if path is None: # path = dataset_path / self.METADATA # if path.exists(): # raise ValueError('Dataset already exists') dataset.to_yaml()
def from_jsonld( cls, data, client=None, __reference__=None, __source__=None, ): """Instantiate a JSON-LD class from data.""" if isinstance(data, cls): return data if not isinstance(data, dict): raise ValueError(data) if '@type' in data: type_ = tuple(sorted(data['@type'])) if type_ in cls.__type_registry__ and getattr( cls, '_jsonld_type', None) != type_: new_cls = cls.__type_registry__[type_] if cls != new_cls: return new_cls.from_jsonld(data, client=client) if cls._jsonld_translate: data = ld.compact(data, {'@context': cls._jsonld_translate}) data.pop('@context', None) data.setdefault('@context', cls._jsonld_context) if data['@context'] != cls._jsonld_context: try: compacted = ld.compact(data, {'@context': cls._jsonld_context}) except Exception: compacted = data else: compacted = data for migration in MIGRATIONS: data = migration(data) fields = cls._jsonld_fields data_ = {} if client: data_['client'] = client for k, v in compacted.items(): if k in fields: data_[k.lstrip('_')] = v if __reference__: with with_reference(__reference__): self = cls(**data_) else: self = cls(**data_) if __source__: setattr(self, '__source__', __source__) return self
def from_cwl(cls, data, __reference__=None): """Return an instance from CWL data.""" class_name = data.get('class', None) cls = cls.registry.get(class_name, cls) if __reference__: with with_reference(__reference__): self = cls( **{k: v for k, v in iteritems(data) if k != 'class'}) else: self = cls(**{k: v for k, v in iteritems(data) if k != 'class'}) return self