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