5
5
from .resource_tagger import _ResourceTagger
6
6
from .. import RequestFactory , WorkbookItem , ConnectionItem , ViewItem , PaginationItem
7
7
from ...models .job_item import JobItem
8
- from ...filesys_helpers import to_filename , make_download_path
8
+ from ...filesys_helpers import to_filename , make_download_path , file_is_compressed , get_file_object_size
9
9
10
10
import os
11
11
import logging
@@ -254,7 +254,7 @@ def delete_permission(self, item, capability_item):
254
254
@parameter_added_in (as_job = '3.0' )
255
255
@parameter_added_in (connections = '2.8' )
256
256
def publish (
257
- self , workbook_item , file_path , mode ,
257
+ self , workbook_item , file , mode ,
258
258
connection_credentials = None , connections = None , as_job = False ,
259
259
hidden_views = None
260
260
):
@@ -264,23 +264,40 @@ def publish(
264
264
warnings .warn ("connection_credentials is being deprecated. Use connections instead" ,
265
265
DeprecationWarning )
266
266
267
- if not os .path .isfile (file_path ):
268
- error = "File path does not lead to an existing file."
269
- raise IOError (error )
267
+ try :
268
+ # Expect file to be a filepath
269
+ if not os .path .isfile (file ):
270
+ error = "File path does not lead to an existing file."
271
+ raise IOError (error )
272
+
273
+ filename = os .path .basename (file )
274
+ file_extension = os .path .splitext (filename )[1 ][1 :]
275
+ file_size = os .path .getsize (file )
276
+
277
+ # If name is not defined, grab the name from the file to publish
278
+ if not workbook_item .name :
279
+ workbook_item .name = os .path .splitext (filename )[0 ]
280
+ if file_extension not in ALLOWED_FILE_EXTENSIONS :
281
+ error = "Only {} files can be published as workbooks." .format (', ' .join (ALLOWED_FILE_EXTENSIONS ))
282
+ raise ValueError (error )
283
+
284
+ except TypeError :
285
+ # Expect file to be a file object
286
+ file_size = get_file_object_size (file )
287
+ file_extension = 'twbx' if file_is_compressed (file ) else 'twb'
288
+
289
+ if not workbook_item .name :
290
+ error = "Workbook item must have a name when passing a file object"
291
+ raise ValueError (error )
292
+
293
+ # Generate filename for file object.
294
+ # This is needed when publishing the workbook in a single request
295
+ filename = "{}.{}" .format (workbook_item .name , file_extension )
296
+
270
297
if not hasattr (self .parent_srv .PublishMode , mode ):
271
298
error = 'Invalid mode defined.'
272
299
raise ValueError (error )
273
300
274
- filename = os .path .basename (file_path )
275
- file_extension = os .path .splitext (filename )[1 ][1 :]
276
-
277
- # If name is not defined, grab the name from the file to publish
278
- if not workbook_item .name :
279
- workbook_item .name = os .path .splitext (filename )[0 ]
280
- if file_extension not in ALLOWED_FILE_EXTENSIONS :
281
- error = "Only {} files can be published as workbooks." .format (', ' .join (ALLOWED_FILE_EXTENSIONS ))
282
- raise ValueError (error )
283
-
284
301
# Construct the url with the defined mode
285
302
url = "{0}?workbookType={1}" .format (self .baseurl , file_extension )
286
303
if mode == self .parent_srv .PublishMode .Overwrite :
@@ -293,9 +310,9 @@ def publish(
293
310
url += '&{0}=true' .format ('asJob' )
294
311
295
312
# Determine if chunking is required (64MB is the limit for single upload method)
296
- if os . path . getsize ( file_path ) >= FILESIZE_LIMIT :
297
- logger .info ('Publishing {0} to server with chunking method (workbook over 64MB)' .format (filename ))
298
- upload_session_id = Fileuploads .upload_chunks (self .parent_srv , file_path )
313
+ if file_size >= FILESIZE_LIMIT :
314
+ logger .info ('Publishing {0} to server with chunking method (workbook over 64MB)' .format (workbook_item . name ))
315
+ upload_session_id = Fileuploads .upload_chunks (self .parent_srv , file )
299
316
url = "{0}&uploadSessionId={1}" .format (url , upload_session_id )
300
317
conn_creds = connection_credentials
301
318
xml_request , content_type = RequestFactory .Workbook .publish_req_chunked (workbook_item ,
@@ -304,8 +321,14 @@ def publish(
304
321
hidden_views = hidden_views )
305
322
else :
306
323
logger .info ('Publishing {0} to server' .format (filename ))
307
- with open (file_path , 'rb' ) as f :
308
- file_contents = f .read ()
324
+
325
+ try :
326
+ with open (file , 'rb' ) as f :
327
+ file_contents = f .read ()
328
+
329
+ except TypeError :
330
+ file_contents = file .read ()
331
+
309
332
conn_creds = connection_credentials
310
333
xml_request , content_type = RequestFactory .Workbook .publish_req (workbook_item ,
311
334
filename ,
@@ -325,9 +348,9 @@ def publish(
325
348
326
349
if as_job :
327
350
new_job = JobItem .from_response (server_response .content , self .parent_srv .namespace )[0 ]
328
- logger .info ('Published {0} (JOB_ID: {1}' .format (filename , new_job .id ))
351
+ logger .info ('Published {0} (JOB_ID: {1}' .format (workbook_item . name , new_job .id ))
329
352
return new_job
330
353
else :
331
354
new_workbook = WorkbookItem .from_response (server_response .content , self .parent_srv .namespace )[0 ]
332
- logger .info ('Published {0} (ID: {1})' .format (filename , new_workbook .id ))
355
+ logger .info ('Published {0} (ID: {1})' .format (workbook_item . name , new_workbook .id ))
333
356
return new_workbook
0 commit comments