merged settingsmanager with applocation; moved applocation

This commit is contained in:
Andreas Preikschat 2013-02-27 12:24:50 +01:00
parent 00451ebfe0
commit 4d1d912aaa
7 changed files with 209 additions and 192 deletions

View File

@ -44,7 +44,7 @@ from openlp.core.lib.theme import ThemeXML, BackgroundType, VerticalType, Backgr
from openlp.core.lib.ui import critical_error_message_box, create_widget_action
from openlp.core.theme import Theme
from openlp.core.ui import FileRenameForm, ThemeForm
from openlp.core.utils import AppLocation, SettingsManager, delete_file, locale_compare, get_filesystem_encoding
from openlp.core.utils import AppLocation, delete_file, locale_compare, get_filesystem_encoding
log = logging.getLogger(__name__)
@ -149,7 +149,7 @@ class ThemeManager(QtGui.QWidget):
Import new themes downloaded by the first time wizard
"""
self.application.set_busy_cursor()
files = SettingsManager.get_files(self.settingsSection, u'.otz')
files = AppLocation.get_files(self.settingsSection, u'.otz')
for theme_file in files:
theme_file = os.path.join(self.path, theme_file)
self.unzip_theme(theme_file, self.path)
@ -418,10 +418,10 @@ class ThemeManager(QtGui.QWidget):
log.debug(u'Load themes from dir')
self.theme_list = []
self.theme_list_widget.clear()
files = SettingsManager.get_files(self.settingsSection, u'.png')
files = AppLocation.get_files(self.settingsSection, u'.png')
if first_time:
self.first_time()
files = SettingsManager.get_files(self.settingsSection, u'.png')
files = AppLocation.get_files(self.settingsSection, u'.png')
# No themes have been found so create one
if not files:
theme = ThemeXML()
@ -429,7 +429,7 @@ class ThemeManager(QtGui.QWidget):
self._write_theme(theme, None, None)
Settings().setValue(self.settingsSection + u'/global theme', theme.theme_name)
self.config_updated()
files = SettingsManager.get_files(self.settingsSection, u'.png')
files = AppLocation.get_files(self.settingsSection, u'.png')
# Sort the themes by its name considering language specific
files.sort(key=lambda file_name: unicode(file_name), cmp=locale_compare)
# now process the file list of png files

View File

@ -50,7 +50,6 @@ if sys.platform != u'win32' and sys.platform != u'darwin':
except ImportError:
XDG_BASE_AVAILABLE = False
import openlp
from openlp.core.lib import translate, check_directory_exists
log = logging.getLogger(__name__)
@ -78,107 +77,6 @@ class VersionThread(QtCore.QThread):
if LooseVersion(str(version)) > LooseVersion(str(app_version[u'full'])):
Registry().execute(u'openlp_version_check', u'%s' % version)
class AppLocation(object):
"""
The :class:`AppLocation` class is a static class which retrieves a
directory based on the directory type.
"""
AppDir = 1
ConfigDir = 2
DataDir = 3
PluginsDir = 4
VersionDir = 5
CacheDir = 6
LanguageDir = 7
# Base path where data/config/cache dir is located
BaseDir = None
@staticmethod
def get_directory(dir_type=1):
"""
Return the appropriate directory according to the directory type.
``dir_type``
The directory type you want, for instance the data directory.
"""
if dir_type == AppLocation.AppDir:
return _get_frozen_path(os.path.abspath(os.path.split(sys.argv[0])[0]), os.path.split(openlp.__file__)[0])
elif dir_type == AppLocation.PluginsDir:
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
return _get_frozen_path(os.path.join(app_path, u'plugins'),
os.path.join(os.path.split(openlp.__file__)[0], u'plugins'))
elif dir_type == AppLocation.VersionDir:
return _get_frozen_path(os.path.abspath(os.path.split(sys.argv[0])[0]), os.path.split(openlp.__file__)[0])
elif dir_type == AppLocation.LanguageDir:
app_path = _get_frozen_path(os.path.abspath(os.path.split(sys.argv[0])[0]), _get_os_dir_path(dir_type))
return os.path.join(app_path, u'i18n')
elif dir_type == AppLocation.DataDir and AppLocation.BaseDir:
return os.path.join(AppLocation.BaseDir, 'data')
else:
return _get_os_dir_path(dir_type)
@staticmethod
def get_data_path():
"""
Return the path OpenLP stores all its data under.
"""
# Check if we have a different data location.
if Settings().contains(u'advanced/data path'):
path = Settings().value(u'advanced/data path')
else:
path = AppLocation.get_directory(AppLocation.DataDir)
check_directory_exists(path)
return os.path.normpath(path)
@staticmethod
def get_section_data_path(section):
"""
Return the path a particular module stores its data under.
"""
data_path = AppLocation.get_data_path()
path = os.path.join(data_path, section)
check_directory_exists(path)
return path
def _get_os_dir_path(dir_type):
"""
Return a path based on which OS and environment we are running in.
"""
encoding = sys.getfilesystemencoding()
if sys.platform == u'win32':
if dir_type == AppLocation.DataDir:
return os.path.join(unicode(os.getenv(u'APPDATA'), encoding), u'openlp', u'data')
elif dir_type == AppLocation.LanguageDir:
return os.path.split(openlp.__file__)[0]
return os.path.join(unicode(os.getenv(u'APPDATA'), encoding), u'openlp')
elif sys.platform == u'darwin':
if dir_type == AppLocation.DataDir:
return os.path.join(unicode(os.getenv(u'HOME'), encoding),
u'Library', u'Application Support', u'openlp', u'Data')
elif dir_type == AppLocation.LanguageDir:
return os.path.split(openlp.__file__)[0]
return os.path.join(unicode(os.getenv(u'HOME'), encoding), u'Library', u'Application Support', u'openlp')
else:
if dir_type == AppLocation.LanguageDir:
prefixes = [u'/usr/local', u'/usr']
for prefix in prefixes:
directory = os.path.join(prefix, u'share', u'openlp')
if os.path.exists(directory):
return directory
return os.path.join(u'/usr', u'share', u'openlp')
if XDG_BASE_AVAILABLE:
if dir_type == AppLocation.ConfigDir:
return os.path.join(unicode(BaseDirectory.xdg_config_home, encoding), u'openlp')
elif dir_type == AppLocation.DataDir:
return os.path.join(unicode(BaseDirectory.xdg_data_home, encoding), u'openlp')
elif dir_type == AppLocation.CacheDir:
return os.path.join(unicode(BaseDirectory.xdg_cache_home, encoding), u'openlp')
if dir_type == AppLocation.DataDir:
return os.path.join(unicode(os.getenv(u'HOME'), encoding), u'.openlp', u'data')
return os.path.join(unicode(os.getenv(u'HOME'), encoding), u'.openlp')
def _get_frozen_path(frozen_option, non_frozen_option):
"""
@ -497,10 +395,11 @@ def locale_compare(string1, string2):
locale_direct_compare = locale.strcoll
from applocation import AppLocation
from languagemanager import LanguageManager
from actions import ActionList
from settingsmanager import SettingsManager
__all__ = [u'AppLocation', u'ActionList', u'LanguageManager', u'SettingsManager', u'get_application_version',
u'check_latest_version', u'add_actions', u'get_filesystem_encoding', u'get_web_page', u'get_uno_command',
u'get_uno_instance', u'delete_file', u'clean_filename', u'format_time', u'locale_compare', u'locale_direct_compare']
__all__ = [u'AppLocation', u'ActionList', u'LanguageManager', u'get_application_version', u'check_latest_version',
u'add_actions', u'get_filesystem_encoding', u'get_web_page', u'get_uno_command', u'get_uno_instance',
u'delete_file', u'clean_filename', u'format_time', u'locale_compare', u'locale_direct_compare']

