1717# limitations under the License.
1818"""Model objects representing datasets."""
1919
20- import configparser
2120import datetime
2221import os
2322import pathlib
3029import attr
3130from attr .validators import instance_of
3231
33- from renku .core import errors
3432from renku .core .models .provenance .agents import Person
3533from renku .core .models .provenance .entities import Entity
3634from renku .core .utils .datetime8601 import parse_date
@@ -54,69 +52,24 @@ def _extract_doi(value):
5452 return value
5553
5654
57- class Creator (Person ):
58- """Represent the creator of a resource."""
59-
60- affiliation = jsonld .ib (
61- default = None , kw_only = True , context = 'schema:affiliation'
62- )
63-
64- alternate_name = jsonld .ib (
65- default = None , kw_only = True , context = 'schema:alternateName'
66- )
67-
68- @property
69- def short_name (self ):
70- """Gives full name in short form."""
71- names = self .name .split ()
72- if len (names ) == 1 :
73- return self .name
74-
75- last_name = names [- 1 ]
76- initials = [name [0 ] for name in names ]
77- initials .pop ()
78-
79- return '{0}.{1}' .format ('.' .join (initials ), last_name )
80-
81- @classmethod
82- def from_git (cls , git ):
83- """Create an instance from a Git repo."""
84- git_config = git .config_reader ()
85- try :
86- name = git_config .get_value ('user' , 'name' , None )
87- email = git_config .get_value ('user' , 'email' , None )
88- except (
89- configparser .NoOptionError , configparser .NoSectionError
90- ): # pragma: no cover
91- raise errors .ConfigurationError (
92- 'The user name and email are not configured. '
93- 'Please use the "git config" command to configure them.\n \n '
94- '\t git config --global --add user.name "John Doe"\n '
95- '\t git config --global --add user.email '
96- '"john.doe@example.com"\n '
97- )
98-
99- # Check the git configuration.
100- if not name : # pragma: no cover
101- raise errors .MissingUsername ()
102- if not email : # pragma: no cover
103- raise errors .MissingEmail ()
104-
105- return cls (name = name , email = email )
55+ def _convert_creators (value ):
56+ """Convert creators."""
57+ if isinstance (value , dict ): # compatibility with previous versions
58+ return [Person .from_jsonld (value )]
10659
107- def __attrs_post_init__ (self ):
108- """Finish object initialization."""
109- # handle the case where ids were improperly set
110- if self ._id == 'mailto:None' :
111- self ._id = self .default_id ()
60+ if isinstance (value , list ):
61+ return [Person .from_jsonld (v ) for v in value ]
11262
11363
11464@attr .s
115- class CreatorsMixin :
65+ class PersonMixin :
11666 """Mixin for handling creators container."""
11767
11868 creator = jsonld .container .list (
119- Creator , kw_only = True , context = 'schema:creator'
69+ Person ,
70+ kw_only = True ,
71+ context = 'schema:creator' ,
72+ converter = _convert_creators
12073 )
12174
12275 @property
@@ -190,34 +143,16 @@ class Language:
190143 name = jsonld .ib (default = None , kw_only = True , context = 'schema:name' )
191144
192145
193- def _convert_dataset_files_creators (value ):
194- """Convert dataset files creators."""
195- coll = value
196-
197- if isinstance (coll , dict ):
198- return [Creator .from_jsonld (coll )]
199-
200- if isinstance (coll , list ):
201- return [Creator .from_jsonld (c ) for c in coll ]
202-
203-
204146@jsonld .s (
205147 type = 'schema:DigitalDocument' ,
206148 slots = True ,
207149 context = {
208150 'schema' : 'http://schema.org/' ,
209151 }
210152)
211- class DatasetFile (Entity , CreatorsMixin ):
153+ class DatasetFile (Entity , PersonMixin ):
212154 """Represent a file in a dataset."""
213155
214- creator = jsonld .container .list (
215- Creator ,
216- converter = _convert_dataset_files_creators ,
217- kw_only = True ,
218- context = 'schema:creator'
219- )
220-
221156 added = jsonld .ib (
222157 converter = parse_date , context = 'schema:dateCreated' , kw_only = True
223158 )
@@ -290,15 +225,6 @@ def _convert_dataset_tags(value):
290225 return [DatasetTag .from_jsonld (v ) for v in value ]
291226
292227
293- def _convert_dataset_creator (value ):
294- """Convert dataset creators."""
295- if isinstance (value , dict ): # compatibility with previous versions
296- return [Creator .from_jsonld (value )]
297-
298- if isinstance (value , list ):
299- return [Creator .from_jsonld (v ) for v in value ]
300-
301-
302228def _convert_language (obj ):
303229 """Convert language object."""
304230 if isinstance (obj , dict ):
@@ -324,7 +250,7 @@ def _convert_keyword(keywords):
324250 'schema' : 'http://schema.org/' ,
325251 },
326252)
327- class Dataset (Entity , CreatorsMixin ):
253+ class Dataset (Entity , PersonMixin ):
328254 """Repesent a dataset."""
329255
330256 SUPPORTED_SCHEMES = ('' , 'file' , 'http' , 'https' , 'git+https' , 'git+ssh' )
@@ -337,13 +263,6 @@ class Dataset(Entity, CreatorsMixin):
337263 _id = jsonld .ib (default = None , context = '@id' , kw_only = True )
338264 _label = jsonld .ib (default = None , context = 'rdfs:label' , kw_only = True )
339265
340- creator = jsonld .container .list (
341- Creator ,
342- converter = _convert_dataset_creator ,
343- context = 'schema:creator' ,
344- kw_only = True
345- )
346-
347266 date_published = jsonld .ib (
348267 default = None , context = 'schema:datePublished' , kw_only = True
349268 )
0 commit comments