コード例 #1
0
ファイル: __init__.py プロジェクト: atty303/pyfilesystem
class _TahoeFS(FS):    
    def __init__(self, dircap, autorun, largefilesize, webapi):
        self.dircap = dircap if not dircap.endswith('/') else dircap[:-1]
        self.autorun = autorun
        self.largefilesize = largefilesize
        self.connection = Connection(webapi)
        self.tahoeutil = TahoeUtil(webapi)
        self.readonly = dircap.startswith('URI:DIR2-RO')
        
        super(_TahoeFS, self).__init__(thread_synchronize=_thread_synchronize_default)       
    
    def _log(self, level, message):
        if not logger.isEnabledFor(level): return
        logger.log(level, u'(%d) %s' % (id(self),
                                unicode(message).encode('ASCII', 'replace')))
        
    def _fixpath(self, path):
        return abspath(normpath(path))
    
    def _get_file_handler(self, path):
        if not self.autorun:
            if path.lower().startswith('/autorun.'):
                self._log(DEBUG, 'Access to file %s denied' % path)
                return NullFile()

        return self.getrange(path, 0)
    
    @_fix_path
    def getpathurl(self, path, allow_none=False, webapi=None):
        '''
            Retrieve URL where the file/directory is stored
        '''
        if webapi == None:
            webapi = self.connection.webapi
            
        self._log(DEBUG, "Retrieving URL for %s over %s" % (path, webapi))
        path = self.tahoeutil.fixwinpath(path, False)
        return u"%s/uri/%s%s" % (webapi, self.dircap, path)

    @_fix_path
    def getrange(self, path, offset, length=None):
        path = self.tahoeutil.fixwinpath(path, False)
        return self.connection.get(u'/uri/%s%s' % (self.dircap, path),
                    offset=offset, length=length)
       
    @_fix_path             
    def setcontents(self, path, file, chunk_size=64*1024):    
        self._log(INFO, 'Uploading file %s' % path)
        path = self.tahoeutil.fixwinpath(path, False)
        size=None
        
        if self.readonly:
            raise errors.UnsupportedError('read only filesystem')
        
        # Workaround for large files:
        # First create zero file placeholder, then
        # upload final content.
        if self.largefilesize != None and getattr(file, 'read', None):
            # As 'file' can be also a string, need to check,
            # if 'file' looks like duck. Sorry, file.
            file.seek(0, SEEK_END)
            size = file.tell()
            file.seek(0)

            if size > self.largefilesize:
                self.connection.put(u'/uri/%s%s' % (self.dircap, path),
                    "PyFilesystem.TahoeFS: Upload started, final size %d" % size)

        self.connection.put(u'/uri/%s%s' % (self.dircap, path), file, size=size)

    @_fix_path
    def getinfo(self, path): 
        self._log(INFO, 'Reading meta for %s' % path)
        info = self.tahoeutil.info(self.dircap, path)        
        #import datetime
        #info['created_time'] = datetime.datetime.now()
        #info['modified_time'] = datetime.datetime.now()
        #info['accessed_time'] = datetime.datetime.now()
        return info