1
+ from office365 .runtime .client_result import ClientResult
2
+ from office365 .runtime .queries .service_operation_query import ServiceOperationQuery
3
+ from office365 .runtime .resource_path import ResourcePath
1
4
from office365 .runtime .resource_path_service_operation import ResourcePathServiceOperation
2
5
from office365 .sharepoint .base_entity import BaseEntity
3
6
4
7
5
8
class FileVersion (BaseEntity ):
6
9
"""Represents a version of a File object."""
7
10
11
+ def download (self , file_object ):
12
+ """Downloads the file version as a stream and save into a file.
13
+
14
+ :type file_object: typing.IO
15
+ """
16
+ def _file_version_loaded ():
17
+ result = self .open_binary_stream ()
18
+
19
+ def _process_response (response ):
20
+ """
21
+ :type response: requests.Response
22
+ """
23
+ response .raise_for_status ()
24
+ file_object .write (result .value )
25
+ self .context .after_execute (_process_response )
26
+ self .ensure_property ("ID" , _file_version_loaded )
27
+ return self
28
+
29
+ def open_binary_stream (self ):
30
+ """Opens the file as a stream."""
31
+ return_stream = ClientResult (self .context )
32
+ qry = ServiceOperationQuery (self , "OpenBinaryStream" , None , None , None , return_stream )
33
+ self .context .add_query (qry )
34
+ return return_stream
35
+
36
+ def open_binary_stream_with_options (self , open_options ):
37
+ """Opens the file as a stream."""
38
+ return_stream = ClientResult (self .context )
39
+ qry = ServiceOperationQuery (self , "OpenBinaryStreamWithOptions" , [open_options ], None , None , return_stream )
40
+ self .context .add_query (qry )
41
+ return return_stream
42
+
43
+ @property
44
+ def created_by (self ):
45
+ """Gets the user that created the file version."""
46
+ from office365 .sharepoint .principal .user import User
47
+ return self .properties .get ("CreatedBy" , User (self .context , ResourcePath ("CreatedBy" , self .resource_path )))
48
+
49
+ @property
50
+ def id (self ):
51
+ """Gets a file version identifier"""
52
+ return int (self .properties .get ("ID" , - 1 ))
53
+
8
54
@property
9
55
def url (self ):
10
56
"""Gets a value that specifies the relative URL of the file version based on the URL for the site or subsite."""
@@ -25,11 +71,18 @@ def checkin_comment(self):
25
71
"""Gets a value that specifies the check-in comment."""
26
72
return self .properties .get ("CheckInComment" , None )
27
73
74
+ def get_property (self , name , default_value = None ):
75
+ if default_value is None :
76
+ property_mapping = {
77
+ "CreatedBy" : self .created_by ,
78
+ }
79
+ default_value = property_mapping .get (name , None )
80
+ return super (FileVersion , self ).get_property (name , default_value )
81
+
28
82
def set_property (self , name , value , persist_changes = True ):
29
83
super (FileVersion , self ).set_property (name , value , persist_changes )
30
84
if self ._resource_path is None :
31
85
if name == "ID" :
32
86
self ._resource_path = ResourcePathServiceOperation (
33
- "GetById" ,
34
- [value ],
35
- self ._parent_collection .resource_path )
87
+ "GetById" , [value ], self ._parent_collection .resource_path )
88
+ return self
0 commit comments