View File

@ -0,0 +1,184 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# This program 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; version 2 of the License. #
# #
# 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., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`openlp.core.utils` module provides the utility libraries for OpenLP.
"""
import logging
import os
import re
import sys
import urllib2
from openlp.core.lib import Registry, Settings
from openlp.core.utils import _get_frozen_path
if sys.platform != u'win32' and sys.platform != u'darwin':
try:
from xdg import BaseDirectory
XDG_BASE_AVAILABLE = True
except ImportError:
XDG_BASE_AVAILABLE = False
import openlp
from openlp.core.lib import translate, check_directory_exists
log = logging.getLogger(__name__)
class AppLocation(object):
"""
The :class:`AppLocation` class is a static class which retrieves a
directory based on the directory type.
"""
AppDir = 1
ConfigDir = 2
DataDir = 3
PluginsDir = 4
VersionDir = 5
CacheDir = 6
LanguageDir = 7
# Base path where data/config/cache dir is located
BaseDir = None
@staticmethod
def get_directory(dir_type=1):
"""
Return the appropriate directory according to the directory type.
``dir_type``
The directory type you want, for instance the data directory.
"""
if dir_type == AppLocation.AppDir:
return _get_frozen_path(os.path.abspath(os.path.split(sys.argv[0])[0]), os.path.split(openlp.__file__)[0])
elif dir_type == AppLocation.PluginsDir:
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
return _get_frozen_path(os.path.join(app_path, u'plugins'),
os.path.join(os.path.split(openlp.__file__)[0], u'plugins'))
elif dir_type == AppLocation.VersionDir:
return _get_frozen_path(os.path.abspath(os.path.split(sys.argv[0])[0]), os.path.split(openlp.__file__)[0])
elif dir_type == AppLocation.LanguageDir:
app_path = _get_frozen_path(os.path.abspath(os.path.split(sys.argv[0])[0]), _get_os_dir_path(dir_type))
return os.path.join(app_path, u'i18n')
elif dir_type == AppLocation.DataDir and AppLocation.BaseDir:
return os.path.join(AppLocation.BaseDir, 'data')
else:
return _get_os_dir_path(dir_type)
@staticmethod
def get_data_path():
"""
Return the path OpenLP stores all its data under.
"""
# Check if we have a different data location.
if Settings().contains(u'advanced/data path'):
path = Settings().value(u'advanced/data path')
else:
path = AppLocation.get_directory(AppLocation.DataDir)
check_directory_exists(path)
return os.path.normpath(path)
@staticmethod
def get_files(section=None, extension=None):
"""
Get a list of files from the data files path.
``section``
Defaults to *None*. The section of code getting the files - used to load from a section's data subdirectory.
``extension``
Defaults to *None*. The extension to search for.
"""
path = AppLocation.get_data_path()
if section:
path = os.path.join(path, section)
try:
files = os.listdir(path)
except OSError:
return []
if extension:
return [filename for filename in files if extension == os.path.splitext(filename)[1]]
else:
# no filtering required
return files
@staticmethod
def get_section_data_path(section):
"""
Return the path a particular module stores its data under.
"""
data_path = AppLocation.get_data_path()
path = os.path.join(data_path, section)
check_directory_exists(path)
return path
def _get_os_dir_path(dir_type):
"""
Return a path based on which OS and environment we are running in.
"""
encoding = sys.getfilesystemencoding()
if sys.platform == u'win32':
if dir_type == AppLocation.DataDir:
return os.path.join(unicode(os.getenv(u'APPDATA'), encoding), u'openlp', u'data')
elif dir_type == AppLocation.LanguageDir:
return os.path.split(openlp.__file__)[0]
return os.path.join(unicode(os.getenv(u'APPDATA'), encoding), u'openlp')
elif sys.platform == u'darwin':
if dir_type == AppLocation.DataDir:
return os.path.join(unicode(os.getenv(u'HOME'), encoding),
u'Library', u'Application Support', u'openlp', u'Data')
elif dir_type == AppLocation.LanguageDir:
return os.path.split(openlp.__file__)[0]
return os.path.join(unicode(os.getenv(u'HOME'), encoding), u'Library', u'Application Support', u'openlp')
else:
if dir_type == AppLocation.LanguageDir:
prefixes = [u'/usr/local', u'/usr']
for prefix in prefixes:
directory = os.path.join(prefix, u'share', u'openlp')
if os.path.exists(directory):
return directory
return os.path.join(u'/usr', u'share', u'openlp')
if XDG_BASE_AVAILABLE:
if dir_type == AppLocation.ConfigDir:
return os.path.join(unicode(BaseDirectory.xdg_config_home, encoding), u'openlp')
elif dir_type == AppLocation.DataDir:
return os.path.join(unicode(BaseDirectory.xdg_data_home, encoding), u'openlp')
elif dir_type == AppLocation.CacheDir:
return os.path.join(unicode(BaseDirectory.xdg_cache_home, encoding), u'openlp')
if dir_type == AppLocation.DataDir:
return os.path.join(unicode(os.getenv(u'HOME'), encoding), u'.openlp', u'data')
return os.path.join(unicode(os.getenv(u'HOME'), encoding), u'.openlp')

View File

@ -1,66 +0,0 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
# --------------------------------------------------------------------------- #
# This program 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; version 2 of the License. #
# #
# 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., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
Provide handling for persisting OpenLP settings. OpenLP uses QSettings to manage settings persistence. QSettings
provides a single API for saving and retrieving settings from the application but writes to disk in an OS dependant
format.
"""
import os
from openlp.core.utils import AppLocation
class SettingsManager(object):
"""
Class to provide helper functions for the loading and saving of application settings.
"""
@staticmethod
def get_files(section=None, extension=None):
"""
Get a list of files from the data files path.
``section``
Defaults to *None*. The section of code getting the files - used to load from a section's data subdirectory.
``extension``
Defaults to *None*. The extension to search for.
"""
path = AppLocation.get_data_path()
if section:
path = os.path.join(path, section)
try:
files = os.listdir(path)
except OSError:
return []
if extension:
return [filename for filename in files if extension == os.path.splitext(filename)[1]]
else:
# no filtering required
return files

