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