Merge pull request #1020 from hansent/app-user-data-directory

fix for #952
This commit is contained in:
Mathieu Virbel 2013-03-20 17:17:06 -07:00
commit 9997cd2a9f
2 changed files with 49 additions and 0 deletions

View File

@ -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/<app_name>``is returned.
On Windows ``~/Application Settings/<app_name>``is returned.
On Mac OS X ``~/Library/Application Support <app_name>``is returned.
On Linux, `$XDG_CONFIG_HOME/<app_name>` 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

View File

@ -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")