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

Source Code for Module fife.extensions.fife_utils

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2010 by the FIFE team 
  5  #  http://www.fifengine.net 
  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  """ This file contains some functions that may be useful """ 
 25   
 26  import fife, re, sys, os 
 27   
 28  __all__ = ['is_fife_exc', 'getUserDataDirectory'] 
 29   
 30  _exc_re = re.compile(r'_\[(\w+)\]_') 
 31   
32 -def is_fife_exc(type, original_exc):
33 """ Checks if an exception is of given type. 34 Example: 35 try: 36 obj = self.model.createObject(str(id), str(nspace), parent) 37 except RuntimeError, e: 38 if is_fife_exc(fife.NameClash, e): 39 raise NameClash('Tried to create already existing object, ignoring') 40 raise 41 """ 42 ret = False 43 m = _exc_re.search(str(original_exc)) 44 if m: 45 if m.group(1) == type('').getTypeStr(): 46 ret = True 47 return ret
48
49 -def getUserDataDirectory(vendor, appname):
50 """ Gets the proper location to save configuration and data files, depending on depending on OS. 51 52 Windows: %APPDATA%\vendor\appname 53 Mac: ~/Library/Application Support/vendor/appname 54 Linux/Unix/Other: ~/.vendor/appname 55 56 See: 57 Brian Vanderburg II @ http://mail.python.org/pipermail/python-list/2008-May/660779.html 58 """ 59 dir = None 60 61 # WINDOWS 62 if os.name == "nt": 63 64 # Try env APPDATA or USERPROFILE or HOMEDRIVE/HOMEPATH 65 if "APPDATA" in os.environ: 66 dir = os.environ["APPDATA"] 67 68 if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" in os.environ): 69 dir = os.environ["USERPROFILE"] 70 if os.path.isdir(os.path.join(dir, "Application Data")): 71 dir = os.path.join(dir, "Application Data") 72 73 if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" in os.environ) and ("HOMEPATH" in os.environ): 74 dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"] 75 if os.path.isdir(os.path.join(dir, "Application Data")): 76 dir = os.path.join(dir, "Application Data") 77 78 if (dir is None) or (not os.path.isdir(dir)): 79 dir = os.path.expanduser("~") 80 81 # On windows, add vendor and app name 82 dir = os.path.join(dir, vendor, appname) 83 84 # Mac 85 elif os.name == "mac": # ?? may not be entirely correct 86 dir = os.path.expanduser("~") 87 dir = os.path.join(dir, "Library", "Application Support") 88 dir = os.path.join(dir, vendor, appname) 89 90 # Unix/Linux/all others 91 if dir is None: 92 dir = os.path.expanduser("~") 93 dir = os.path.join(dir, "."+vendor, appname) 94 95 # Create vendor/appname folder if it doesn't exist 96 if not os.path.isdir(dir): 97 os.makedirs(dir) 98 99 return dir
100