Package horizons :: Package util :: Module platform
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.platform

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22  import os 
 23  import platform 
 24   
 25  CSIDL_PERSONAL = 5 # 'My documents' folder for win32 API 
 26   
 27   
28 -def get_home_directory():
29 """ 30 Returns the home directory of the user running UH. 31 """ 32 if platform.system() != "Windows": 33 return os.path.expanduser('~') 34 else: 35 import ctypes.wintypes 36 buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH) 37 # get the My Documents folder into buf.value 38 ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_PERSONAL, 0, 0, buf) 39 return buf.value
40 41 42 # define here as string to reduce chance of typos 43 UH = "unknown-horizons" 44 45
46 -def get_old_user_game_directory():
47 """ 48 Returns the old directory where for game-related data. 49 This is used when migrating from the old structure to the new 50 """ 51 user_dir = os.environ.get("UH_USER_DIR") 52 if user_dir: # not None or empty string 53 return user_dir 54 55 home_directory = get_home_directory() 56 if platform.system() != "Windows": 57 return os.path.join(home_directory, "." + UH) 58 else: 59 return os.path.join(home_directory, 'My Games', UH)
60 61
62 -def _try_directories(paths, default=None):
63 # takes a list of lists of strings as first argument 64 # returns the joined path for the first list where all strings exist and are nonempty 65 # or the second argument if there is no such list 66 # (lists could also be tuples of course) 67 for parts in paths: 68 if all(parts): 69 return os.path.join(*parts) 70 return default
71 72
73 -def get_user_game_directories():
74 """ 75 Returns a triplet of directories for game-related data. 76 The first value is the directory for configuration files (settings.xml) 77 The second is the directory for game data (saves, maps, screenshots, logs) 78 The third is the directory for cache (atlas-metadata.cache, yamldata.cache) 79 """ 80 home_directory = get_home_directory() 81 82 # these are the default directories 83 if platform.system() == "Windows": 84 user_dir = os.path.join(home_directory, 'My Games', UH) 85 config_dir = user_dir 86 data_dir = user_dir 87 cache_dir = os.path.join(user_dir, "cache") 88 else: 89 config_dir = os.path.join(home_directory, ".config", UH) 90 data_dir = os.path.join(home_directory, ".local", "share", UH) 91 cache_dir = os.path.join(home_directory, ".cache", UH) 92 93 # for each of the directories, try the paths with environment variables in this order 94 # if one of the environment variables exists, use that path 95 # otherwise, keep using the default dir 96 97 config_dir = _try_directories([ 98 (os.environ.get("UH_USER_CONFIG_DIR"),), 99 (os.environ.get("UH_USER_DIR"),), 100 (os.environ.get("XDG_CONFIG_HOME"), UH)], 101 config_dir) 102 103 if not os.path.exists(config_dir): 104 os.makedirs(config_dir) 105 106 data_dir = _try_directories([ 107 (os.environ.get("UH_USER_DATA_DIR"),), 108 (os.environ.get("UH_USER_DIR"),), 109 (os.environ.get("XDG_DATA_HOME"), UH)], 110 data_dir) 111 112 if not os.path.exists(data_dir): 113 os.makedirs(data_dir) 114 115 cache_dir = _try_directories([ 116 (os.environ.get("UH_USER_CACHE_DIR"),), 117 (os.environ.get("UH_USER_DIR"), "cache"), 118 (os.environ.get("XDG_CACHE_HOME"), UH)], 119 cache_dir) 120 121 if not os.path.exists(cache_dir): 122 os.makedirs(cache_dir) 123 124 return config_dir, data_dir, cache_dir
125