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
14from office365 .runtime .resource_path_service_operation import ResourcePathServiceOperation
25from office365 .sharepoint .base_entity import BaseEntity
36
47
58class FileVersion (BaseEntity ):
69 """Represents a version of a File object."""
710
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+
854 @property
955 def url (self ):
1056 """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):
2571 """Gets a value that specifies the check-in comment."""
2672 return self .properties .get ("CheckInComment" , None )
2773
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+
2882 def set_property (self , name , value , persist_changes = True ):
2983 super (FileVersion , self ).set_property (name , value , persist_changes )
3084 if self ._resource_path is None :
3185 if name == "ID" :
3286 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