Repository: ResearchObject/ro-crate-py (python)

Issue:
Duplicate preview entry in output crate
```python
import shutil
from rocrate.rocrate import ROCrate

shutil.rmtree("/tmp/out_crate", ignore_errors=True)
crate = ROCrate("test/test-data/read_crate")
crate.write("/tmp/out_crate")
```

The output crate has:

```javascript
        {
            "@id": "ro-crate-preview.html",
            "@type": "CreativeWork",
            "about": {
                "@id": "./"
            }
        },
```

but also:

```javascript
        {
            "@id": "#ro-crate-preview.html",
            "@type": "CreativeWork",
            "about": {
                "@id": "./"
            }
        },
```

Pull request:
Fix duplicate metadata entry for preview
Fixes #92.

Patch:
diff --git a/rocrate/model/preview.py b/rocrate/model/preview.py
index 553eacc..03013b9 100644
--- a/rocrate/model/preview.py
+++ b/rocrate/model/preview.py
@@ -32,8 +32,8 @@ class Preview(File):
     """
     BASENAME = "ro-crate-preview.html"
 
-    def __init__(self, crate, source=None):
-        super().__init__(crate, source, self.BASENAME, None)
+    def __init__(self, crate, source=None, properties=None):
+        super().__init__(crate, source, self.BASENAME, properties=properties)
 
     def _empty(self):
         # default properties of the metadata entry
diff --git a/rocrate/rocrate.py b/rocrate/rocrate.py
index 8e3564e..af7a1cb 100644
--- a/rocrate/rocrate.py
+++ b/rocrate/rocrate.py
@@ -180,8 +180,9 @@ class ROCrate():
         assert root_id == root_entity.pop('@id')
         parts = root_entity.pop('hasPart', [])
         self.add(RootDataset(self, properties=root_entity))
-        if not gen_preview and Preview.BASENAME in entities:
-            self.add(Preview(self, source / Preview.BASENAME))
+        preview_entity = entities.pop(Preview.BASENAME, None)
+        if preview_entity and not gen_preview:
+            self.add(Preview(self, source / Preview.BASENAME, properties=preview_entity))
         type_map = OrderedDict((_.__name__, _) for _ in subclasses(FileOrDir))
         for data_entity_ref in parts:
             id_ = data_entity_ref['@id']


Test patch:
diff --git a/test/test_read.py b/test/test_read.py
index 8db4036..ac173f1 100644
--- a/test/test_read.py
+++ b/test/test_read.py
@@ -33,7 +33,6 @@ _URL = ('https://raw.githubusercontent.com/ResearchObject/ro-crate-py/master/'
 
 @pytest.mark.parametrize("gen_preview,from_zip", [(False, False), (True, False), (True, True)])
 def test_crate_dir_loading(test_data_dir, tmpdir, helpers, gen_preview, from_zip):
-    # load crate
     crate_dir = test_data_dir / 'read_crate'
     if from_zip:
         zip_source = shutil.make_archive(tmpdir / "read_crate.crate", "zip", crate_dir)
@@ -41,7 +40,21 @@ def test_crate_dir_loading(test_data_dir, tmpdir, helpers, gen_preview, from_zip
     else:
         crate = ROCrate(crate_dir, gen_preview=gen_preview)
 
-    # check loaded entities and properties
+    assert set(_["@id"] for _ in crate.default_entities) == {
+        "./",
+        "ro-crate-metadata.json",
+        "ro-crate-preview.html"
+    }
+    assert set(_["@id"] for _ in crate.data_entities) == {
+        "test_galaxy_wf.ga",
+        "abstract_wf.cwl",
+        "test_file_galaxy.txt",
+        "https://raw.githubusercontent.com/ResearchObject/ro-crate-py/master/test/test-data/sample_file.txt",
+        "examples/",
+        "test/",
+    }
+    assert set(_["@id"] for _ in crate.contextual_entities) == {"#joe"}
+
     root = crate.dereference('./')
     assert crate.root_dataset is root
     root_prop = root.properties()