示例#1
0
文件: files.py 项目: hw20686832/vpan
    def __init__(self, path, **kwargs):
        self.redis = redis.Redis(**settings.REDIS)
        self.path = path
        self.root = '/'.join(path.split('/')[:2])
        self.s3 = kwargs.pop('s3')
        self.db = kwargs.pop('db', MotorClient(**settings.MONGODB).pan)
        self.current_user = kwargs.pop("current_user", {})
        self.sm = SpaceManager(self.current_user, self.db)
        self.log = Logger().getlog("files")

        self._load_from_cache()

        old_size = self.size
        # override obj info.
        for k, v in kwargs.items():
            if k == 'type':
                k = '_type'
            assert hasattr(self, k), "Set attr error, attr name '%s' not suported." % k
            setattr(self, k, v)

        self.size_diff = self.size - old_size
示例#2
0
文件: files.py 项目: hw20686832/vpan
class ObjectModel(object):
    """Reflect redis key structure to Python object, like as 'pan/uid/path'.
    - example:
    pan/100/book/language/c/book1.txt
    pan/100/book/language/python/book1.txt
    pan/100/book/language/python/book2.txt
    pan/100/book/language/go/book1.txt
    pan/100/book/language/go/book2.txt

    - value:
    {
        'name': '',
        'rights': '',
        'size': 0,
        'date': '',
        'type': 'dir|file'
    }
    """
    name = None
    rights = None
    size = 0
    date = None
    _type = None

    def __init__(self, path, **kwargs):
        self.redis = redis.Redis(**settings.REDIS)
        self.path = path
        self.root = '/'.join(path.split('/')[:2])
        self.s3 = kwargs.pop('s3')
        self.db = kwargs.pop('db', MotorClient(**settings.MONGODB).pan)
        self.current_user = kwargs.pop("current_user", {})
        self.sm = SpaceManager(self.current_user, self.db)
        self.log = Logger().getlog("files")

        self._load_from_cache()

        old_size = self.size
        # override obj info.
        for k, v in kwargs.items():
            if k == 'type':
                k = '_type'
            assert hasattr(self, k), "Set attr error, attr name '%s' not suported." % k
            setattr(self, k, v)

        self.size_diff = self.size - old_size

    def _load_from_cache(self):
        obj = self.redis.get(self.path)
        if obj:
            for k, v in json.loads(obj).items():
                if k == 'type':
                    k = '_type'
                setattr(self, k, v)

    def _copy(self, dist):
        self.redis.set(dist, json.dumps(self.__dict__))
        return self.size

    def rename(self, name):
        raise NotImplementedError

    def _set(self):
        self.redis.set(self.path, json.dumps(self.__dict__))
        return self.size_diff

    def _rm(self):
        self.redis.delete(self.path)
        return -self.size

    @coroutine
    def save(self):
        yield self.sm.extend(self._set())

    def __getitem__(self, key):
        return getattr(self, key)

    @property
    def __dict__(self):
        return {
            'name': self.name,
            'rights': self.rights,
            'size': self.size,
            'date': self.date,
            'type': self._type
        }
示例#3
0
 def _identify_space(self, size):
     """Identify wheather new size out of the total size.
     raise OutOfSpaceException if ident fail."""
     sm = SpaceManager(self.current_user, self.db)
     return sm.identify(size)