From aedb5bf44bd4bd552bc39a44bc49d29e3c394ca7 Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Sun, 17 Mar 2013 19:13:06 -0700 Subject: [PATCH 1/2] add user_data_dir to App class to get a platform conformant path to store user data --- kivy/app.py | 42 ++++++++++++++++++++++++++++++++++++++++++ kivy/tests/test_app.py | 8 ++++++++ 2 files changed, 50 insertions(+) diff --git a/kivy/app.py b/kivy/app.py index dda745360..65a4933b4 100644 --- a/kivy/app.py +++ b/kivy/app.py @@ -210,6 +210,7 @@ The current implemented Pause mechanism is: __all__ = ('App', ) +import os from inspect import getfile from os.path import dirname, join, exists, sep, expanduser from kivy.config import ConfigParser @@ -505,6 +506,47 @@ class App(EventDispatcher): self._app_directory = '.' return self._app_directory + @property + def user_data_dir(self): + ''' + .. versionadded:: 1.6.1 + + Returns the path to a directory in the users files system, which the + application can use to store additional data. + + Different platforms have different conventions for where to save user + data like preferences, saved games, and settings. This function + implements those conventions. + + On iOS ``/Documents``is returned (which is inside the apps sandbox). + + On Android ``/sdcard/``is returned. + + On Windows ``~/Application Settings/``is returned. + + On Mac OS X ``~/Library/Application Support ``is returned. + + On Linux, `$XDG_CONFIG_HOME/` is returned. + + ''' + data_dir = "" + if platform == 'ios': + data_dir = join('/Documents', self.name) + elif platform == 'android': + data_dir = join('/sdcard', self.name) + elif platform == 'win': + data_dir = expanduser( + '~/Application Settings/{}'.format(self.name)) + elif platform == 'macosx': + data_dir = expanduser( + '~/Library/Application Support/{}'.format(self.name)) + else: # _platform == 'linux' or anything else...: + data_dir = os.environ.get('XDG_CONFIG_HOME', '~/.config') + data_dir = join(data_dir, self.name) + if not exists(data_dir): + os.mkdir(data_dir) + return data_dir + @property def name(self): '''.. versionadded:: 1.0.7 diff --git a/kivy/tests/test_app.py b/kivy/tests/test_app.py index 1a3139352..1af30a553 100644 --- a/kivy/tests/test_app.py +++ b/kivy/tests/test_app.py @@ -1,6 +1,7 @@ import unittest from kivy.app import App from kivy.clock import Clock +import os.path class AppTest(unittest.TestCase): @@ -16,3 +17,10 @@ class AppTest(unittest.TestCase): a = TestKvApp() Clock.schedule_once(a.stop, .1) a.run() + + def test_user_data_dir(self): + a = App() + data_dir = a.user_data_dir + if not os.path.exists(data_dir): + raise Exception("user_data_dir didnt exists") + From 95d4262775e6a13365c2f4df33bb98cb7246d155 Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Mon, 18 Mar 2013 10:01:26 -0700 Subject: [PATCH 2/2] make sure expanduser is called on the returned string --- kivy/app.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kivy/app.py b/kivy/app.py index 65a4933b4..1c11b6718 100644 --- a/kivy/app.py +++ b/kivy/app.py @@ -535,14 +535,13 @@ class App(EventDispatcher): elif platform == 'android': data_dir = join('/sdcard', self.name) elif platform == 'win': - data_dir = expanduser( - '~/Application Settings/{}'.format(self.name)) + data_dir = '~/Application Settings/{}'.format(self.name) elif platform == 'macosx': - data_dir = expanduser( - '~/Library/Application Support/{}'.format(self.name)) + data_dir = '~/Library/Application Support/{}'.format(self.name) else: # _platform == 'linux' or anything else...: data_dir = os.environ.get('XDG_CONFIG_HOME', '~/.config') data_dir = join(data_dir, self.name) + data_dir = expanduser(data_dir) if not exists(data_dir): os.mkdir(data_dir) return data_dir