Moved all "get_xxxxx_directory" functions into one "AppLocation" class.

Set the plugin manager to use this new AppLocation class as well.
This commit is contained in:
Raoul Snyman 2010-02-21 14:43:03 +02:00
parent 174a85d969
commit 6f4b4b2616
5 changed files with 58 additions and 43 deletions

View File

@ -35,7 +35,7 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import Receiver, str_to_bool from openlp.core.lib import Receiver, str_to_bool
from openlp.core.resources import qInitResources from openlp.core.resources import qInitResources
from openlp.core.ui import MainWindow, SplashScreen, ScreenList from openlp.core.ui import MainWindow, SplashScreen, ScreenList
from openlp.core.utils import get_config_directory, ConfigHelper from openlp.core.utils import AppLocation, ConfigHelper
log = logging.getLogger() log = logging.getLogger()
@ -160,7 +160,8 @@ def main():
parser.add_option("-s", "--style", dest="style", parser.add_option("-s", "--style", dest="style",
help="Set the Qt4 style (passed directly to Qt4).") help="Set the Qt4 style (passed directly to Qt4).")
# Set up logging # Set up logging
filename = os.path.join(get_config_directory(), u'openlp.log') filename = os.path.join(AppLocation.get_directory(AppLocation.ConfigDir),
u'openlp.log')
logfile = FileHandler(filename, u'w') logfile = FileHandler(filename, u'w')
logfile.setFormatter(logging.Formatter( logfile.setFormatter(logging.Formatter(
u'%(asctime)s %(name)-15s %(levelname)-8s %(message)s')) u'%(asctime)s %(name)-15s %(levelname)-8s %(message)s'))

View File

@ -77,7 +77,7 @@ class PluginManager(object):
if name.endswith(u'.py') and not name.startswith(u'__'): if name.endswith(u'.py') and not name.startswith(u'__'):
path = os.path.abspath(os.path.join(root, name)) path = os.path.abspath(os.path.join(root, name))
thisdepth = len(path.split(os.sep)) thisdepth = len(path.split(os.sep))
if thisdepth-startdepth > 2: if thisdepth - startdepth > 2:
# skip anything lower down # skip anything lower down
continue continue
modulename, pyext = os.path.splitext(path) modulename, pyext = os.path.splitext(path)

View File