View File

@ -31,7 +31,7 @@ import logging
import os
from openlp.core.lib import Registry, Settings, translate
from openlp.core.utils import AppLocation, SettingsManager, delete_file
from openlp.core.utils import AppLocation, delete_file
from openlp.plugins.bibles.lib import parse_reference, get_reference_separator, LanguageSelection
from openlp.plugins.bibles.lib.db import BibleDB, BibleMeta
from csvbible import CSVBible
@ -137,7 +137,7 @@ class BibleManager(object):
BibleDB class.
"""
log.debug(u'Reload bibles')
files = SettingsManager.get_files(self.settingsSection, self.suffix)
files = AppLocation.get_files(self.settingsSection, self.suffix)
if u'alternative_book_names.sqlite' in files:
files.remove(u'alternative_book_names.sqlite')
log.debug(u'Bible Files %s', files)

View File

@ -35,7 +35,7 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import MediaManagerItem, ItemCapabilities, Registry, ServiceItemContext, Settings, UiStrings, \
build_icon, check_item_selected, check_directory_exists, create_thumb, translate, validate_thumb
from openlp.core.lib.ui import critical_error_message_box
from openlp.core.utils import AppLocation, SettingsManager, delete_file, locale_compare, get_images_filter
from openlp.core.utils import AppLocation, delete_file, locale_compare, get_images_filter
log = logging.getLogger(__name__)
@ -106,7 +106,7 @@ class ImageMediaItem(MediaManagerItem):
delete_file(os.path.join(self.servicePath, text.text()))
self.listView.takeItem(row)
self.main_window.incrementProgressBar()
SettingsManager.setValue(self.settingsSection + u'/images files', self.getFileList())
AppLocation.setValue(self.settingsSection + u'/images files', self.getFileList())
self.main_window.finishedProgressBar()
self.application.set_normal_cursor()
self.listView.blockSignals(False)

View File

@ -15,10 +15,10 @@ class TestAppLocation(TestCase):
"""
Test the AppLocation.get_data_path() method
"""
with patch(u'openlp.core.utils.Settings') as mocked_class, \
with patch(u'openlp.core.utils.applocation.Settings') as mocked_class, \
patch(u'openlp.core.utils.AppLocation.get_directory') as mocked_get_directory, \
patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists, \
patch(u'openlp.core.utils.os') as mocked_os:
patch(u'openlp.core.utils.applocation.check_directory_exists') as mocked_check_directory_exists, \
patch(u'openlp.core.utils.applocation.os') as mocked_os:
# GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory()
mocked_settings = mocked_class.return_value
mocked_settings.contains.return_value = False
@ -37,8 +37,8 @@ class TestAppLocation(TestCase):
"""
Test the AppLocation.get_data_path() method when a custom location is set in the settings
"""
with patch(u'openlp.core.utils.Settings') as mocked_class,\
patch(u'openlp.core.utils.os') as mocked_os:
with patch(u'openlp.core.utils.applocation.Settings') as mocked_class,\
patch(u'openlp.core.utils.applocation.os') as mocked_os:
# GIVEN: A mocked out Settings class which returns a custom data location
mocked_settings = mocked_class.return_value
mocked_settings.contains.return_value = True
@ -56,7 +56,7 @@ class TestAppLocation(TestCase):
Test the AppLocation.get_section_data_path() method
"""
with patch(u'openlp.core.utils.AppLocation.get_data_path') as mocked_get_data_path, \
patch(u'openlp.core.utils.check_directory_exists') as mocked_check_directory_exists:
patch(u'openlp.core.utils.applocation.check_directory_exists') as mocked_check_directory_exists:
# GIVEN: A mocked out AppLocation.get_data_path()
mocked_get_data_path.return_value = u'test/dir'
mocked_check_directory_exists.return_value = True
@ -70,7 +70,7 @@ class TestAppLocation(TestCase):
"""
Test the AppLocation.get_directory() method for AppLocation.AppDir
"""
with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path:
with patch(u'openlp.core.utils.applocation._get_frozen_path') as mocked_get_frozen_path:
mocked_get_frozen_path.return_value = u'app/dir'
# WHEN: We call AppLocation.get_directory
directory = AppLocation.get_directory(AppLocation.AppDir)
@ -81,10 +81,10 @@ class TestAppLocation(TestCase):
"""
Test the AppLocation.get_directory() method for AppLocation.PluginsDir
"""
with patch(u'openlp.core.utils._get_frozen_path') as mocked_get_frozen_path, \
patch(u'openlp.core.utils.os.path.abspath') as mocked_abspath, \
patch(u'openlp.core.utils.os.path.split') as mocked_split, \
patch(u'openlp.core.utils.sys') as mocked_sys:
with patch(u'openlp.core.utils.applocation._get_frozen_path') as mocked_get_frozen_path, \
patch(u'openlp.core.utils.applocation.os.path.abspath') as mocked_abspath, \
patch(u'openlp.core.utils.applocation.os.path.split') as mocked_split, \
patch(u'openlp.core.utils.applocation.sys') as mocked_sys:
mocked_abspath.return_value = u'plugins/dir'
mocked_split.return_value = [u'openlp']
mocked_get_frozen_path.return_value = u'plugins/dir'