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