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, gfile):
50 self.parent = parent 51 self.filename = gfile.name 52 self.iconNames = self._getIconNames()
53
54 - def _getIconNames(self):
55 import gio 56 gFile = gio.File(self._filename) 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.get_path(), self.name)
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):
74 import gio 75 gfile = gio.File(os.path.abspath(path)) 76 self.path = path 77 self.filename = gfile.get_basename() 78 self.iconNames = self._getIconNames(gfile)
79
80 - def _getIconNames(self, gFile):
81 gFileInfo = gFile.query_info('standard::icon') 82 gIcon = gFileInfo.get_icon() 83 return gIcon.get_names()
84 85 86 # IFile 87
88 - def getPath(self):
89 return self.path
90 91 # IDirectory 92
93 - def getFiles(self):
94 import gio 95 log.info('vfsgio', 'getting files for %s' % (self.path,)) 96 retval = [] 97 gfile = gio.File(os.path.abspath(self.path)) 98 try: 99 gfiles = gfile.enumerate_children('standard::*') 100 except gobject.GError, e: 101 if (e.domain == gio.ERROR and 102 e.code == gio.ERROR_PERMISSION_DENIED): 103 raise AccessDeniedError 104 raise 105 for gfile in gfiles: 106 filename = gfile.get_basename() 107 if filename.startswith('.') and filename != '..': 108 continue 109 if gfile.get_file_type() == gio.FILE_TYPE_DIRECTORY: 110 obj = GIODirectory(os.path.join(self.path, gfile.name)) 111 else: 112 obj = GIOFile(self.path, gfile) 113 retval.append(obj) 114 log.info('vfsgio', 'returning %r' % (retval,)) 115 return succeed(retval)
116 117
118 -def registerGIOJelly():
119 """Register the jelly used by the GIO VFS backend. 120 """ 121 setUnjellyableForClass(GIOFile, GIOFile) 122 setUnjellyableForClass(GIODirectory, GIODirectory) 123 log.info('jelly', 'GIO registered')
124