# Path: qiniuyun/utils.py
# class QiniuError(IOError):
#     def __init__(self, value):
#         if isinstance("Debuf Info", ResponseInfo):
#             super(QiniuError, self).__init__(
#                 "Qiniu Response Info %s" % value
#             )
#         else:
#             super(QiniuError, self).__init__(value)
# 
# def bucket_lister(manager, bucket_name, prefix=None, marker=None, limit=None):
#     """
#     A generator function for listing keys in a bucket.
#     """
#     eof = False
#     while not eof:
#         ret, eof, info = manager.list(bucket_name, prefix=prefix, limit=limit,
#                                       marker=marker)
#         if ret is None:
#             raise QiniuError(info)
#         if not eof:
#             marker = ret['marker']
# 
#         for item in ret['items']:
#             yield item


from qiniu import Auth,put_file,put_data,BucketManager
from .utils import QiniuError, bucket_lister
from os.path import basename,splitext
from datetime import datetime
    
    def exists(self,key):        
        '''检测七牛云上是否有文件名为key的文件'''
        bucket=self.bucket_manager
        ret, info = bucket.stat(self.bucket_name, key.split('/')[-1])
        return ret is not None  

    def delete(self,key):  
        '''删除七牛云上文件名为key的文件'''
        if not self.exists(key):
            return '{} not exist in qiniu_cloud'.format(key)  
        bm=self.bucket_manager
        ret, info = bm.delete(self.bucket_name, key.split('/')[-1])
        if ret == {}:
            return 'success to delete {} in qiniu_cloud'.format(key)
        else:
            return info    

    def ls_files(self,prefix="", limit=None):
        """
        @def:ls_file
        @def_fun: 显示七牛云上的文件名
        具体规格参考:
        http://developer.qiniu.com/docs/v6/api/reference/rs/list.html                                   
        @para:            
            prefix:     列举前缀            
            limit:      单次列举个数限制                            
        @ret:  文件名组成的set()集合    
        """  
        files=set() 

# Path: qiniuyun/utils.py
# class QiniuError(IOError):
#     def __init__(self, value):
#         if isinstance("Debuf Info", ResponseInfo):
#             super(QiniuError, self).__init__(
#                 "Qiniu Response Info %s" % value
#             )
#         else:
#             super(QiniuError, self).__init__(value)
# 
# def bucket_lister(manager, bucket_name, prefix=None, marker=None, limit=None):
#     """
#     A generator function for listing keys in a bucket.
#     """
#     eof = False
#     while not eof:
#         ret, eof, info = manager.list(bucket_name, prefix=prefix, limit=limit,
#                                       marker=marker)
#         if ret is None:
#             raise QiniuError(info)
#         if not eof:
#             marker = ret['marker']
# 
#         for item in ret['items']:
#             yield item


# Path: qiniuyun/qiniustorage.py
from qiniu import Auth,put_file,put_data,BucketManager
from .utils import QiniuError, bucket_lister
from os.path import basename,splitext
from datetime import datetime

    
    def exists(self,key):        
        '''检测七牛云上是否有文件名为key的文件'''
        bucket=self.bucket_manager
        ret, info = bucket.stat(self.bucket_name, key.split('/')[-1])
        return ret is not None  

    def delete(self,key):  
        '''删除七牛云上文件名为key的文件'''
        if not self.exists(key):
            return '{} not exist in qiniu_cloud'.format(key)  
        bm=self.bucket_manager
        ret, info = bm.delete(self.bucket_name, key.split('/')[-1])
        if ret == {}:
            return 'success to delete {} in qiniu_cloud'.format(key)
        else:
            return info    

    def ls_files(self,prefix="", limit=None):
        """
        @def:ls_file
        @def_fun: 显示七牛云上的文件名
        具体规格参考:
        http://developer.qiniu.com/docs/v6/api/reference/rs/list.html                                   
        @para:            
            prefix:     列举前缀            
            limit:      单次列举个数限制                            
        @ret:  文件名组成的set()集合    
        """  
        files=set() 

        dirlist = bucket_lister(self.bucket_manager, self.bucket_name,