~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to nova/objectstore/stored.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright [2010] [Anso Labs, LLC]
 
2
 
3
#    Licensed under the Apache License, Version 2.0 (the "License");
 
4
#    you may not use this file except in compliance with the License.
 
5
#    You may obtain a copy of the License at
 
6
 
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
 
 
15
"""
 
16
Properties of an object stored within a bucket.
 
17
"""
 
18
 
 
19
from nova.exception import NotFound, NotAuthorized
 
20
 
 
21
import os
 
22
import nova.crypto
 
23
 
 
24
class Object(object):
 
25
    def __init__(self, bucket, key):
 
26
        """ wrapper class of an existing key """
 
27
        self.bucket = bucket
 
28
        self.key = key
 
29
        self.path = bucket._object_path(key)
 
30
        if not os.path.isfile(self.path):
 
31
            raise NotFound
 
32
 
 
33
    def __repr__(self):
 
34
        return "<Object %s/%s>" % (self.bucket, self.key)
 
35
 
 
36
    @property
 
37
    def md5(self):
 
38
        """ computes the MD5 of the contents of file """
 
39
        with open(self.path, "r") as f:
 
40
            return nova.crypto.compute_md5(f)
 
41
 
 
42
    @property
 
43
    def mtime(self):
 
44
        """ mtime of file """
 
45
        return os.path.getmtime(self.path)
 
46
 
 
47
    def read(self):
 
48
         """ read all contents of key into memory and return """
 
49
         return self.file.read()
 
50
 
 
51
    @property
 
52
    def file(self):
 
53
        """ return a file object for the key """
 
54
        return open(self.path, 'rb')
 
55
 
 
56
    def delete(self):
 
57
        """ deletes the file """
 
58
        os.unlink(self.path)