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

Source Code for Module flumotion.common.vfsgio

  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  """GIO backend for Virtual File System. 
 19  """ 
 20   
 21  import os 
 22   
 23  import gobject 
 24  from twisted.internet.defer import succeed 
 25  from twisted.spread.flavors import Copyable, RemoteCopy 
 26  from twisted.spread.jelly import setUnjellyableForClass 
 27  from zope.interface import implements 
 28   
 29  from flumotion.common import log 
 30  from flumotion.common.errors import AccessDeniedError, NotDirectoryError 
 31  from flumotion.common.interfaces import IDirectory, IFile 
 32   
 33  # gio is only imported inside nested scopes so that 
 34  # pychecker can ignore them, If pychecker ever gets fixed, 
 35  # move it back where it belongs 
 36  __pychecker__ = 'keepgoing' 
 37   
 38   
39 -class GIOFile(Copyable, RemoteCopy):
40 """I am object implementing L{IFile} on top of GIO, 41 see L{IFile} for more information. 42 """ 43 implements(IFile) 44
45 - def __init__(self, parent, gfileinfo):
46 self.parent = parent 47 self.filename = gfileinfo.get_name() 48 self.iconNames = self._getIconNames()
49
50 - def _getIconNames(self):
51 import gio 52 gFile = gio.File(self.getPath()) 53 gFileInfo = gFile.query_info('standard::icon') 54 gIcon = gFileInfo.get_icon() 55 return gIcon.get_names()
56 57 # IFile 58
59 - def getPath(self):
60 return os.path.join(self.parent, self.filename)
61 62
63 -class GIODirectory(Copyable, RemoteCopy):
64 """I am object implementing L{IDirectory} on top of GIO, 65 see L{IDirectory} for more information. 66 """ 67 implements(IDirectory) 68
69 - def __init__(self, path, name=None):
70 import gio 71 if not os.path.exists(path): 72 self.path = '/' 73 elif not os.path.isdir(path): 74 raise NotDirectoryError() 75 else: 76 self.path = os.path.abspath(path) 77 78 gfile = gio.File(self.path) 79 if name is None: 80 name = gfile.get_basename() 81 self.filename = name 82 self.iconNames = self._getIconNames(gfile)
83
84 - def _getIconNames(self, gFile):
85 gFileInfo = gFile.query_info('standard::icon') 86 gIcon = gFileInfo.get_icon() 87 return gIcon.get_names()
88 89 # IFile 90
91 - def getPath(self):
92 return self.path
93 94 # IDirectory 95
96 - def getFiles(self):
97 return succeed(self._cachedFiles)
98
99 - def cacheFiles(self):
100 """ 101 Fetches the files contained on the directory for posterior usage of 102 them. This should be called on the worker side to work or the files 103 wouldn't be the expected ones. 104 """ 105 import gio 106 log.debug('vfsgio', 'getting files for %s' % (self.path, )) 107 retval = [] 108 gfile = gio.File(os.path.abspath(self.path)) 109 try: 110 gfileinfos = gfile.enumerate_children('standard::*') 111 except gobject.GError, e: 112 if (e.domain == gio.ERROR and 113 e.code == gio.ERROR_PERMISSION_DENIED): 114 raise AccessDeniedError 115 raise 116 if self.path != '/': 117 retval.append(GIODirectory(os.path.dirname(self.path), name='..')) 118 for gfileinfo in gfileinfos: 119 filename = gfileinfo.get_name() 120 if filename.startswith('.') and filename != '..': 121 continue 122 if gfileinfo.get_file_type() == gio.FILE_TYPE_DIRECTORY: 123 obj = GIODirectory(os.path.join(self.path, 124 gfileinfo.get_name())) 125 else: 126 obj = GIOFile(self.path, gfileinfo) 127 retval.append(obj) 128 log.log('vfsgio', 'returning %r' % (retval, )) 129 self._cachedFiles = retval
130 131
132 -def registerGIOJelly():
133 """Register the jelly used by the GIO VFS backend. 134 """ 135 setUnjellyableForClass(GIOFile, GIOFile) 136 setUnjellyableForClass(GIODirectory, GIODirectory) 137 log.info('jelly', 'GIO registered')
138