comparison env/lib/python3.9/site-packages/boto/file/key.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # Copyright 2010 Google Inc.
2 # Copyright (c) 2011, Nexenta Systems Inc.
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish, dis-
8 # tribute, sublicense, and/or sell copies of the Software, and to permit
9 # persons to whom the Software is furnished to do so, subject to the fol-
10 # lowing conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22
23 # File representation of key, for use with "file://" URIs.
24
25 import os, shutil
26 import sys
27
28 from boto.compat import StringIO
29
30 class Key(object):
31
32 KEY_STREAM_READABLE = 0x01
33 KEY_STREAM_WRITABLE = 0x02
34 KEY_STREAM = (KEY_STREAM_READABLE | KEY_STREAM_WRITABLE)
35 KEY_REGULAR_FILE = 0x00
36
37 def __init__(self, bucket, name, fp=None, key_type=KEY_REGULAR_FILE):
38 self.bucket = bucket
39 self.full_path = name
40 if name == '-':
41 self.name = None
42 self.size = None
43 else:
44 self.name = name
45 self.size = os.stat(name).st_size
46 self.key_type = key_type
47 if key_type == self.KEY_STREAM_READABLE:
48 self.fp = sys.stdin
49 self.full_path = '<STDIN>'
50 elif key_type == self.KEY_STREAM_WRITABLE:
51 self.fp = sys.stdout
52 self.full_path = '<STDOUT>'
53 else:
54 self.fp = fp
55
56 def __str__(self):
57 return 'file://' + self.full_path
58
59 def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False):
60 """
61 Retrieves a file from a Key
62
63 :type fp: file
64 :param fp: File pointer to put the data into
65
66 :type headers: string
67 :param: ignored in this subclass.
68
69 :type cb: function
70 :param cb: ignored in this subclass.
71
72 :type cb: int
73 :param num_cb: ignored in this subclass.
74 """
75 if self.key_type & self.KEY_STREAM_WRITABLE:
76 raise BotoClientError('Stream is not readable')
77 elif self.key_type & self.KEY_STREAM_READABLE:
78 key_file = self.fp
79 else:
80 key_file = open(self.full_path, 'rb')
81 try:
82 shutil.copyfileobj(key_file, fp)
83 finally:
84 key_file.close()
85
86 def set_contents_from_file(self, fp, headers=None, replace=True, cb=None,
87 num_cb=10, policy=None, md5=None):
88 """
89 Store an object in a file using the name of the Key object as the
90 key in file URI and the contents of the file pointed to by 'fp' as the
91 contents.
92
93 :type fp: file
94 :param fp: the file whose contents to upload
95
96 :type headers: dict
97 :param headers: ignored in this subclass.
98
99 :type replace: bool
100 :param replace: If this parameter is False, the method
101 will first check to see if an object exists in the
102 bucket with the same key. If it does, it won't
103 overwrite it. The default value is True which will
104 overwrite the object.
105
106 :type cb: function
107 :param cb: ignored in this subclass.
108
109 :type cb: int
110 :param num_cb: ignored in this subclass.
111
112 :type policy: :class:`boto.s3.acl.CannedACLStrings`
113 :param policy: ignored in this subclass.
114
115 :type md5: A tuple containing the hexdigest version of the MD5 checksum
116 of the file as the first element and the Base64-encoded
117 version of the plain checksum as the second element.
118 This is the same format returned by the compute_md5 method.
119 :param md5: ignored in this subclass.
120 """
121 if self.key_type & self.KEY_STREAM_READABLE:
122 raise BotoClientError('Stream is not writable')
123 elif self.key_type & self.KEY_STREAM_WRITABLE:
124 key_file = self.fp
125 else:
126 if not replace and os.path.exists(self.full_path):
127 return
128 key_file = open(self.full_path, 'wb')
129 try:
130 shutil.copyfileobj(fp, key_file)
131 finally:
132 key_file.close()
133
134 def get_contents_to_file(self, fp, headers=None, cb=None, num_cb=None,
135 torrent=False, version_id=None,
136 res_download_handler=None, response_headers=None):
137 """
138 Copy contents from the current file to the file pointed to by 'fp'.
139
140 :type fp: File-like object
141 :param fp:
142
143 :type headers: dict
144 :param headers: Unused in this subclass.
145
146 :type cb: function
147 :param cb: Unused in this subclass.
148
149 :type cb: int
150 :param num_cb: Unused in this subclass.
151
152 :type torrent: bool
153 :param torrent: Unused in this subclass.
154
155 :type res_upload_handler: ResumableDownloadHandler
156 :param res_download_handler: Unused in this subclass.
157
158 :type response_headers: dict
159 :param response_headers: Unused in this subclass.
160 """
161 shutil.copyfileobj(self.fp, fp)
162
163 def get_contents_as_string(self, headers=None, cb=None, num_cb=10,
164 torrent=False):
165 """
166 Retrieve file data from the Key, and return contents as a string.
167
168 :type headers: dict
169 :param headers: ignored in this subclass.
170
171 :type cb: function
172 :param cb: ignored in this subclass.
173
174 :type cb: int
175 :param num_cb: ignored in this subclass.
176
177 :type cb: int
178 :param num_cb: ignored in this subclass.
179
180 :type torrent: bool
181 :param torrent: ignored in this subclass.
182
183 :rtype: string
184 :returns: The contents of the file as a string
185 """
186
187 fp = StringIO()
188 self.get_contents_to_file(fp)
189 return fp.getvalue()
190
191 def is_stream(self):
192 return (self.key_type & self.KEY_STREAM)
193
194 def close(self):
195 """
196 Closes fp associated with underlying file.
197 Caller should call this method when done with this class, to avoid
198 using up OS resources (e.g., when iterating over a large number
199 of files).
200 """
201 self.fp.close()