Repository: ResearchObject/ro-crate-py
Pull request #42 (2021-02-04 16:54:23)

Misc fixes
Various fixes


Patch:
diff --git a/README.md b/README.md
index 8e742d4..bc1e8f5 100644
--- a/README.md
+++ b/README.md
@@ -59,6 +59,8 @@ python setup.py install
 In general you will want to start by instantiating the `ROCrate` object. This can be a new one: 
 
 ```python
+from rocrate.rocrate import ROCrate
+
 crate = ROCrate() 
 ```
 
@@ -110,6 +112,8 @@ dataset_entity = crate.add_directory(sample_dir, 'relative/rocrate/path')
 [Contextual entities](https://www.researchobject.org/ro-crate/1.1/contextual-entities.html) are used in an RO-Crate to adequately describe a Data Entity. The following example shows how to add the person contextual entity to the RO-Crate root:
 
 ```python
+from rocrate.model.person import Person
+
 # Add authors info
 crate.add(Person(crate, '#joe', {'name': 'Joe Bloggs'}))
 
diff --git a/rocrate/model/workflow.py b/rocrate/model/computationalworkflow.py
similarity index 94%
rename from rocrate/model/workflow.py
rename to rocrate/model/computationalworkflow.py
index d9a4022..dff684f 100644
--- a/rocrate/model/workflow.py
+++ b/rocrate/model/computationalworkflow.py
@@ -22,7 +22,7 @@ import os
 from .file import File
 
 
-class Workflow(File):
+class ComputationalWorkflow(File):
     """\
     A scientific workflow that was used (or can be used) to analyze or
     generate files in the RO-Crate.
@@ -37,6 +37,6 @@ class Workflow(File):
         }
 
 
-class LegacyWorkflow(Workflow):
+class Workflow(ComputationalWorkflow):
 
     TYPES = ["File", "SoftwareSourceCode", "Workflow"]
diff --git a/rocrate/model/file.py b/rocrate/model/file.py
index ef8b598..36c853c 100644
--- a/rocrate/model/file.py
+++ b/rocrate/model/file.py
@@ -33,7 +33,9 @@ from .data_entity import DataEntity
 class File(DataEntity):
 
     def __init__(self, crate, source=None, dest_path=None, fetch_remote=False,
-                 validate_url=True, properties={}):
+                 validate_url=True, properties=None):
+        if properties is None:
+            properties = {}
         # process source
         self.fetch_remote = fetch_remote
         self.source = source
@@ -81,8 +83,6 @@ class File(DataEntity):
                     identifier = os.path.basename(source)
                 else:
                     identifier = source
-                if not properties:
-                    properties = {}
                 # set url to the source. When creating through workflowhub
                 # this is auto set to URL Workflow Hub page
                 properties.update({'url': source})
diff --git a/rocrate/rocrate.py b/rocrate/rocrate.py
index 1f2361c..78becba 100644
--- a/rocrate/rocrate.py
+++ b/rocrate/rocrate.py
@@ -354,21 +354,14 @@ class ROCrate():
         canonical_id = self.resolve_id(entity_id)
         return self.__entity_map.get(canonical_id, None)
 
-    # source: file object or path (str)
-    def add_file(self, source, crate_path=None, fetch_remote=False,
-                 properties={}, **kwargs):
-        props = dict(properties)
-        props.update(kwargs)
-        file_entity = File(self, source=source, dest_path=crate_path, fetch_remote=fetch_remote, properties=props)
-        self.add(file_entity)
-        return file_entity
-
-    def add_directory(self, source, crate_path=None, properties={}, **kwargs):
-        props = dict(properties)
-        props.update(kwargs)
-        dataset_entity = Dataset(self, source, crate_path, properties)
-        self.add(dataset_entity)
-        return dataset_entity
+    def add_file(self, source, dest_path=None, fetch_remote=False,
+                 validate_url=True, properties=None):
+        return self.add(File(self, source, dest_path=dest_path, fetch_remote=fetch_remote, properties=properties))
+
+    def add_dataset(self, source, dest_path=None, properties=None):
+        return self.add(Dataset(self, source, dest_path=dest_path, properties=properties))
+
+    add_directory = add_dataset
 
     def add(self, *entities):
         for e in entities:
@@ -389,6 +382,7 @@ class ROCrate():
                 self.data_entities.append(e)
             else:
                 self.contextual_entities.append(e)
+        return entities[0] if len(entities) == 1 else entities
 
     # TODO
     # def fetch_all(self):
diff --git a/rocrate/rocrate_api.py b/rocrate/rocrate_api.py
index d7827ca..de9d67a 100644
--- a/rocrate/rocrate_api.py
+++ b/rocrate/rocrate_api.py
@@ -24,7 +24,7 @@ from contextlib import redirect_stdout
 from pathlib import Path
 
 import rocrate.rocrate as roc
-from rocrate.model.workflow import Workflow
+from rocrate.model.computationalworkflow import ComputationalWorkflow
 from rocrate.model.computerlanguage import get_lang
 from galaxy2cwl import get_cwl_interface
 
@@ -35,7 +35,7 @@ def galaxy_to_abstract_cwl(crate, workflow_path):
             get_cwl_interface.main(['1', str(workflow_path)])
     atexit.register(os.unlink, f.name)
     abstract_wf_id = workflow_path.with_suffix(".cwl").name
-    return Workflow(crate, f.name, abstract_wf_id)
+    return ComputationalWorkflow(crate, f.name, abstract_wf_id)
 
 
 def make_workflow_rocrate(workflow_path, wf_type, include_files=[],
@@ -78,8 +78,7 @@ def make_workflow_rocrate(workflow_path, wf_type, include_files=[],
     wf_crate = roc.ROCrate()
     workflow_path = Path(workflow_path)
     # should this be added in a special path within the crate?
-    wf_file = Workflow(wf_crate, str(workflow_path), workflow_path.name)
-    wf_crate.add(wf_file)
+    wf_file = wf_crate.add(ComputationalWorkflow(wf_crate, str(workflow_path), workflow_path.name))
     wf_crate.mainEntity = wf_file
     lang = get_lang(wf_crate, wf_type)
     wf_crate.add(lang)


Test patch:
diff --git a/test/test_api.py b/test/test_api.py
index b23dc43..d96dbe9 100644
--- a/test/test_api.py
+++ b/test/test_api.py
@@ -26,6 +26,7 @@ def test_galaxy_wf_crate(test_data_dir, tmpdir, helpers):
     assert isinstance(wf_crate, ROCrate)
 
     wf = wf_crate.dereference(wf_id)
+    assert wf._default_type == "ComputationalWorkflow"
     assert wf_crate.mainEntity is wf
     lang = wf_crate.dereference("#galaxy")
     assert hasattr(lang, "name")
diff --git a/test/test_model.py b/test/test_model.py
index 010bd90..2666c9a 100644
--- a/test/test_model.py
+++ b/test/test_model.py
@@ -70,8 +70,7 @@ def test_dereferencing_equivalent_id(test_data_dir, name):
 
 def test_contextual_entities():
     crate = ROCrate()
-    new_person = Person(crate, '#joe', {'name': 'Joe Pesci'})
-    crate.add(new_person)
+    new_person = crate.add(Person(crate, '#joe', {'name': 'Joe Pesci'}))
     person_dereference = crate.dereference('#joe')
     assert person_dereference is new_person
     assert person_dereference.type == 'Person'
@@ -80,8 +79,7 @@ def test_contextual_entities():
     assert person_prop['name'] == 'Joe Pesci'
     assert not new_person.datePublished
     id_ = "https://orcid.org/0000-0002-1825-0097"
-    new_person = Person(crate, id_, {'name': 'Josiah Carberry'})
-    crate.add(new_person)
+    new_person = crate.add(Person(crate, id_, {'name': 'Josiah Carberry'}))
     person_dereference = crate.dereference(id_)
     assert person_dereference is new_person
 
@@ -104,16 +102,14 @@ def test_properties():
     crate.datePublished = crate_datePublished
     assert crate.datePublished == crate_datePublished
 
-    new_person = Person(crate, '#001', {'name': 'Lee Ritenour'})
-    crate.add(new_person)
+    new_person = crate.add(Person(crate, '#001', {'name': 'Lee Ritenour'}))
     crate.creator = new_person
     assert crate.creator is new_person
     assert isinstance(crate.creator, Person)
     assert crate.creator['name'] == 'Lee Ritenour'
     assert crate.creator.type == 'Person'
 
-    new_person2 = Person(crate, '#002', {'name': 'Lee Ritenour'})
-    crate.add(new_person2)
+    new_person2 = crate.add(Person(crate, '#002', {'name': 'Lee Ritenour'}))
     crate.creator = [new_person, new_person2]
     assert isinstance(crate.creator, list)
     assert crate.creator[0] is new_person
@@ -122,8 +118,7 @@ def test_properties():
 
 def test_uuid():
     crate = ROCrate()
-    new_person = Person(crate, properties={"name": "No Identifier"})
-    crate.add(new_person)
+    new_person = crate.add(Person(crate, properties={"name": "No Identifier"}))
     jsonld = new_person.as_jsonld()
     assert "Person" == jsonld["@type"]
     assert jsonld["@id"].startswith("#")
diff --git a/test/test_test_metadata.py b/test/test_test_metadata.py
index 6237999..b468b5e 100644
--- a/test/test_test_metadata.py
+++ b/test/test_test_metadata.py
@@ -123,8 +123,8 @@ def test_create():
 
     test_definition = TestDefinition(crate, dest_path="test/foo/bar.yml")
     crate.add(test_definition)
-    test_definition.conformsTo = test_service
-    assert test_definition.conformsTo is test_service
+    test_definition.conformsTo = test_engine
+    assert test_definition.conformsTo is test_engine
     test_definition.conformsTo = None
     test_definition.engine = test_engine
     test_definition.engineVersion = ">=0.70"