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 
 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 else: 78 self.path = os.path.abspath(path) 79 80 gfile = gio.File(self.path) 81 if name is None: 82 name = gfile.get_basename() 83 self.filename = name 84 self.iconNames = self._getIconNames(gfile)
85
86 - def _getIconNames(self, gFile):
87 gFileInfo = gFile.query_info('standard::icon') 88 gIcon = gFileInfo.get_icon() 89 return gIcon.get_names()
90 91 # IFile 92
93 - def getPath(self):
94 return self.path
95 96 # IDirectory 97
98 - def getFiles(self):
99 return succeed(self._cachedFiles)
100
101 - def cacheFiles(self):
102 """ 103 Fetches the files contained on the directory for posterior usage of 104 them. This should be called on the worker side to work or the files 105 wouldn't be the expected ones. 106 """ 107 import gio 108 log.debug('vfsgio', 'getting files for %s' % (self.path, )) 109 retval = [] 110 gfile = gio.File(os.path.abspath(self.path)) 111 try: 112 gfileinfos = gfile.enumerate_children('standard::*') 113 except gobject.GError, e: 114 if (e.domain == gio.ERROR and 115 e.code == gio.ERROR_PERMISSION_DENIED): 116 raise AccessDeniedError 117 raise 118 if self.path != '/': 119 retval.append(GIODirectory(os.path.dirname(self.path), name='..')) 120 for gfileinfo in gfileinfos: 121 filename = gfileinfo.get_name() 122 if filename.startswith('.') and filename != '..': 123 continue 124 if gfileinfo.get_file_type() == gio.FILE_TYPE_DIRECTORY: 125 obj = GIODirectory(os.path.join(self.path, 126 gfileinfo.get_name())) 127 else: 128 obj = GIOFile(self.path, gfileinfo) 129 retval.append(obj) 130 log.log('vfsgio', 'returning %r' % (retval, )) 131 self._cachedFiles = retval
132 133
134 -def registerGIOJelly():
135 """Register the jelly used by the GIO VFS backend. 136 """ 137 setUnjellyableForClass(GIOFile, GIOFile) 138 setUnjellyableForClass(GIODirectory, GIODirectory) 139 log.info('jelly', 'GIO registered')
140