Package fife :: Package extensions :: Module filebrowser
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.filebrowser

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2009 by the FIFE team 
  5  #  http://www.fifengine.de 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  """ a filebrowser implementation for pychan """ 
 25   
 26  import pychan 
 27  import pychan.widgets as widgets 
 28  import sys 
 29   
30 -def u2s(string):
31 return string.encode(sys.getfilesystemencoding())
32
33 -class FileBrowser(object):
34 """ The B{FileBrowser} displays directory and file listings from the vfs. 35 36 B{The fileSelected} parameter is a callback invoked when a file selection has been made; its 37 signature must be fileSelected(path,filename). 38 39 B{If selectdir} is set, fileSelected's filename parameter should be optional. 40 41 B{The savefile} option provides a box for supplying a new filename that doesn't exist yet. 42 43 B{The selectdir} option allows directories to be selected as well as files. 44 """
45 - def __init__(self, engine, fileSelected, savefile=False, selectdir=False, extensions=('xml',), guixmlpath="gui/filebrowser.xml"):
46 """ 47 @type engine: object 48 @param engine: initiated instance of FIFE 49 @type fileSelected: function 50 @param fileSelected: callback invoked on file selection 51 @type savefile: bool 52 @param savefile: flag to provide a gui for an usergiven filename 53 @type selectdir: bool 54 @param selectdir: flag to fire fileSelected without filename 55 @type extensions: tuple 56 @param extensions: list of extensions the filebrowser should show (defaults to xml) 57 @type guixmlpath: string 58 @param guixmlpath: path to the xml guifile defaults to (gui/filebrowser.xml) 59 """ 60 self.engine = engine 61 self.fileSelected = fileSelected 62 63 self._widget = None 64 self.savefile = savefile 65 self.selectdir = selectdir 66 67 self.guixmlpath = guixmlpath 68 69 self.extensions = extensions 70 self.path = './..' 71 self.dir_list = [] 72 self.file_list = []
73
74 - def showBrowser(self):
75 """ create and / or show the gui """ 76 if self._widget: 77 self.setDirectory(self.path) 78 self._widget.show() 79 return 80 81 self._widget = pychan.loadXML(self.guixmlpath) 82 self._widget.mapEvents({ 83 'dirList' : self._selectDir, 84 'selectButton' : self._selectFile, 85 'closeButton' : self._widget.hide 86 }) 87 if self.savefile: 88 self._file_entry = widgets.TextField(name='saveField', text=u'') 89 self._widget.findChild(name="fileColumn").addChild(self._file_entry) 90 91 self.setDirectory(self.path) 92 self._widget.show()
93
94 - def setDirectory(self, path):
95 """ sets the current directory according to path """ 96 path_copy = self.path 97 self.path = path 98 if not self._widget: return 99 100 def decodeList(list): 101 fs_encoding = sys.getfilesystemencoding() 102 if fs_encoding is None: fs_encoding = "ascii" 103 104 newList = [] 105 for i in list: 106 try: newList.append(unicode(i, fs_encoding)) 107 except: 108 newList.append(unicode(i, fs_encoding, 'replace')) 109 print "WARNING: Could not decode item:", i 110 return newList
111 112 dir_list_copy = list(self.dir_list) 113 file_list_copy = list(self.file_list) 114 115 self.dir_list = [] 116 self.file_list = [] 117 118 try: 119 dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(self.path)) 120 file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(self.path)) 121 self.dir_list = decodeList(dir_list) 122 self.file_list = decodeList(file_list) 123 except: 124 self.path = path_copy 125 self.dir_list = list(dir_list_copy) 126 self.file_list = list(file_list_copy) 127 print "WARNING: Tried to browse to directory that is not accessible!" 128 129 self._widget.distributeInitialData({ 130 'dirList' : self.dir_list, 131 'fileList' : self.file_list 132 }) 133 134 self._widget.adaptLayout()
135
136 - def _selectDir(self):
137 """ callback for directory ListBox """ 138 selection = self._widget.collectData('dirList') 139 if selection >= 0 and selection < len(self.dir_list): 140 new_dir = u2s(self.dir_list[selection]) 141 lst = self.path.split('/') 142 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.': 143 lst.pop() 144 else: 145 lst.append(new_dir) 146 147 path = '/'.join(lst) 148 self.setDirectory(path)
149
150 - def _selectFile(self):
151 """ callback for selectButton, hides filebrowser """ 152 self._widget.hide() 153 selection = self._widget.collectData('fileList') 154 155 if self.savefile: 156 if self._widget.collectData('saveField'): 157 self.fileSelected(self.path, u2s(self._widget.collectData('saveField'))) 158 return 159 160 if selection >= 0 and selection < len(self.file_list): 161 self.fileSelected(self.path, u2s(self.file_list[selection])) 162 return 163 164 if self.selectdir: 165 self.fileSelected(self.path) 166 return 167 168 print 'FileBrowser: error, no selection.'
169