2
2
from abc import abstractmethod
3
3
from typing import Type , List , Mapping , Optional , Union
4
4
from uuid import UUID
5
+ from warnings import warn
5
6
6
7
from citrine ._serialization import properties
7
8
from citrine ._serialization .polymorphic_serializable import PolymorphicSerializable
@@ -30,15 +31,16 @@ def __eq__(self, other):
30
31
else :
31
32
return False
32
33
34
+ @classmethod
35
+ def _subclass_list (self ) -> List [Type [Serializable ]]:
36
+ return [CSVDataSource , GemTableDataSource , ExperimentDataSourceRef , SnapshotDataSource ]
37
+
33
38
@classmethod
34
39
def get_type (cls , data ) -> Type [Serializable ]:
35
40
"""Return the subtype."""
36
41
if "type" not in data :
37
42
raise ValueError ("Can only get types from dicts with a 'type' key" )
38
- types : List [Type [Serializable ]] = [
39
- CSVDataSource , GemTableDataSource , ExperimentDataSourceRef , SnapshotDataSource
40
- ]
41
- res = next ((x for x in types if x .typ == data ["type" ]), None )
43
+ res = next ((x for x in cls ._subclass_list () if x .typ == data ["type" ]), None )
42
44
if res is None :
43
45
raise ValueError ("Unrecognized type: {}" .format (data ["type" ]))
44
46
return res
@@ -52,10 +54,7 @@ def _data_source_type(self) -> str:
52
54
def from_data_source_id (cls , data_source_id : str ) -> "DataSource" :
53
55
"""Build a DataSource from a datasource_id."""
54
56
terms = data_source_id .split ("::" )
55
- types : List [Type [Serializable ]] = [
56
- CSVDataSource , GemTableDataSource , ExperimentDataSourceRef , SnapshotDataSource
57
- ]
58
- res = next ((x for x in types if x ._data_source_type == terms [0 ]), None )
57
+ res = next ((x for x in cls ._subclass_list () if x ._data_source_type == terms [0 ]), None )
59
58
if res is None :
60
59
raise ValueError ("Unrecognized type: {}" .format (terms [0 ]))
61
60
return res ._data_source_id_builder (* terms [1 :])
@@ -107,16 +106,17 @@ def __init__(self,
107
106
@classmethod
108
107
def _data_source_id_builder (cls , * args ) -> DataSource :
109
108
# TODO Figure out how to populate the column definitions
109
+ warn ("A CSVDataSource was derived from a data_source_id "
110
+ "but is missing its column_definitions and identities" ,
111
+ UserWarning )
110
112
return CSVDataSource (
111
113
file_link = FileLink (url = args [0 ], filename = args [1 ]),
112
114
column_definitions = {}
113
115
)
114
116
115
117
def to_data_source_id (self ) -> str :
116
118
"""Generate the data_source_id for this DataSource."""
117
- return "::" .join (
118
- str (x ) for x in [self ._data_source_type , self .file_link .url , self .file_link .filename ]
119
- )
119
+ return f"{ self ._data_source_type } ::{ self .file_link .url } ::{ self .file_link .filename } "
120
120
121
121
122
122
class GemTableDataSource (Serializable ['GemTableDataSource' ], DataSource ):
@@ -151,9 +151,7 @@ def _data_source_id_builder(cls, *args) -> DataSource:
151
151
152
152
def to_data_source_id (self ) -> str :
153
153
"""Generate the data_source_id for this DataSource."""
154
- return "::" .join (
155
- str (x ) for x in [self ._data_source_type , self .table_id , self .table_version ]
156
- )
154
+ return f"{ self ._data_source_type } ::{ self .table_id } ::{ self .table_version } "
157
155
158
156
@classmethod
159
157
def from_gemtable (cls , table : GemTable ) -> "GemTableDataSource" :
@@ -192,7 +190,7 @@ def _data_source_id_builder(cls, *args) -> DataSource:
192
190
193
191
def to_data_source_id (self ) -> str :
194
192
"""Generate the data_source_id for this DataSource."""
195
- return "::" . join ( str ( x ) for x in [ self ._data_source_type , self .datasource_id ])
193
+ return f" { self ._data_source_type } :: { self .datasource_id } "
196
194
197
195
198
196
class SnapshotDataSource (Serializable ['SnapshotDataSource' ], DataSource ):
@@ -219,4 +217,4 @@ def _data_source_id_builder(cls, *args) -> DataSource:
219
217
220
218
def to_data_source_id (self ) -> str :
221
219
"""Generate the data_source_id for this DataSource."""
222
- return "::" . join ( str ( x ) for x in [ self ._data_source_type , self .snapshot_id ])
220
+ return f" { self ._data_source_type } :: { self .snapshot_id } "
0 commit comments