@@ -106,30 +106,42 @@ def get_statement(self):
106106 def get_entry (self ):
107107 return self .hostDataverse .connection .swordConnection .get_resource (self .editUri ).content
108108
109+ def get_file (self , file_name ):
110+ for f in self .get_files ():
111+ if file_name == f .name :
112+ return f
113+
109114 def get_files (self ):
110- atomXml = self .get_entry ()
111- #print atomXml
112- statementLink = get_elements (atomXml ,
113- tag = "link" ,
114- attribute = "rel" ,
115- attributeValue = "http://purl.org/net/sword/terms/statement" ,
116- numberOfElements = 1 )
117- studyStatementLink = statementLink .get ("href" )
115+ if not self .statementUri :
116+ atomXml = self . get_entry ()
117+ statementLink = get_elements (atomXml ,
118+ tag = "link" ,
119+ attribute = "rel" ,
120+ attributeValue = "http://purl.org/net/sword/terms/statement" ,
121+ numberOfElements = 1 )
122+ self . statementUri = statementLink .get ("href" )
118123
119- atomStatement = self .hostDataverse .connection .swordConnection .get_atom_sword_statement (studyStatementLink )
124+ atomStatement = self .hostDataverse .connection .swordConnection .get_atom_sword_statement (self . statementUri )
120125
121126 return [DvnFile .CreateFromAtomStatementObject (res , self ) for res in atomStatement .resources ]
122- # files = []
123- # for res in atomStatement.resources:
124- # f = DvnFile.CreateFromAtomStatementObject(res, self)
125- # files.append(f)
126- #
127- # return files
128127
129- def add_file (self , file ):
130- self .add_files ([file ])
128+ def add_file (self , filepath ):
129+ self .add_files ([filepath ])
131130
132131 def add_files (self , filepaths ):
132+ # convert a directory to a list of files
133+ if len (filepaths ) == 1 and os .path .isdir (filepaths [0 ]):
134+ path = os .path .normpath (filepaths .pop ()) + os .sep
135+ for filename in os .listdir (path ):
136+ filepaths .append (path + filename )
137+ print filepaths
138+
139+ # Todo: Handle file versions
140+ for filepath in filepaths :
141+ filename = os .path .basename (filepath )
142+ if filename in [f .name for f in self .get_files ()]:
143+ raise ValueError ('The file {} already exists on the DataVerse' .format (filename ))
144+
133145 print "Uploading files: " , filepaths
134146
135147 deleteAfterUpload = False
@@ -144,7 +156,7 @@ def add_files(self, filepaths):
144156 # todo no need to guess: it's a zip!
145157 fileMimetype = mimetypes .guess_type (filepath , strict = True )
146158 filename = os .path .basename (filepath )
147-
159+
148160 with open (filepath , "rb" ) as pkg :
149161 depositReceipt = self .hostDataverse .connection .swordConnection .append (
150162 dr = self .lastDepositReceipt ,
@@ -155,7 +167,7 @@ def add_files(self, filepaths):
155167 packaging = 'http://purl.org/net/sword/package/SimpleZip' )
156168
157169 self ._refresh (deposit_receipt = depositReceipt )
158-
170+
159171 if deleteAfterUpload :
160172 print "Deleting temporary zip file: " , filepath
161173 os .remove (filepath )
0 commit comments