1717# local modules
1818from file import DvnFile
1919import utils
20-
20+ from utils import format_term
2121
2222class Study (object ):
23- def __init__ (self , title , id = None , author = None , producer = None , date = None , description = None , abstract = None ,
24- type = None , source = None , restriction = None , relation = None , keyword = None , coverage = None ,
25- publication = None , editUri = None , editMediaUri = None , statementUri = None , hostDataverse = None ,
26- atomEntryXml = None ):
27- # Create SWORD Entry with Metadata for study
28- if atomEntryXml :
29- self .entry = sword2 .Entry (atomEntryXml = atomEntryXml )
30- else :
31- self .entry = sword2 .Entry (
32- dcterms_title = title ,
33- dcterms_identifier = id ,
34- dcterms_creator = author ,
35- dcterms_publisher = producer ,
36- dcterms_date = date ,
37- dcterms_description = description ,
38- dcterms_abstract = abstract ,
39- dcterms_type = type ,
40- dcterms_source = source ,
41- dcterms_rights = restriction ,
42- dcterms_relation = relation ,
43- dcterms_subject = keyword ,
44- dcterms_coverage = coverage ,
45- dcterms_isReferencedBy = publication ,
46- )
23+ def __init__ (self , * args , ** kwargs ):
4724
48- # TODO: Add list functionality to passed parameters
49- # self.entry.add_fields(dcterms_subject="KEYWORD2") # how to define additional fields
25+ # adds dict to keyword arguments
26+ kwargs = dict ( args [ 0 ]. items () + kwargs . items ()) if args and isinstance ( args [ 0 ], dict ) else kwargs
5027
51- # deposit receipt is added when Dataverse.addStudy() is called on this study
52- self .lastDepositReceipt = None
53-
54- self .editUri = editUri
55- self .editMediaUri = editMediaUri
56- self .statementUri = statementUri
28+ # deposit receipt is added when Dataverse.addStudy() is called on this study
29+ self .lastDepositReceipt = None
30+
31+ # sets fields from kwargs
32+ self .editUri = kwargs .pop ('editUri' ) if 'editUri' in kwargs .keys () else None
33+ self .editMediaUri = kwargs .pop ('editMediaUri' ) if 'editMediaUri' in kwargs .keys () else None
34+ self .statementUri = kwargs .pop ('statementUri' ) if 'statementUri' in kwargs .keys () else None
35+
36+ # todo: add self.exists_on_dataverse / self.created
37+ self .hostDataverse = kwargs .pop ('hostDataverse' ) if 'hostDataverse' in kwargs .keys () else None
38+
39+ # creates sword entry from xml
40+ if args and not isinstance (args [0 ], dict ):
41+ with open (args [0 ]) as f :
42+ xml = f .read ()
43+ self .entry = sword2 .Entry (xml )
44+
45+ # creates sword entry from keyword arguments
46+ if kwargs :
47+ if 'title' not in kwargs .keys () or isinstance (kwargs .get ('title' ), list ):
48+ raise Exception ('Study needs a single, valid title.' )
49+
50+ self .entry = sword2 .Entry ()
5751
58- # todo: add self.exists_on_dataverse / self.created
59- self .hostDataverse = hostDataverse # generally used for sword connection
52+ for k in kwargs .keys ():
53+ if isinstance (kwargs [k ], list ):
54+ for item in kwargs [k ]:
55+ self .entry .add_field (format_term (k ), item )
56+ else :
57+ self .entry .add_field (format_term (k ), kwargs [k ])
6058
6159 def __repr__ (self ):
6260 studyObject = pprint .pformat (self .__dict__ )
@@ -68,42 +66,6 @@ def __repr__(self):
6866 entry=
6967{eo}
7068/STUDY ========= """ .format (so = studyObject ,eo = entryObject )
71-
72- @classmethod
73- def from_dict (cls , dict ):
74- return cls (title = dict ["title" ],
75- id = dict ["id" ],
76- author = dict ["author" ],
77- producer = dict ["producer" ],
78- date = dict ["date" ],
79- description = dict ["description" ],
80- abstract = dict ["abstract" ],
81- type = dict ["type" ],
82- source = dict ["source" ],
83- restriction = dict ["restriction" ],
84- relation = dict ["relation" ],
85- keyword = dict ["keyword" ],
86- coverage = dict ["coverage" ],
87- publication = dict ["publication" ],
88- )
89-
90- @classmethod
91- def from_atom_xml_file (cls , xmlFilePath ):
92- with open (xmlFilePath ) as f :
93- xml = f .read ()
94- study = cls .from_atom_xml_string (xml = xml )
95-
96- return study
97-
98- @classmethod
99- def from_atom_xml_string (cls , xml ):
100- title = utils .get_elements (xml ,
101- namespace = "http://purl.org/dc/terms/" ,
102- tag = "title" ,
103- numberOfElements = 1 ,
104- ).text
105-
106- return cls (title , atomEntryXml = xml )
10769
10870 @classmethod
10971 def from_entry_element (cls , entryElement , hostDataverse = None ):
0 commit comments