Skip to content

Commit

Permalink
Extract home/game directory determination into function
Browse files Browse the repository at this point in the history
  • Loading branch information
squiddy committed Jun 9, 2017
1 parent 7677635 commit 0c4a3df
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
14 changes: 4 additions & 10 deletions horizons/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from typing import List, Optional

from horizons.ext.enum import Enum
from horizons.util.platform import get_user_game_directory


"""This file keeps track of the constants that are used in Unknown Horizons.
Expand Down Expand Up @@ -545,17 +546,10 @@ class LAYERS:
# Prefer the value from the environment. Used to override user dir when
# running GUI tests.
_user_dir = os.environ['UH_USER_DIR']
elif platform.system() != "Windows":
_user_dir = os.path.join(os.path.expanduser('~'), '.unknown-horizons')
else:
import ctypes.wintypes
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
# get the My Documents folder into buf.value
ctypes.windll.shell32.SHGetFolderPathW(0, 5, 0, 0, buf)
my_games = os.path.join(buf.value, 'My Games')
if not os.path.exists(my_games):
os.makedirs(my_games)
_user_dir = os.path.join(my_games, 'unknown-horizons')
_user_dir = str(get_user_game_directory())
if not os.path.exists(_user_dir):
os.makedirs(_user_dir)


class GFX:
Expand Down
4 changes: 2 additions & 2 deletions horizons/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import os

from horizons.constants import PATHS


def create_user_dirs():
"""Creates the userdir and subdirs. Includes from horizons."""
from horizons.constants import PATHS

for directory in (PATHS.USER_DIR, PATHS.LOG_DIR, PATHS.USER_MAPS_DIR, PATHS.SCREENSHOT_DIR):
if not os.path.isdir(directory):
os.makedirs(directory)
51 changes: 51 additions & 0 deletions horizons/util/platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# ###################################################
# Copyright (C) 2008-2017 The Unknown Horizons Team
# team@unknown-horizons.org
# This file is part of Unknown Horizons.
#
# Unknown Horizons is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# ###################################################

import os
import platform
from pathlib import PurePath

CSIDL_PERSONAL = 5 # 'My documents' folder for win32 API


def get_home_directory():
"""
Returns the home directory of the user running UH.
"""
if platform.system() != "Windows":
return PurePath(os.path.expanduser('~'))
else:
import ctypes.wintypes
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
# get the My Documents folder into buf.value
ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_PERSONAL, 0, 0, buf)
return PurePath(buf.value)


def get_user_game_directory():
"""
Returns the directory where we store game-related data, such as savegames.
"""
home_directory = get_home_directory()
if platform.system() != "Windows":
return home_directory.joinpath('.unknown-horizons')
else:
return home_directory.joinpath('My Games', 'unknown-horizons')
40 changes: 40 additions & 0 deletions tests/unittests/util/test_platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ###################################################
# Copyright (C) 2008-2017 The Unknown Horizons Team
# team@unknown-horizons.org
# This file is part of Unknown Horizons.
#
# Unknown Horizons is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# ###################################################

from pathlib import PurePath

from horizons.util.platform import get_user_game_directory


def test_get_user_game_directory_windows(mocker, tmpdir):
mocker.patch('horizons.util.platform.get_home_directory',
return_value=PurePath(str(tmpdir)))
mocker.patch('platform.system', return_value='Windows')

assert str(get_user_game_directory()) == tmpdir.join('My Games', 'unknown-horizons')


def test_get_user_game_directory_unix(mocker, tmpdir):
mocker.patch('horizons.util.platform.get_home_directory',
return_value=PurePath(str(tmpdir)))
mocker.patch('platform.system', return_value='Linux')

assert str(get_user_game_directory()) == tmpdir.join('.unknown-horizons')

0 comments on commit 0c4a3df

Please sign in to comment.