@ -34,7 +34,7 @@ from openlp.core.ui import AboutForm, SettingsForm, \
PluginForm, MediaDockManager PluginForm, MediaDockManager
from openlp.core.lib import RenderManager, PluginConfig, build_icon, \ from openlp.core.lib import RenderManager, PluginConfig, build_icon, \
OpenLPDockWidget, SettingsManager, PluginManager, Receiver, str_to_bool OpenLPDockWidget, SettingsManager, PluginManager, Receiver, str_to_bool
from openlp.core.utils import check_latest_version from openlp.core.utils import check_latest_version, AppLocation
media_manager_style = """ media_manager_style = """
QToolBox::tab { QToolBox::tab {
@ -434,9 +434,7 @@ class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
self.aboutForm = AboutForm(self, applicationVersion) self.aboutForm = AboutForm(self, applicationVersion)
self.settingsForm = SettingsForm(self.screens, self, self) self.settingsForm = SettingsForm(self.screens, self, self)
# Set up the path with plugins # Set up the path with plugins
pluginpath = os.path.split(os.path.abspath(__file__))[0] pluginpath = AppLocation.get_directory(AppLocation.PluginsDir)
pluginpath = os.path.abspath(
os.path.join(pluginpath, u'..', u'..', u'plugins'))
self.plugin_manager = PluginManager(pluginpath) self.plugin_manager = PluginManager(pluginpath)
self.plugin_helpers = {} self.plugin_helpers = {}
# Set up the interface # Set up the interface

View File

@ -24,12 +24,60 @@
############################################################################### ###############################################################################
import os import os
import sys
import logging import logging
import urllib2 import urllib2
from datetime import datetime from datetime import datetime
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class AppLocation(object):
"""
Retrieve a directory based on the directory type.
"""
AppDir = 1
ConfigDir = 2
DataDir = 3
PluginsDir = 4
@staticmethod
def get_directory(dir_type):
if dir_type == AppLocation.AppDir:
return os.path.abspath(os.path.split(sys.argv[0])[0])
elif dir_type == AppLocation.ConfigDir:
if os.name == u'nt':
path = os.path.join(os.getenv(u'APPDATA'), u'openlp')
elif os.name == u'mac':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp')
else:
try:
from xdg import BaseDirectory
path = os.path.join(BaseDirectory.xdg_config_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp')
return path
elif dir_type == AppLocation.DataDir:
if os.name == u'nt':
path = os.path.join(os.getenv(u'APPDATA'), u'openlp', u'data')
elif os.name == u'mac':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp', u'Data')
else:
try:
from xdg import BaseDirectory
path = os.path.join(BaseDirectory.xdg_data_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp', u'data')
return path
elif dir_type == AppLocation.PluginsDir:
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
if hasattr(sys, u'frozen') and sys.frozen == 1:
return os.path.join(app_path, u'plugins')
else:
return os.path.join(app_path, u'openlp', u'plugins')
def check_latest_version(config, current_version): def check_latest_version(config, current_version):
version_string = current_version version_string = current_version
#set to prod in the distribution confif file. #set to prod in the distribution confif file.
@ -50,39 +98,7 @@ def check_latest_version(config, current_version):
log.exception(u'Reason for failure: %s', e.reason) log.exception(u'Reason for failure: %s', e.reason)
return version_string return version_string
def get_config_directory():
path = u''
if os.name == u'nt':
path = os.path.join(os.getenv(u'APPDATA'), u'openlp')
elif os.name == u'mac':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp')
else:
try:
from xdg import BaseDirectory
path = os.path.join(BaseDirectory.xdg_config_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp')
return path
def get_data_directory():
path = u''
if os.name == u'nt':
# ask OS for path to application data, set on Windows XP and Vista
path = os.path.join(os.getenv(u'APPDATA'), u'openlp', u'data')
elif os.name == u'mac':
path = os.path.join(os.getenv(u'HOME'), u'Library',
u'Application Support', u'openlp', u'Data')
else:
try:
from xdg import BaseDirectory
path = os.path.join(BaseDirectory.xdg_data_home, u'openlp')
except ImportError:
path = os.path.join(os.getenv(u'HOME'), u'.openlp', u'data')
return path
from registry import Registry from registry import Registry
from confighelper import ConfigHelper from confighelper import ConfigHelper
__all__ = [u'Registry', u'ConfigHelper', u'get_config_directory', __all__ = [u'Registry', u'ConfigHelper', u'AppLocations', u'check_latest_version']
u'get_data_directory', u'check_latest_version']

View File

@ -25,7 +25,7 @@
import os import os
from openlp.core.utils import get_data_directory, get_config_directory from openlp.core.utils import AppLocation
from openlp.core.utils.registry import Registry from openlp.core.utils.registry import Registry
class ConfigHelper(object): class ConfigHelper(object):
@ -36,7 +36,7 @@ class ConfigHelper(object):
@staticmethod @staticmethod
def get_data_path(): def get_data_path():
path = get_data_directory() path = AppLocation.get_directory(AppLocation.DataDir)
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path) os.makedirs(path)
return path return path
@ -70,7 +70,7 @@ class ConfigHelper(object):
current operating system, and returns an instantiation of that class. current operating system, and returns an instantiation of that class.
""" """
if ConfigHelper.__registry__ is None: if ConfigHelper.__registry__ is None:
config_path = get_config_directory() config_path = AppLocation.get_directory(AppLocation.ConfigDir)
ConfigHelper.__registry__ = Registry(config_path) ConfigHelper.__registry__ = Registry(config_path)
return ConfigHelper.__registry__ return ConfigHelper.__registry__