Package flumotion :: Package common :: Module vfsgnome
[hide private]

Source Code for Module flumotion.common.vfsgnome

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """GnomeVFS backend for Virtual File System. 
 23  """ 
 24   
 25  import os 
 26   
 27  from twisted.internet.defer import succeed 
 28  from twisted.spread.flavors import Copyable, RemoteCopy 
 29  from twisted.spread.jelly import setUnjellyableForClass 
 30  from zope.interface import implements 
 31   
 32  from flumotion.common import log 
 33  from flumotion.common.errors import AccessDeniedError 
 34  from flumotion.common.interfaces import IDirectory, IFile 
 35   
 36  # gnomevfs is only imported inside nested scopes so that 
 37  # pychecker can ignore them, If pychecker ever gets fixed, 
 38  # move it back where it belongs 
 39  __pychecker__ = 'keepgoing' 
 40   
 41   
42 -class GnomeVFSFile(Copyable, RemoteCopy):
43 """I am object implementing L{IFile} on top of GnomeVFS, 44 see L{IFile} for more information. 45 """ 46 implements(IFile) 47
48 - def __init__(self, parent, fileInfo):
49 self.parent = parent 50 self.filename = fileInfo.name 51 self.iconNames = ['gnome-fs-regular']
52 53 # IFile 54
55 - def getPath(self):
56 return os.path.join(self.parent, self.filename)
57 58
59 -class GnomeVFSDirectory(Copyable, RemoteCopy):
60 """I am object implementing L{IDirectory} on top of GnomeVFS, 61 see L{IDirectory} for more information. 62 """ 63 implements(IDirectory) 64
65 - def __init__(self, path, name=None):
66 import gnomevfs 67 if not os.path.exists(path): 68 self.path = '/' 69 else: 70 self.path = os.path.abspath(path) 71 72 if name is None: 73 fileInfo = gnomevfs.get_file_info(self.path) 74 name = fileInfo.name 75 self.filename = name 76 self.iconNames = ['gnome-fs-directory'] 77 self._cachedFiles = None
78 79 # IFile 80
81 - def getPath(self):
82 return self.path
83 84 # IDirectory 85
86 - def getFiles(self):
87 return succeed(self._cachedFiles)
88
89 - def cacheFiles(self):
90 """ 91 Fetches the files contained on the directory for posterior usage of 92 them. This should be called on the worker side to work or the files 93 wouldn't be the expected ones. 94 """ 95 import gnomevfs 96 log.debug('vfsgnome', 'getting files for %s' % (self.path, )) 97 retval = [] 98 try: 99 fileInfos = gnomevfs.open_directory(self.path) 100 except gnomevfs.AccessDeniedError: 101 raise AccessDeniedError 102 if self.path != '/': 103 retval.append(GnomeVFSDirectory(os.path.dirname(self.path), 104 name='..')) 105 for fileInfo in fileInfos: 106 filename = fileInfo.name 107 if filename.startswith('.'): 108 continue 109 if fileInfo.type == gnomevfs.FILE_TYPE_DIRECTORY: 110 obj = GnomeVFSDirectory(os.path.join(self.path, 111 fileInfo.name)) 112 else: 113 obj = GnomeVFSFile(self.path, fileInfo) 114 retval.append(obj) 115 log.log('vfsgnome', 'returning %r' % (retval, )) 116 self._cachedFiles = retval
117 118
119 -def registerGnomeVFSJelly():
120 """Register the jelly used by the GnomeVFS VFS backend. 121 """ 122 setUnjellyableForClass(GnomeVFSFile, GnomeVFSFile) 123 setUnjellyableForClass(GnomeVFSDirectory, GnomeVFSDirectory) 124 log.info('jelly', 'GnomeVFS registered')
125