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