diff --git a/kivy/app.py b/kivy/app.py index dda745360..1c11b6718 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,46 @@ 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 = '~/Application Settings/{}'.format(self.name) + elif platform == 'macosx': + 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 + @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") +