From 1b168dd7bfef74f739c144ccfdcf1cddc58aae7b Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Sat, 18 Nov 2017 23:14:28 +0000 Subject: [PATCH] More pathlib refactors --- openlp/core/common/__init__.py | 11 ----- openlp/core/ui/servicemanager.py | 16 ++++--- .../openlp_core/common/test_init.py | 43 +------------------ .../openlp_core/ui/test_servicemanager.py | 4 +- 4 files changed, 12 insertions(+), 62 deletions(-) diff --git a/openlp/core/common/__init__.py b/openlp/core/common/__init__.py index d280cbde2..99e222041 100644 --- a/openlp/core/common/__init__.py +++ b/openlp/core/common/__init__.py @@ -318,17 +318,6 @@ def get_filesystem_encoding(): return encoding -def split_filename(path): - """ - Return a list of the parts in a given path. - """ - path = os.path.abspath(path) - if not os.path.isfile(path): - return path, '' - else: - return os.path.split(path) - - def delete_file(file_path): """ Deletes a file from the system. diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index 81edd4930..9c943afaf 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -32,7 +32,7 @@ from tempfile import mkstemp from PyQt5 import QtCore, QtGui, QtWidgets -from openlp.core.common import ThemeLevel, split_filename, delete_file +from openlp.core.common import ThemeLevel, delete_file from openlp.core.common.actions import ActionList, CategoryOrder from openlp.core.common.applocation import AppLocation from openlp.core.common.i18n import UiStrings, format_time, translate @@ -319,7 +319,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.service_id = 0 # is a new service and has not been saved self._modified = False - self._file_name = '' + self._service_path = None self.service_has_all_original_files = True self.list_double_clicked = False @@ -366,7 +366,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi :param openlp.core.common.path.Path file_path: The service file name :rtype: None """ - self._file_name = path_to_str(file_path) + self._service_path = file_path self.main_window.set_service_modified(self.is_modified(), self.short_file_name()) Settings().setValue('servicemanager/last file', file_path) if file_path and file_path.suffix == '.oszl': @@ -377,14 +377,16 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi def file_name(self): """ Return the current file name including path. + + :rtype: openlp.core.common.path.Path """ - return self._file_name + return self._service_path def short_file_name(self): """ Return the current file name, excluding the path. """ - return split_filename(self._file_name)[1] + return self._service_path.name def reset_supported_suffixes(self): """ @@ -706,13 +708,13 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi default_file_path = directory_path / default_file_path lite_filter = translate('OpenLP.ServiceManager', 'OpenLP Service Files - lite (*.oszl)') packaged_filter = translate('OpenLP.ServiceManager', 'OpenLP Service Files (*.osz)') - if self._file_name.endswith('oszl'): + if self._service_path and self._service_path.suffix == '.oszl': default_filter = lite_filter else: default_filter = packaged_filter # SaveAs from osz to oszl is not valid as the files will be deleted on exit which is not sensible or usable in # the long term. - if self._file_name.endswith('oszl') or self.service_has_all_original_files: + if self._service_path and self._service_path.suffix == '.oszl' or self.service_has_all_original_files: file_path, filter_used = FileDialog.getSaveFileName( self.main_window, UiStrings().SaveService, default_file_path, '{packaged};; {lite}'.format(packaged=packaged_filter, lite=lite_filter), diff --git a/tests/functional/openlp_core/common/test_init.py b/tests/functional/openlp_core/common/test_init.py index 5acfe59bb..9b86c99aa 100644 --- a/tests/functional/openlp_core/common/test_init.py +++ b/tests/functional/openlp_core/common/test_init.py @@ -28,7 +28,7 @@ from unittest import TestCase from unittest.mock import MagicMock, PropertyMock, call, patch from openlp.core.common import add_actions, clean_filename, delete_file, get_file_encoding, get_filesystem_encoding, \ - get_uno_command, get_uno_instance, split_filename + get_uno_command, get_uno_instance from openlp.core.common.path import Path from tests.helpers.testmixin import TestMixin @@ -236,47 +236,6 @@ class TestInit(TestCase, TestMixin): mocked_getdefaultencoding.assert_called_with() self.assertEqual('utf-8', result, 'The result should be "utf-8"') - def test_split_filename_with_file_path(self): - """ - Test the split_filename() function with a path to a file - """ - # GIVEN: A path to a file. - if os.name == 'nt': - file_path = 'C:\\home\\user\\myfile.txt' - wanted_result = ('C:\\home\\user', 'myfile.txt') - else: - file_path = '/home/user/myfile.txt' - wanted_result = ('/home/user', 'myfile.txt') - with patch('openlp.core.common.os.path.isfile') as mocked_is_file: - mocked_is_file.return_value = True - - # WHEN: Split the file name. - result = split_filename(file_path) - - # THEN: A tuple should be returned. - self.assertEqual(wanted_result, result, 'A tuple with the dir and file name should have been returned') - - def test_split_filename_with_dir_path(self): - """ - Test the split_filename() function with a path to a directory - """ - # GIVEN: A path to a dir. - if os.name == 'nt': - file_path = 'C:\\home\\user\\mydir' - wanted_result = ('C:\\home\\user\\mydir', '') - else: - file_path = '/home/user/mydir' - wanted_result = ('/home/user/mydir', '') - with patch('openlp.core.common.os.path.isfile') as mocked_is_file: - mocked_is_file.return_value = False - - # WHEN: Split the file name. - result = split_filename(file_path) - - # THEN: A tuple should be returned. - self.assertEqual(wanted_result, result, - 'A two-entry tuple with the directory and file name (empty) should have been returned.') - def test_clean_filename(self): """ Test the clean_filename() function diff --git a/tests/functional/openlp_core/ui/test_servicemanager.py b/tests/functional/openlp_core/ui/test_servicemanager.py index adf241d35..3c0958506 100644 --- a/tests/functional/openlp_core/ui/test_servicemanager.py +++ b/tests/functional/openlp_core/ui/test_servicemanager.py @@ -637,7 +637,7 @@ class TestServiceManager(TestCase): Registry().register('main_window', mocked_main_window) Registry().register('application', MagicMock()) service_manager = ServiceManager(None) - service_manager._file_name = os.path.join('temp', 'filename.osz') + service_manager._service_path = os.path.join('temp', 'filename.osz') service_manager._save_lite = False service_manager.service_items = [] service_manager.service_theme = 'Default' @@ -666,7 +666,7 @@ class TestServiceManager(TestCase): Registry().register('main_window', mocked_main_window) Registry().register('application', MagicMock()) service_manager = ServiceManager(None) - service_manager._file_name = os.path.join('temp', 'filename.osz') + service_manager._service_path = os.path.join('temp', 'filename.osz') service_manager._save_lite = False service_manager.service_items = [] service_manager.service_theme = 'Default'