From 343ad6780e9ba820f302ea4e454271aa7097f6b7 Mon Sep 17 00:00:00 2001
From: Chandrasekhar Ramakrishnan <chandrasekhar.ramakrishnan@id.ethz.ch>
Date: Wed, 5 Apr 2017 17:22:12 +0200
Subject: [PATCH] SSDM-4670: Moving functionality for creating git data sets to
 a separate module.

---
 src/python/PyBis/pybis/data_set.py | 95 ++++++++++++++++++++++++++++++
 src/python/PyBis/pybis/pybis.py    | 63 ++------------------
 2 files changed, 99 insertions(+), 59 deletions(-)
 create mode 100644 src/python/PyBis/pybis/data_set.py

diff --git a/src/python/PyBis/pybis/data_set.py b/src/python/PyBis/pybis/data_set.py
new file mode 100644
index 00000000000..b53c039ee7a
--- /dev/null
+++ b/src/python/PyBis/pybis/data_set.py
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+data_set.py
+
+Module with functions for operating on data sets.
+
+
+Created by Chandrasekhar Ramakrishnan on 2017-04-05.
+Copyright (c) 2017 Chandrasekhar Ramakrishnan. All rights reserved.
+"""
+
+
+class GitDataSetCreation(object):
+    def __init__(self, openbis):
+        """Initialize the object with an openbis reference
+        :param openbis: The openBIS API object.
+        """
+        self.openbis = openbis
+
+    def new_git_data_set(self, data_set_type, path, commit_id, dms, sample=None, properties={},
+                         dss_code=None, parents=None, data_set_code=None):
+        """ Create a link data set.
+        :param data_set_type: The type of the data set
+        :param data_set_type: The type of the data set
+        :param path: The path to the git repository
+        :param commit_id: The git commit id
+        :param dms: An external data managment system object or external_dms_id
+        :param sample: A sample object or sample id.
+        :param dss_code: Code for the DSS -- defaults to the first dss if none is supplied.
+        :param properties: Properties for the data set.
+        :param parents: Parents for the data set.
+        :param data_set_code: A data set code -- used if provided, otherwise generated on the server
+        :return: A DataSet object
+        """
+
+        if dss_code is None:
+            dss_code = self.openbis.get_datastores()['code'][0]
+
+        # if a sample identifier was given, use it as a string.
+        # if a sample object was given, take its identifier
+        sample_id = self.openbis.sample_to_sample_id(sample)
+        dms_id = self.openbis.external_data_managment_system_to_dms_id(dms)
+
+        parentIds = []
+        if parents is not None:
+            if not isinstance(parents, list):
+                parents = [parents]
+            parentIds = [self.openbis.data_set_to_data_set_id(parent) for parent in parents]
+
+        data_set_creation = {
+            "linkedData": {
+                "@type": "as.dto.dataset.create.LinkedDataCreation",
+                "contentCopies": [
+                    {
+                        "@type": "as.dto.dataset.create.ContentCopyCreation",
+                        "path": path,
+                        "gitCommitHash": commit_id,
+                        "externalDmsId": dms_id
+                    }
+                ]
+            },
+            "typeId": {
+                "@type": "as.dto.entitytype.id.EntityTypePermId",
+                "permId": data_set_type
+            },
+            "sampleId": sample_id,
+            "dataStoreId": {
+                "permId": dss_code,
+                "@type": "as.dto.datastore.id.DataStorePermId"
+            },
+            "parentIds": parentIds,
+            "measured": False,
+            "properties": properties,
+            "@type": "as.dto.dataset.create.DataSetCreation"
+        }
+        if data_set_code is not None:
+            data_set_creation['code'] = data_set_code
+            data_set_creation["autoGeneratedCode"] = False
+        else:
+            data_set_creation["autoGeneratedCode"] = True
+
+        # register the files in openBIS
+        request = {
+            "method": "createDataSets",
+            "params": [
+                self.openbis.token,
+                [data_set_creation]
+            ]
+        }
+
+        # noinspection PyProtectedMember
+        resp = self.openbis._post_request(self.openbis.as_v3, request)
+        return self.openbis.get_dataset(resp[0]['permId'])
diff --git a/src/python/PyBis/pybis/pybis.py b/src/python/PyBis/pybis/pybis.py
index 530a01578ce..7fead8c7f89 100644
--- a/src/python/PyBis/pybis/pybis.py
+++ b/src/python/PyBis/pybis/pybis.py
@@ -30,6 +30,7 @@ from tabulate import tabulate
 from pybis.utils import parse_jackson, check_datatype, split_identifier, format_timestamp, is_identifier, is_permid, nvl
 from pybis.property import PropertyHolder, PropertyAssignments
 from pybis.masterdata import Vocabulary
+from . import data_set as pbds
 
 import pandas as pd
 from pandas import DataFrame, Series
@@ -1760,70 +1761,14 @@ class Openbis:
         :param commit_id: The git commit id
         :param dms: An external data managment system object or external_dms_id
         :param sample: A sample object or sample id.
-        :param dss_code: Code for the DSS -- defaults to the first dss if noe is supplied.
+        :param dss_code: Code for the DSS -- defaults to the first dss if none is supplied.
         :param properties: Properties for the data set.
         :param parents: Parents for the data set.
         :param data_set_code: A data set code -- used if provided, otherwise generated on the server
         :return: A DataSet object
         """
-
-        if dss_code is None:
-            dss_code = self.get_datastores()['code'][0]
-
-        # if a sample identifier was given, use it as a string.
-        # if a sample object was given, take its identifier
-        sample_id = self.sample_to_sample_id(sample)
-        dms_id = self.external_data_managment_system_to_dms_id(dms)
-
-        parentIds = []
-        if parents is not None:
-            if not isinstance(parents, list):
-                parents = [parents]
-            parentIds = [self.data_set_to_data_set_id(parent) for parent in parents]
-
-        data_set_creation = {
-            "linkedData": {
-                "@type": "as.dto.dataset.create.LinkedDataCreation",
-                "contentCopies": [
-                    {
-                        "@type": "as.dto.dataset.create.ContentCopyCreation",
-                        "path": path,
-                        "gitCommitHash": commit_id,
-                        "externalDmsId": dms_id
-                    }
-                ]
-            },
-            "typeId": {
-                "@type": "as.dto.entitytype.id.EntityTypePermId",
-                "permId": data_set_type
-            },
-            "sampleId": sample_id,
-            "dataStoreId": {
-                "permId": dss_code,
-                "@type": "as.dto.datastore.id.DataStorePermId"
-            },
-            "parentIds": parentIds,
-            "measured": False,
-            "properties": properties,
-            "@type": "as.dto.dataset.create.DataSetCreation"
-        }
-        if data_set_code is not None:
-            data_set_creation['code'] = data_set_code
-            data_set_creation["autoGeneratedCode"] = False
-        else:
-            data_set_creation["autoGeneratedCode"] = True
-
-        # register the files in openBIS
-        request = {
-            "method": "createDataSets",
-            "params": [
-                self.token,
-                [data_set_creation]
-            ]
-        }
-
-        resp = self._post_request(self.as_v3, request)
-        return self.get_dataset(resp[0]['permId'])
+        return pbds.GitDataSetCreation(self).new_git_data_set(data_set_type, path, commit_id, dms, sample,
+                                                              properties, dss_code, parents, data_set_code)
 
     @staticmethod
     def sample_to_sample_id(sample):
-- 
GitLab