From 7a70550c206e39835e515b4ff049c937a1142361 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Sun, 2 Sep 2018 21:21:57 +0200 Subject: [PATCH 1/3] Make it possible to set the data-path for portable data. --- openlp/core/app.py | 9 +++++++-- tests/functional/openlp_core/test_app.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/openlp/core/app.py b/openlp/core/app.py index 4e0d4842a..4a2a3a867 100644 --- a/openlp/core/app.py +++ b/openlp/core/app.py @@ -38,7 +38,7 @@ from PyQt5 import QtCore, QtWidgets from openlp.core.common import is_macosx, is_win from openlp.core.common.applocation import AppLocation from openlp.core.common.i18n import LanguageManager, UiStrings, translate -from openlp.core.common.path import create_paths, copytree +from openlp.core.common.path import create_paths, copytree, str_to_path from openlp.core.common.registry import Registry from openlp.core.common.settings import Settings from openlp.core.display.screens import ScreenList @@ -295,6 +295,8 @@ def parse_options(args=None): help='Set logging to LEVEL level. Valid values are "debug", "info", "warning".') parser.add_argument('-p', '--portable', dest='portable', action='store_true', help='Specify if this should be run as a portable app, ') + parser.add_argument('-pp', '--portable-path', dest='portablepath', default=None, + help='Specify the path of the portable data, defaults to "/../../".') parser.add_argument('-w', '--no-web-server', dest='no_web_server', action='store_true', help='Turn off the Web and Socket Server ') parser.add_argument('rargs', nargs='?', default=[]) @@ -350,7 +352,10 @@ def main(args=None): application.setApplicationName('OpenLPPortable') Settings.setDefaultFormat(Settings.IniFormat) # Get location OpenLPPortable.ini - portable_path = (AppLocation.get_directory(AppLocation.AppDir) / '..' / '..').resolve() + if args.portablepath: + portable_path = str_to_path(args.portablepath) + else: + portable_path = (AppLocation.get_directory(AppLocation.AppDir) / '..' / '..').resolve() data_path = portable_path / 'Data' set_up_logging(portable_path / 'Other') log.info('Running portable') diff --git a/tests/functional/openlp_core/test_app.py b/tests/functional/openlp_core/test_app.py index 5b0a0d885..5e327aa36 100644 --- a/tests/functional/openlp_core/test_app.py +++ b/tests/functional/openlp_core/test_app.py @@ -81,6 +81,24 @@ def test_parse_options_debug_and_portable(): assert args.rargs == [], 'The service file should be blank' +def test_parse_options_portable_and_portable_path(): + """ + Test the parse options process works portable and portable-path + """ + # GIVEN: a a set of system arguments. + sys.argv[1:] = ['--portable', '--portable-path .'] + + # WHEN: We we parse them to expand to options + args = parse_options() + + # THEN: the following fields will have been extracted. + assert args.loglevel == 'warning', 'The log level should be set to warning' + assert args.no_error_form is False, 'The no_error_form should be set to False' + assert args.portable is True, 'The portable flag should be set to true' + assert args.portablepath == '.', 'The portable path should be set to "."' + assert args.rargs == [], 'The service file should be blank' + + def test_parse_options_all_no_file(): """ Test the parse options process works with two options From b3c1996e4fa9541248b564ba4ae84dda5a11e0e0 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Tue, 30 Apr 2019 21:43:41 +0200 Subject: [PATCH 2/3] Handle absolute and relative paths. --- openlp/core/app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/openlp/core/app.py b/openlp/core/app.py index 7317155cb..4a081c3ce 100644 --- a/openlp/core/app.py +++ b/openlp/core/app.py @@ -30,6 +30,7 @@ import argparse import logging import sys import time +import os from datetime import datetime from traceback import format_exception @@ -359,7 +360,11 @@ def main(args=None): Settings.setDefaultFormat(Settings.IniFormat) # Get location OpenLPPortable.ini if args.portablepath: - portable_path = str_to_path(args.portablepath) + if os.path.isabs(args.portablepath): + portable_path = str_to_path(args.portablepath).resolve() + else: + portable_path = (AppLocation.get_directory(AppLocation.AppDir) / '..' / + str_to_path(args.portablepath)).resolve() else: portable_path = (AppLocation.get_directory(AppLocation.AppDir) / '..' / '..').resolve() data_path = portable_path / 'Data' From 85879122ab4f2e604b7ca59172ed60059d78a0ce Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Wed, 1 May 2019 21:19:21 +0200 Subject: [PATCH 3/3] Implement suggestions and fix test. --- openlp/core/app.py | 13 +++++++------ tests/functional/openlp_core/test_app.py | 9 +++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/openlp/core/app.py b/openlp/core/app.py index 4a081c3ce..80168a148 100644 --- a/openlp/core/app.py +++ b/openlp/core/app.py @@ -41,7 +41,7 @@ from openlp.core.common import is_macosx, is_win from openlp.core.common.applocation import AppLocation from openlp.core.loader import loader from openlp.core.common.i18n import LanguageManager, UiStrings, translate -from openlp.core.common.path import copytree, create_paths, str_to_path +from openlp.core.common.path import copytree, create_paths, Path from openlp.core.common.registry import Registry from openlp.core.common.settings import Settings from openlp.core.display.screens import ScreenList @@ -303,7 +303,8 @@ def parse_options(args=None): parser.add_argument('-p', '--portable', dest='portable', action='store_true', help='Specify if this should be run as a portable app, ') parser.add_argument('-pp', '--portable-path', dest='portablepath', default=None, - help='Specify the path of the portable data, defaults to "/../../".') + help='Specify the path of the portable data, defaults to "{dir_name}".'.format( + dir_name=os.path.join('', '..', '..'))) parser.add_argument('-w', '--no-web-server', dest='no_web_server', action='store_true', help='Turn off the Web and Socket Server ') parser.add_argument('rargs', nargs='?', default=[]) @@ -361,12 +362,12 @@ def main(args=None): # Get location OpenLPPortable.ini if args.portablepath: if os.path.isabs(args.portablepath): - portable_path = str_to_path(args.portablepath).resolve() + portable_path = Path(args.portablepath) else: - portable_path = (AppLocation.get_directory(AppLocation.AppDir) / '..' / - str_to_path(args.portablepath)).resolve() + portable_path = AppLocation.get_directory(AppLocation.AppDir) / '..' / args.portablepath else: - portable_path = (AppLocation.get_directory(AppLocation.AppDir) / '..' / '..').resolve() + portable_path = AppLocation.get_directory(AppLocation.AppDir) / '..' / '..' + portable_path = portable_path.resolve() data_path = portable_path / 'Data' set_up_logging(portable_path / 'Other') log.info('Running portable') diff --git a/tests/functional/openlp_core/test_app.py b/tests/functional/openlp_core/test_app.py index 90d5d9082..3bbeb105b 100644 --- a/tests/functional/openlp_core/test_app.py +++ b/tests/functional/openlp_core/test_app.py @@ -29,6 +29,7 @@ from PyQt5 import QtCore, QtWidgets sys.modules['PyQt5.QtWebEngineWidgets'] = MagicMock() from openlp.core.app import OpenLP, parse_options +from openlp.core.common import is_win from openlp.core.common.settings import Settings from tests.utils.constants import RESOURCE_PATH @@ -89,7 +90,11 @@ def test_parse_options_portable_and_portable_path(): Test the parse options process works portable and portable-path """ # GIVEN: a a set of system arguments. - sys.argv[1:] = ['--portable', '--portable-path .'] + if is_win(): + data_path = 'c:\\temp\\openlp-data' + else: + data_path = '/tmp/openlp-data' + sys.argv[1:] = ['--portable', '--portable-path', '{datapath}'.format(datapath=data_path)] # WHEN: We we parse them to expand to options args = parse_options() @@ -98,7 +103,7 @@ def test_parse_options_portable_and_portable_path(): assert args.loglevel == 'warning', 'The log level should be set to warning' assert args.no_error_form is False, 'The no_error_form should be set to False' assert args.portable is True, 'The portable flag should be set to true' - assert args.portablepath == '.', 'The portable path should be set to "."' + assert args.portablepath == data_path, 'The portable path should be set as expected' assert args.rargs == [], 'The service file should be blank'