PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/file/key.py

#
Python | 168 lines | 115 code | 12 blank | 41 comment | 11 complexity | 34faf36403ebb04f6471b085abe69aa7 MD5 | raw file
  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. # File representation of key, for use with "file://" URIs.
  23. import os, shutil, StringIO
  24. import sys
  25. class Key(object):
  26. KEY_STREAM_READABLE = 0x01
  27. KEY_STREAM_WRITABLE = 0x02
  28. KEY_STREAM = (KEY_STREAM_READABLE | KEY_STREAM_WRITABLE)
  29. KEY_REGULAR_FILE = 0x00
  30. def __init__(self, bucket, name, fp=None, key_type=KEY_REGULAR_FILE):
  31. self.bucket = bucket
  32. self.full_path = name
  33. if name == '-':
  34. self.name = None
  35. else:
  36. self.name = name
  37. self.key_type = key_type
  38. if key_type == self.KEY_STREAM_READABLE:
  39. self.fp = sys.stdin
  40. self.full_path = '<STDIN>'
  41. elif key_type == self.KEY_STREAM_WRITABLE:
  42. self.fp = sys.stdout
  43. self.full_path = '<STDOUT>'
  44. else:
  45. self.fp = fp
  46. def __str__(self):
  47. return 'file://' + self.full_path
  48. def get_file(self, fp, headers=None, cb=None, num_cb=10, torrent=False):
  49. """
  50. Retrieves a file from a Key
  51. :type fp: file
  52. :param fp: File pointer to put the data into
  53. :type headers: string
  54. :param: ignored in this subclass.
  55. :type cb: function
  56. :param cb: ignored in this subclass.
  57. :type cb: int
  58. :param num_cb: ignored in this subclass.
  59. """
  60. if self.key_type & self.KEY_STREAM_READABLE:
  61. raise BotoClientError('Stream is not Readable')
  62. elif self.key_type & self.KEY_STREAM_WRITABLE:
  63. key_file = self.fp
  64. else:
  65. key_file = open(self.full_path, 'rb')
  66. try:
  67. shutil.copyfileobj(key_file, fp)
  68. finally:
  69. key_file.close()
  70. def set_contents_from_file(self, fp, headers=None, replace=True, cb=None,
  71. num_cb=10, policy=None, md5=None):
  72. """
  73. Store an object in a file using the name of the Key object as the
  74. key in file URI and the contents of the file pointed to by 'fp' as the
  75. contents.
  76. :type fp: file
  77. :param fp: the file whose contents to upload
  78. :type headers: dict
  79. :param headers: ignored in this subclass.
  80. :type replace: bool
  81. :param replace: If this parameter is False, the method
  82. will first check to see if an object exists in the
  83. bucket with the same key. If it does, it won't
  84. overwrite it. The default value is True which will
  85. overwrite the object.
  86. :type cb: function
  87. :param cb: ignored in this subclass.
  88. :type cb: int
  89. :param num_cb: ignored in this subclass.
  90. :type policy: :class:`boto.s3.acl.CannedACLStrings`
  91. :param policy: ignored in this subclass.
  92. :type md5: A tuple containing the hexdigest version of the MD5 checksum
  93. of the file as the first element and the Base64-encoded
  94. version of the plain checksum as the second element.
  95. This is the same format returned by the compute_md5 method.
  96. :param md5: ignored in this subclass.
  97. """
  98. if self.key_type & self.KEY_STREAM_WRITABLE:
  99. raise BotoClientError('Stream is not writable')
  100. elif self.key_type & self.KEY_STREAM_READABLE:
  101. key_file = self.fp
  102. else:
  103. if not replace and os.path.exists(self.full_path):
  104. return
  105. key_file = open(self.full_path, 'wb')
  106. try:
  107. shutil.copyfileobj(fp, key_file)
  108. finally:
  109. key_file.close()
  110. def get_contents_as_string(self, headers=None, cb=None, num_cb=10,
  111. torrent=False):
  112. """
  113. Retrieve file data from the Key, and return contents as a string.
  114. :type headers: dict
  115. :param headers: ignored in this subclass.
  116. :type cb: function
  117. :param cb: ignored in this subclass.
  118. :type cb: int
  119. :param num_cb: ignored in this subclass.
  120. :type cb: int
  121. :param num_cb: ignored in this subclass.
  122. :type torrent: bool
  123. :param torrent: ignored in this subclass.
  124. :rtype: string
  125. :returns: The contents of the file as a string
  126. """
  127. fp = StringIO.StringIO()
  128. self.get_contents_to_file(fp)
  129. return fp.getvalue()
  130. def is_stream(self):
  131. return (self.key_type & self.KEY_STREAM)
  132. def close(self):
  133. """
  134. Closes fp associated with underlying file.
  135. Caller should call this method when done with this class, to avoid
  136. using up OS resources (e.g., when iterating over a large number
  137. of files).
  138. """
  139. self.fp.close()