Commit db06e17e authored by Luci Stanescu's avatar Luci Stanescu

Added ApplicationData class and made resource paths unicode friendly

parent 4cdce257
......@@ -3,22 +3,45 @@
"""Provide access to Blink's resources"""
__all__ = ['Resources']
__all__ = ['ApplicationData', 'Resources']
import os
import platform
import sys
from blink.util import classproperty
class DirectoryContextManager(str):
class DirectoryContextManager(unicode):
def __enter__(self):
self.directory = os.getcwd()
self.directory = os.getcwdu()
os.chdir(self)
def __exit__(self, type, value, traceback):
os.chdir(self.directory)
class ApplicationData(object):
"""Provide access to user data"""
_cached_directory = None
@classproperty
def directory(cls):
if cls._cached_directory is None:
if platform.system() == 'Darwin':
from Foundation import NSApplicationSupportDirectory, NSSearchPathForDirectoriesInDomains, NSUserDomainMask
cls._cached_directory = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True)[0]
elif platform.system() == 'Windows':
cls._cached_directory = os.path.join(os.environ['APPDATA'], 'Blink').decode(sys.getfilesystemencoding())
else:
cls._cached_directory = os.path.expanduser('~/.blink').decode(sys.getfilesystemencoding())
return DirectoryContextManager(cls._cached_directory)
@classmethod
def get(cls, resource):
return os.path.join(cls.directory, resource)
class Resources(object):
"""Provide access to Blink's resources"""
......@@ -37,9 +60,9 @@ class Resources(object):
else:
application_directory = binary_directory
if os.path.exists(os.path.join(application_directory, 'resources', 'blink.ui')):
cls._cached_directory = os.path.join(application_directory, 'resources')
cls._cached_directory = os.path.join(application_directory, 'resources').decode(sys.getfilesystemencoding())
else:
cls._cached_directory = os.path.join(application_directory, 'share', 'blink')
cls._cached_directory = os.path.join(application_directory, 'share', 'blink').decode(sys.getfilesystemencoding())
return DirectoryContextManager(cls._cached_directory)
@classmethod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment