2013-02-27 11:24:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2016-12-31 11:01:36 +00:00
|
|
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
2013-02-27 11:24:50 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
2013-10-13 13:51:13 +00:00
|
|
|
The :mod:`openlp.core.common.applocation` module provides an utility for OpenLP receiving the data path etc.
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2014-08-27 23:18:06 +00:00
|
|
|
from openlp.core.common import Settings, is_win, is_macosx
|
2017-08-25 20:03:25 +00:00
|
|
|
from openlp.core.common.path import Path
|
2013-10-13 20:36:42 +00:00
|
|
|
|
2014-08-27 23:18:06 +00:00
|
|
|
if not is_win() and not is_macosx():
|
2013-02-27 11:24:50 +00:00
|
|
|
try:
|
|
|
|
from xdg import BaseDirectory
|
|
|
|
XDG_BASE_AVAILABLE = True
|
|
|
|
except ImportError:
|
|
|
|
XDG_BASE_AVAILABLE = False
|
|
|
|
|
|
|
|
import openlp
|
2013-10-13 17:02:12 +00:00
|
|
|
from openlp.core.common import check_directory_exists, get_frozen_path
|
2013-02-27 11:24:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2017-08-01 20:59:41 +00:00
|
|
|
FROZEN_APP_PATH = Path(sys.argv[0]).parent
|
|
|
|
APP_PATH = Path(openlp.__file__).parent
|
|
|
|
|
2013-02-27 11:24:50 +00:00
|
|
|
|
|
|
|
class AppLocation(object):
|
|
|
|
"""
|
2013-07-06 19:21:21 +00:00
|
|
|
The :class:`AppLocation` class is a static class which retrieves a directory based on the directory type.
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
|
|
|
AppDir = 1
|
2013-07-06 19:21:21 +00:00
|
|
|
DataDir = 2
|
|
|
|
PluginsDir = 3
|
|
|
|
VersionDir = 4
|
|
|
|
CacheDir = 5
|
|
|
|
LanguageDir = 6
|
2013-02-27 11:24:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2013-07-06 19:21:21 +00:00
|
|
|
def get_directory(dir_type=AppDir):
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
|
|
|
Return the appropriate directory according to the directory type.
|
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param dir_type: The directory type you want, for instance the data directory. Default *AppLocation.AppDir*
|
2017-08-01 20:59:41 +00:00
|
|
|
:return: The requested path
|
2017-08-25 20:03:25 +00:00
|
|
|
:rtype: openlp.core.common.path.Path
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
2017-08-01 20:59:41 +00:00
|
|
|
if dir_type == AppLocation.AppDir or dir_type == AppLocation.VersionDir:
|
|
|
|
return get_frozen_path(FROZEN_APP_PATH, APP_PATH)
|
2013-02-27 11:24:50 +00:00
|
|
|
elif dir_type == AppLocation.PluginsDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
return get_frozen_path(FROZEN_APP_PATH, APP_PATH) / 'plugins'
|
2013-02-27 11:24:50 +00:00
|
|
|
elif dir_type == AppLocation.LanguageDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
return get_frozen_path(FROZEN_APP_PATH, _get_os_dir_path(dir_type)) / 'i18n'
|
2013-02-27 11:24:50 +00:00
|
|
|
else:
|
|
|
|
return _get_os_dir_path(dir_type)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_data_path():
|
|
|
|
"""
|
|
|
|
Return the path OpenLP stores all its data under.
|
2017-08-02 06:09:38 +00:00
|
|
|
|
2017-08-01 20:59:41 +00:00
|
|
|
:return: The data path to use.
|
2017-08-25 20:03:25 +00:00
|
|
|
:rtype: openlp.core.common.path.Path
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
|
|
|
# Check if we have a different data location.
|
2013-08-31 18:17:38 +00:00
|
|
|
if Settings().contains('advanced/data path'):
|
2017-08-26 15:06:11 +00:00
|
|
|
path = Settings().value('advanced/data path')
|
2013-02-27 11:24:50 +00:00
|
|
|
else:
|
|
|
|
path = AppLocation.get_directory(AppLocation.DataDir)
|
2017-08-12 17:45:56 +00:00
|
|
|
check_directory_exists(path)
|
2017-08-01 20:59:41 +00:00
|
|
|
return path
|
2013-02-27 11:24:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2017-08-01 20:59:41 +00:00
|
|
|
def get_files(section=None, extension=''):
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
|
|
|
Get a list of files from the data files path.
|
2017-08-02 06:09:38 +00:00
|
|
|
|
2017-08-18 19:34:20 +00:00
|
|
|
:param None | str section: Defaults to *None*. The section of code getting the files - used to load from a
|
|
|
|
section's data subdirectory.
|
|
|
|
:param str extension: Defaults to ''. The extension to search for. For example::
|
2017-08-01 20:59:41 +00:00
|
|
|
'.png'
|
|
|
|
:return: List of files found.
|
2017-08-25 20:03:25 +00:00
|
|
|
:rtype: list[openlp.core.common.path.Path]
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
|
|
|
path = AppLocation.get_data_path()
|
|
|
|
if section:
|
2017-08-01 20:59:41 +00:00
|
|
|
path = path / section
|
2013-02-27 11:24:50 +00:00
|
|
|
try:
|
2017-08-01 20:59:41 +00:00
|
|
|
file_paths = path.glob('*' + extension)
|
2017-08-01 21:06:52 +00:00
|
|
|
return [file_path.relative_to(path) for file_path in file_paths]
|
2013-02-27 11:24:50 +00:00
|
|
|
except OSError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_section_data_path(section):
|
|
|
|
"""
|
|
|
|
Return the path a particular module stores its data under.
|
2017-08-01 20:59:41 +00:00
|
|
|
|
2017-08-26 15:06:11 +00:00
|
|
|
:param str section:
|
2017-08-25 20:03:25 +00:00
|
|
|
:rtype: openlp.core.common.path.Path
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
2017-08-01 20:59:41 +00:00
|
|
|
path = AppLocation.get_data_path() / section
|
2017-08-12 17:45:56 +00:00
|
|
|
check_directory_exists(path)
|
2013-02-27 11:24:50 +00:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def _get_os_dir_path(dir_type):
|
|
|
|
"""
|
|
|
|
Return a path based on which OS and environment we are running in.
|
2017-08-02 06:09:38 +00:00
|
|
|
|
2017-08-01 20:59:41 +00:00
|
|
|
:param dir_type: AppLocation Enum of the requested path type
|
|
|
|
:return: The requested path
|
2017-08-25 20:03:25 +00:00
|
|
|
:rtype: openlp.core.common.path.Path
|
2013-02-27 11:24:50 +00:00
|
|
|
"""
|
2014-05-22 13:40:05 +00:00
|
|
|
# If running from source, return the language directory from the source directory
|
|
|
|
if dir_type == AppLocation.LanguageDir:
|
2017-08-18 19:34:20 +00:00
|
|
|
directory = Path(openlp.__file__, '..', '..').resolve() / 'resources'
|
2017-08-01 20:59:41 +00:00
|
|
|
if directory.exists():
|
2014-05-22 13:40:05 +00:00
|
|
|
return directory
|
2014-08-27 23:18:06 +00:00
|
|
|
if is_win():
|
2017-08-01 20:59:41 +00:00
|
|
|
openlp_folder_path = Path(os.getenv('APPDATA'), 'openlp')
|
2013-02-27 11:24:50 +00:00
|
|
|
if dir_type == AppLocation.DataDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
return openlp_folder_path / 'data'
|
2013-02-27 11:24:50 +00:00
|
|
|
elif dir_type == AppLocation.LanguageDir:
|
2017-08-18 19:34:20 +00:00
|
|
|
return Path(openlp.__file__).parent
|
2017-08-01 20:59:41 +00:00
|
|
|
return openlp_folder_path
|
2014-08-27 23:18:06 +00:00
|
|
|
elif is_macosx():
|
2017-08-01 20:59:41 +00:00
|
|
|
openlp_folder_path = Path(os.getenv('HOME'), 'Library', 'Application Support', 'openlp')
|
2013-02-27 11:24:50 +00:00
|
|
|
if dir_type == AppLocation.DataDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
return openlp_folder_path / 'Data'
|
2013-02-27 11:24:50 +00:00
|
|
|
elif dir_type == AppLocation.LanguageDir:
|
2017-08-18 19:34:20 +00:00
|
|
|
return Path(openlp.__file__).parent
|
2017-08-01 20:59:41 +00:00
|
|
|
return openlp_folder_path
|
2013-02-27 11:24:50 +00:00
|
|
|
else:
|
|
|
|
if dir_type == AppLocation.LanguageDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
directory = Path('/usr', 'local', 'share', 'openlp')
|
|
|
|
if directory.exists():
|
|
|
|
return directory
|
|
|
|
return Path('/usr', 'share', 'openlp')
|
2013-02-27 11:24:50 +00:00
|
|
|
if XDG_BASE_AVAILABLE:
|
2017-08-01 20:59:41 +00:00
|
|
|
if dir_type == AppLocation.DataDir or dir_type == AppLocation.CacheDir:
|
|
|
|
return Path(BaseDirectory.xdg_data_home, 'openlp')
|
2013-02-27 11:24:50 +00:00
|
|
|
elif dir_type == AppLocation.CacheDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
return Path(BaseDirectory.xdg_cache_home, 'openlp')
|
2013-02-27 11:24:50 +00:00
|
|
|
if dir_type == AppLocation.DataDir:
|
2017-08-01 20:59:41 +00:00
|
|
|
return Path(os.getenv('HOME'), '.openlp', 'data')
|
|
|
|
return Path(os.getenv('HOME'), '.openlp')
|