1# -------------------------------------------------------------------------
2# Copyright (c) Microsoft Corporation. All rights reserved.
3# Licensed under the MIT License. See License.txt in the project root for
4# license information.
5# --------------------------------------------------------------------------
6from ..common._common_conversion import _to_str
7
8
9class Share(object):
10    '''
11    File share class.
12
13    :ivar str name:
14        The name of the share.
15    :ivar ShareProperties properties:
16        System properties for the share.
17    :ivar metadata:
18        A dict containing name-value pairs associated with the share as metadata.
19        This var is set to None unless the include=metadata param was included
20        for the list shares operation. If this parameter was specified but the
21        share has no metadata, metadata will be set to an empty dictionary.
22    :vartype metadata: dict(str, str)
23    :ivar str snapshot:
24        A DateTime value that uniquely identifies the snapshot. The value of
25        this header indicates the snapshot version, and may be used in
26        subsequent requests to access the snapshot.
27    '''
28
29    def __init__(self, name=None, props=None, metadata=None, snapshot=None):
30        self.name = name
31        self.properties = props or ShareProperties()
32        self.metadata = metadata
33        self.snapshot = snapshot
34
35
36class ShareProperties(object):
37    '''
38    File share's properties class.
39
40    :ivar datetime last_modified:
41        A datetime object representing the last time the share was modified.
42    :ivar str etag:
43        The ETag contains a value that you can use to perform operations
44        conditionally.
45    :ivar int quote:
46        Returns the current share quota in GB.
47    '''
48
49    def __init__(self):
50        self.last_modified = None
51        self.etag = None
52        self.quota = None
53
54
55class Directory(object):
56    '''
57    Directory class.
58
59    :ivar str name:
60        The name of the directory.
61    :ivar DirectoryProperties properties:
62        System properties for the directory.
63    :ivar metadata:
64        A dict containing name-value pairs associated with the directory as metadata.
65        This var is set to None unless the include=metadata param was included
66        for the list directory operation. If this parameter was specified but the
67        directory has no metadata, metadata will be set to an empty dictionary.
68    :vartype metadata: dict(str, str)
69    '''
70
71    def __init__(self, name=None, props=None, metadata=None):
72        self.name = name
73        self.properties = props or DirectoryProperties()
74        self.metadata = metadata
75
76
77class DirectoryProperties(object):
78    '''
79    File directory's properties class.
80
81    :ivar datetime last_modified:
82        A datetime object representing the last time the directory was modified.
83    :ivar str etag:
84        The ETag contains a value that you can use to perform operations
85        conditionally.
86    :ivar bool server_encrypted:
87        Set to true if the directory metadata is encrypted on the server.
88    '''
89
90    def __init__(self):
91        self.last_modified = None
92        self.etag = None
93        self.server_encrypted = None
94
95
96class File(object):
97    '''
98    File class.
99
100    :ivar str name:
101        The name of the file.
102    :ivar content:
103        File content.
104    :vartype content: str or bytes
105    :ivar FileProperties properties:
106        System properties for the file.
107    :ivar metadata:
108        A dict containing name-value pairs associated with the file as metadata.
109        This var is set to None unless the include=metadata param was included
110        for the list file operation. If this parameter was specified but the
111        file has no metadata, metadata will be set to an empty dictionary.
112    :vartype metadata: dict(str, str)
113    '''
114
115    def __init__(self, name=None, content=None, props=None, metadata=None):
116        self.name = name
117        self.content = content
118        self.properties = props or FileProperties()
119        self.metadata = metadata
120
121
122class FileProperties(object):
123    '''
124    File Properties.
125
126    :ivar datetime last_modified:
127        A datetime object representing the last time the file was modified.
128    :ivar str etag:
129        The ETag contains a value that you can use to perform operations
130        conditionally.
131    :ivar int content_length:
132        The length of the content returned. If the entire blob was requested,
133        the length of blob in bytes. If a subset of the blob was requested, the
134        length of the returned subset.
135    :ivar str content_range:
136        Indicates the range of bytes returned in the event that the client
137        requested a subset of the blob.
138    :ivar ~azure.storage.file.models.ContentSettings content_settings:
139        Stores all the content settings for the file.
140    :ivar ~azure.storage.file.models.CopyProperties copy:
141        Stores all the copy properties for the file.
142    ivar bool server_encrypted:
143        Set to true if the file data and application metadata are completely encrypted.
144    '''
145
146    def __init__(self):
147        self.last_modified = None
148        self.etag = None
149        self.content_length = None
150        self.content_range = None
151        self.content_settings = ContentSettings()
152        self.copy = CopyProperties()
153        self.server_encrypted = None
154
155
156class ContentSettings(object):
157    '''
158    Used to store the content settings of a file.
159
160    :ivar str content_type:
161        The content type specified for the file. If no content type was
162        specified, the default content type is application/octet-stream.
163    :ivar str content_encoding:
164        If content_encoding has previously been set
165        for the file, that value is stored.
166    :ivar str content_language:
167        If content_language has previously been set
168        for the file, that value is stored.
169    :ivar str content_disposition:
170        content_disposition conveys additional information about how to
171        process the response payload, and also can be used to attach
172        additional metadata. If content_disposition has previously been set
173        for the file, that value is stored.
174    :ivar str cache_control:
175        If cache_control has previously been set for
176        the file, that value is stored.
177    :ivar str content_md5:
178        If the content_md5 has been set for the file, this response
179        header is stored so that the client can check for message content
180        integrity.
181    '''
182
183    def __init__(
184            self, content_type=None, content_encoding=None,
185            content_language=None, content_disposition=None,
186            cache_control=None, content_md5=None):
187        self.content_type = content_type
188        self.content_encoding = content_encoding
189        self.content_language = content_language
190        self.content_disposition = content_disposition
191        self.cache_control = cache_control
192        self.content_md5 = content_md5
193
194    def _to_headers(self):
195        return {
196            'x-ms-cache-control': _to_str(self.cache_control),
197            'x-ms-content-type': _to_str(self.content_type),
198            'x-ms-content-disposition': _to_str(self.content_disposition),
199            'x-ms-content-md5': _to_str(self.content_md5),
200            'x-ms-content-encoding': _to_str(self.content_encoding),
201            'x-ms-content-language': _to_str(self.content_language),
202        }
203
204
205class CopyProperties(object):
206    '''
207    File Copy Properties.
208
209    :ivar str id:
210        String identifier for the last attempted Copy File operation where this file
211        was the destination file. This header does not appear if this file has never
212        been the destination in a Copy File operation, or if this file has been
213        modified after a concluded Copy File operation using Set File Properties or
214        Put File.
215    :ivar str source:
216        URL up to 2 KB in length that specifies the source file used in the last attempted
217        Copy File operation where this file was the destination file. This header does not
218        appear if this file has never been the destination in a Copy File operation, or if
219        this file has been modified after a concluded Copy File operation using
220        Set File Properties or Put File.
221    :ivar str status:
222        State of the copy operation identified by Copy ID, with these values:
223            success:
224                Copy completed successfully.
225            pending:
226                Copy is in progress. Check copy_status_description if intermittent,
227                non-fatal errors impede copy progress but don't cause failure.
228            aborted:
229                Copy was ended by Abort Copy File.
230            failed:
231                Copy failed. See copy_status_description for failure details.
232    :ivar str progress:
233        Contains the number of bytes copied and the total bytes in the source in the last
234        attempted Copy File operation where this file was the destination file. Can show
235        between 0 and Content-Length bytes copied.
236    :ivar datetime completion_time:
237        Conclusion time of the last attempted Copy File operation where this file was the
238        destination file. This value can specify the time of a completed, aborted, or
239        failed copy attempt.
240    :ivar str status_description:
241        Only appears when x-ms-copy-status is failed or pending. Describes cause of fatal
242        or non-fatal copy operation failure.
243    '''
244
245    def __init__(self):
246        self.id = None
247        self.source = None
248        self.status = None
249        self.progress = None
250        self.completion_time = None
251        self.status_description = None
252
253
254class FileRange(object):
255    '''
256    File Range.
257
258    :ivar int start:
259        Byte index for start of file range.
260    :ivar int end:
261        Byte index for end of file range.
262    '''
263
264    def __init__(self, start=None, end=None):
265        self.start = start
266        self.end = end
267
268
269class DeleteSnapshot(object):
270    '''
271    Required if the Share has associated snapshots. Specifies how to handle the snapshots.
272    '''
273
274    Include = 'include'
275    '''
276    Delete the share and all of its snapshots.
277    '''
278
279
280class FilePermissions(object):
281    '''
282    FilePermissions class to be used with
283    :func:`~azure.storage.file.fileservice.FileService.generate_file_shared_access_signature` API.
284
285    :ivar FilePermissions FilePermissions.CREATE:
286        Create a new file or copy a file to a new file.
287    :ivar FilePermissions FilePermissions.DELETE:
288        Delete the file.
289    :ivar FilePermissions FilePermissions.READ:
290        Read the content, properties, metadata. Use the file as the source of a copy
291        operation.
292    :ivar FilePermissions FilePermissions.WRITE:
293        Create or write content, properties, metadata. Resize the file. Use the file
294        as the destination of a copy operation within the same account.
295    '''
296
297    def __init__(self, read=False, create=False, write=False, delete=False,
298                 _str=None):
299        '''
300        :param bool read:
301            Read the content, properties, metadata. Use the file as the source of a copy
302            operation.
303        :param bool create:
304            Create a new file or copy a file to a new file.
305        :param bool write:
306            Create or write content, properties, metadata. Resize the file. Use the file
307            as the destination of a copy operation within the same account.
308        :param bool delete:
309            Delete the file.
310        :param str _str:
311            A string representing the permissions.
312        '''
313
314        if not _str:
315            _str = ''
316        self.read = read or ('r' in _str)
317        self.create = create or ('c' in _str)
318        self.write = write or ('w' in _str)
319        self.delete = delete or ('d' in _str)
320
321    def __or__(self, other):
322        return FilePermissions(_str=str(self) + str(other))
323
324    def __add__(self, other):
325        return FilePermissions(_str=str(self) + str(other))
326
327    def __str__(self):
328        return (('r' if self.read else '') +
329                ('c' if self.create else '') +
330                ('w' if self.write else '') +
331                ('d' if self.delete else ''))
332
333
334FilePermissions.CREATE = FilePermissions(create=True)
335FilePermissions.DELETE = FilePermissions(delete=True)
336FilePermissions.READ = FilePermissions(read=True)
337FilePermissions.WRITE = FilePermissions(write=True)
338
339
340class SharePermissions(object):
341    '''
342    SharePermissions class to be used with `azure.storage.file.FileService.generate_share_shared_access_signature`
343    method and for the AccessPolicies used with `azure.storage.file.FileService.set_share_acl`.
344
345    :ivar SharePermissions FilePermissions.DELETE:
346        Delete any file in the share.
347        Note: You cannot grant permissions to delete a share with a service SAS. Use
348        an account SAS instead.
349    :ivar SharePermissions FilePermissions.LIST:
350        List files and directories in the share.
351    :ivar SharePermissions FilePermissions.READ:
352        Read the content, properties or metadata of any file in the share. Use any
353        file in the share as the source of a copy operation.
354    :ivar SharePermissions FilePermissions.WRITE:
355        For any file in the share, create or write content, properties or metadata.
356        Resize the file. Use the file as the destination of a copy operation within
357        the same account.
358        Note: You cannot grant permissions to read or write share properties or
359        metadata with a service SAS. Use an account SAS instead.
360    '''
361
362    def __init__(self, read=False, write=False, delete=False, list=False,
363                 _str=None):
364        '''
365        :param bool read:
366            Read the content, properties or metadata of any file in the share. Use any
367            file in the share as the source of a copy operation.
368        :param bool write:
369            For any file in the share, create or write content, properties or metadata.
370            Resize the file. Use the file as the destination of a copy operation within
371            the same account.
372            Note: You cannot grant permissions to read or write share properties or
373            metadata with a service SAS. Use an account SAS instead.
374        :param bool delete:
375            Delete any file in the share.
376            Note: You cannot grant permissions to delete a share with a service SAS. Use
377            an account SAS instead.
378        :param bool list:
379            List files and directories in the share.
380        :param str _str:
381            A string representing the permissions
382        '''
383
384        if not _str:
385            _str = ''
386        self.read = read or ('r' in _str)
387        self.write = write or ('w' in _str)
388        self.delete = delete or ('d' in _str)
389        self.list = list or ('l' in _str)
390
391    def __or__(self, other):
392        return SharePermissions(_str=str(self) + str(other))
393
394    def __add__(self, other):
395        return SharePermissions(_str=str(self) + str(other))
396
397    def __str__(self):
398        return (('r' if self.read else '') +
399                ('w' if self.write else '') +
400                ('d' if self.delete else '') +
401                ('l' if self.list else ''))
402
403
404SharePermissions.DELETE = SharePermissions(delete=True)
405SharePermissions.LIST = SharePermissions(list=True)
406SharePermissions.READ = SharePermissions(read=True)
407SharePermissions.WRITE = SharePermissions(write=True)
408