More pathlib refactors

This commit is contained in:
Phill Ridout 2017-11-18 23:14:28 +00:00
parent a79ec45055
commit 1b168dd7bf
4 changed files with 12 additions and 62 deletions

View File

@ -318,17 +318,6 @@ def get_filesystem_encoding():
return 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): def delete_file(file_path):
""" """
Deletes a file from the system. Deletes a file from the system.

View File

@ -32,7 +32,7 @@ from tempfile import mkstemp
from PyQt5 import QtCore, QtGui, QtWidgets 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.actions import ActionList, CategoryOrder
from openlp.core.common.applocation import AppLocation from openlp.core.common.applocation import AppLocation
from openlp.core.common.i18n import UiStrings, format_time, translate 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 self.service_id = 0
# is a new service and has not been saved # is a new service and has not been saved
self._modified = False self._modified = False
self._file_name = '' self._service_path = None
self.service_has_all_original_files = True self.service_has_all_original_files = True
self.list_double_clicked = False 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 :param openlp.core.common.path.Path file_path: The service file name
:rtype: None :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()) self.main_window.set_service_modified(self.is_modified(), self.short_file_name())
Settings().setValue('servicemanager/last file', file_path) Settings().setValue('servicemanager/last file', file_path)
if file_path and file_path.suffix == '.oszl': if file_path and file_path.suffix == '.oszl':
@ -377,14 +377,16 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi
def file_name(self): def file_name(self):
""" """
Return the current file name including path. 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): def short_file_name(self):
""" """
Return the current file name, excluding the path. 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): 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 default_file_path = directory_path / default_file_path
lite_filter = translate('OpenLP.ServiceManager', 'OpenLP Service Files - lite (*.oszl)') lite_filter = translate('OpenLP.ServiceManager', 'OpenLP Service Files - lite (*.oszl)')
packaged_filter = translate('OpenLP.ServiceManager', 'OpenLP Service Files (*.osz)') 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 default_filter = lite_filter
else: else:
default_filter = packaged_filter 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 # 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. # 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( file_path, filter_used = FileDialog.getSaveFileName(
self.main_window, UiStrings().SaveService, default_file_path, self.main_window, UiStrings().SaveService, default_file_path,
'{packaged};; {lite}'.format(packaged=packaged_filter, lite=lite_filter), '{packaged};; {lite}'.format(packaged=packaged_filter, lite=lite_filter),

View File

@ -28,7 +28,7 @@ from unittest import TestCase
from unittest.mock import MagicMock, PropertyMock, call, patch 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, \ 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 openlp.core.common.path import Path
from tests.helpers.testmixin import TestMixin from tests.helpers.testmixin import TestMixin
@ -236,47 +236,6 @@ class TestInit(TestCase, TestMixin):
mocked_getdefaultencoding.assert_called_with() mocked_getdefaultencoding.assert_called_with()
self.assertEqual('utf-8', result, 'The result should be "utf-8"') 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): def test_clean_filename(self):
""" """
Test the clean_filename() function Test the clean_filename() function

View File

@ -637,7 +637,7 @@ class TestServiceManager(TestCase):
Registry().register('main_window', mocked_main_window) Registry().register('main_window', mocked_main_window)
Registry().register('application', MagicMock()) Registry().register('application', MagicMock())
service_manager = ServiceManager(None) 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._save_lite = False
service_manager.service_items = [] service_manager.service_items = []
service_manager.service_theme = 'Default' service_manager.service_theme = 'Default'
@ -666,7 +666,7 @@ class TestServiceManager(TestCase):
Registry().register('main_window', mocked_main_window) Registry().register('main_window', mocked_main_window)
Registry().register('application', MagicMock()) Registry().register('application', MagicMock())
service_manager = ServiceManager(None) 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._save_lite = False
service_manager.service_items = [] service_manager.service_items = []
service_manager.service_theme = 'Default' service_manager.service_theme = 'Default'