From 3e8e72be851082a7d48b678ab3c7c3ea8f2641f1 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Tue, 5 Apr 2016 18:30:20 +0100 Subject: [PATCH] move methods and clean up --- openlp/core/common/__init__.py | 48 +++++++++++++++- openlp/core/common/languagemanager.py | 1 + openlp/core/ui/advancedtab.py | 3 +- openlp/core/ui/themeform.py | 3 +- openlp/core/utils/__init__.py | 55 +----------------- .../plugins/bibles/forms/bibleimportform.py | 2 +- openlp/plugins/bibles/lib/db.py | 3 +- openlp/plugins/images/lib/mediaitem.py | 3 +- openlp/plugins/songs/lib/__init__.py | 3 +- openlp/plugins/songs/lib/openlyricsexport.py | 3 +- .../openlp_core_utils/test_first_time.py | 57 ------------------- .../openlp_core_utils/test_utils.py | 4 +- .../openlp_core_utils/test_utils.py | 2 +- 13 files changed, 58 insertions(+), 129 deletions(-) delete mode 100644 tests/functional/openlp_core_utils/test_first_time.py diff --git a/openlp/core/common/__init__.py b/openlp/core/common/__init__.py index 7b5c9f3fd..ba10da87c 100644 --- a/openlp/core/common/__init__.py +++ b/openlp/core/common/__init__.py @@ -32,7 +32,7 @@ import traceback from ipaddress import IPv4Address, IPv6Address, AddressValueError from shutil import which -from PyQt5 import QtCore +from PyQt5 import QtCore, QtGui from PyQt5.QtCore import QCryptographicHash as QHash log = logging.getLogger(__name__ + '.__init__') @@ -40,6 +40,9 @@ log = logging.getLogger(__name__ + '.__init__') FIRST_CAMEL_REGEX = re.compile('(.)([A-Z][a-z]+)') SECOND_CAMEL_REGEX = re.compile('([a-z0-9])([A-Z])') +CONTROL_CHARS = re.compile(r'[\x00-\x1F\x7F-\x9F]', re.UNICODE) +INVALID_FILE_CHARS = re.compile(r'[\\/:\*\?"<>\|\+\[\]%]', re.UNICODE) +IMAGES_FILTER = None def trace_error_handler(logger): @@ -326,4 +329,45 @@ def delete_file(file_path_name): return True except (IOError, OSError): log.exception("Unable to delete file %s" % file_path_name) - return False \ No newline at end of file + return False + + +def get_images_filter(): + """ + Returns a filter string for a file dialog containing all the supported image formats. + """ + global IMAGES_FILTER + if not IMAGES_FILTER: + log.debug('Generating images filter.') + formats = list(map(bytes.decode, list(map(bytes, QtGui.QImageReader.supportedImageFormats())))) + visible_formats = '(*.%s)' % '; *.'.join(formats) + actual_formats = '(*.%s)' % ' *.'.join(formats) + IMAGES_FILTER = '%s %s %s' % (translate('OpenLP', 'Image Files'), visible_formats, actual_formats) + return IMAGES_FILTER + + +def is_not_image_file(file_name): + """ + Validate that the file is not an image file. + + :param file_name: File name to be checked. + """ + if not file_name: + return True + else: + formats = [bytes(fmt).decode().lower() for fmt in QtGui.QImageReader.supportedImageFormats()] + file_part, file_extension = os.path.splitext(str(file_name)) + if file_extension[1:].lower() in formats and os.path.exists(file_name): + return False + return True + + +def clean_filename(filename): + """ + Removes invalid characters from the given ``filename``. + + :param filename: The "dirty" file name to clean. + """ + if not isinstance(filename, str): + filename = str(filename, 'utf-8') + return INVALID_FILE_CHARS.sub('_', CONTROL_CHARS.sub('', filename)) \ No newline at end of file diff --git a/openlp/core/common/languagemanager.py b/openlp/core/common/languagemanager.py index c3e736d65..52e9e9f13 100644 --- a/openlp/core/common/languagemanager.py +++ b/openlp/core/common/languagemanager.py @@ -33,6 +33,7 @@ from openlp.core.common import AppLocation, Settings, translate, is_win, is_maco log = logging.getLogger(__name__) +ICU_COLLATOR = None DIGITS_OR_NONDIGITS = re.compile(r'\d+|\D+', re.UNICODE) diff --git a/openlp/core/ui/advancedtab.py b/openlp/core/ui/advancedtab.py index ce26ae808..f32672f58 100644 --- a/openlp/core/ui/advancedtab.py +++ b/openlp/core/ui/advancedtab.py @@ -29,10 +29,9 @@ import sys from PyQt5 import QtCore, QtGui, QtWidgets -from openlp.core.common import AppLocation, Settings, SlideLimits, UiStrings, translate +from openlp.core.common import AppLocation, Settings, SlideLimits, UiStrings, translate, get_images_filter from openlp.core.lib import ColorButton, SettingsTab, build_icon from openlp.core.common.languagemanager import format_time -from openlp.core.utils import get_images_filter log = logging.getLogger(__name__) diff --git a/openlp/core/ui/themeform.py b/openlp/core/ui/themeform.py index cd81ec800..d620a0f79 100644 --- a/openlp/core/ui/themeform.py +++ b/openlp/core/ui/themeform.py @@ -27,11 +27,10 @@ import os from PyQt5 import QtCore, QtGui, QtWidgets -from openlp.core.common import Registry, RegistryProperties, UiStrings, translate +from openlp.core.common import Registry, RegistryProperties, UiStrings, translate, get_images_filter, is_not_image_file from openlp.core.lib.theme import BackgroundType, BackgroundGradientType from openlp.core.lib.ui import critical_error_message_box from openlp.core.ui import ThemeLayoutForm -from openlp.core.utils import get_images_filter, is_not_image_file from .themewizard import Ui_ThemeWizard log = logging.getLogger(__name__) diff --git a/openlp/core/utils/__init__.py b/openlp/core/utils/__init__.py index 0be2338b3..76cfad0e8 100644 --- a/openlp/core/utils/__init__.py +++ b/openlp/core/utils/__init__.py @@ -23,8 +23,6 @@ The :mod:`openlp.core.utils` module provides the utility libraries for OpenLP. """ import logging -import os -import re import socket import sys import time @@ -34,8 +32,6 @@ import urllib.request from http.client import HTTPException from random import randint -from PyQt5 import QtGui - from openlp.core.common import Registry, is_win, is_macosx if not is_win() and not is_macosx(): @@ -46,16 +42,8 @@ if not is_win() and not is_macosx(): BaseDirectory = None XDG_BASE_AVAILABLE = False -from openlp.core.common import translate - log = logging.getLogger(__name__ + '.__init__') -APPLICATION_VERSION = {} -IMAGES_FILTER = None -ICU_COLLATOR = None -CONTROL_CHARS = re.compile(r'[\x00-\x1F\x7F-\x9F]', re.UNICODE) -INVALID_FILE_CHARS = re.compile(r'[\\/:\*\?"<>\|\+\[\]%]', re.UNICODE) -DIGITS_OR_NONDIGITS = re.compile(r'\d+|\D+', re.UNICODE) USER_AGENTS = { 'win32': [ 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36', @@ -112,47 +100,6 @@ class HTTPRedirectHandlerFixed(urllib.request.HTTPRedirectHandler): return super(HTTPRedirectHandlerFixed, self).redirect_request(req, fp, code, msg, headers, fixed_url) -def get_images_filter(): - """ - Returns a filter string for a file dialog containing all the supported image formats. - """ - global IMAGES_FILTER - if not IMAGES_FILTER: - log.debug('Generating images filter.') - formats = list(map(bytes.decode, list(map(bytes, QtGui.QImageReader.supportedImageFormats())))) - visible_formats = '(*.%s)' % '; *.'.join(formats) - actual_formats = '(*.%s)' % ' *.'.join(formats) - IMAGES_FILTER = '%s %s %s' % (translate('OpenLP', 'Image Files'), visible_formats, actual_formats) - return IMAGES_FILTER - - -def is_not_image_file(file_name): - """ - Validate that the file is not an image file. - - :param file_name: File name to be checked. - """ - if not file_name: - return True - else: - formats = [bytes(fmt).decode().lower() for fmt in QtGui.QImageReader.supportedImageFormats()] - file_part, file_extension = os.path.splitext(str(file_name)) - if file_extension[1:].lower() in formats and os.path.exists(file_name): - return False - return True - - -def clean_filename(filename): - """ - Removes invalid characters from the given ``filename``. - - :param filename: The "dirty" file name to clean. - """ - if not isinstance(filename, str): - filename = str(filename, 'utf-8') - return INVALID_FILE_CHARS.sub('_', CONTROL_CHARS.sub('', filename)) - - def _get_user_agent(): """ Return a user agent customised for the platform the user is on. @@ -241,4 +188,4 @@ def get_web_page(url, header=None, update_openlp=False): __all__ = ['get_application_version', 'check_latest_version', - 'get_web_page', 'clean_filename'] + 'get_web_page'] diff --git a/openlp/plugins/bibles/forms/bibleimportform.py b/openlp/plugins/bibles/forms/bibleimportform.py index 676801eb8..27dbea963 100644 --- a/openlp/plugins/bibles/forms/bibleimportform.py +++ b/openlp/plugins/bibles/forms/bibleimportform.py @@ -28,7 +28,7 @@ import urllib.error from PyQt5 import QtWidgets -from openlp.core.common import AppLocation, Settings, UiStrings, translate +from openlp.core.common import AppLocation, Settings, UiStrings, translate, clean_filename from openlp.core.lib.db import delete_database from openlp.core.lib.ui import critical_error_message_box from openlp.core.ui.wizard import OpenLPWizard, WizardStrings diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index b77117e21..8dcf8c042 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -33,10 +33,9 @@ from sqlalchemy.exc import OperationalError from sqlalchemy.orm import class_mapper, mapper, relation from sqlalchemy.orm.exc import UnmappedClassError -from openlp.core.common import Registry, RegistryProperties, AppLocation, translate +from openlp.core.common import Registry, RegistryProperties, AppLocation, translate, clean_filename from openlp.core.lib.db import BaseModel, init_db, Manager from openlp.core.lib.ui import critical_error_message_box -from openlp.core.utils import clean_filename from openlp.plugins.bibles.lib import upgrade log = logging.getLogger(__name__) diff --git a/openlp/plugins/images/lib/mediaitem.py b/openlp/plugins/images/lib/mediaitem.py index 6efcc32df..f35fd48c7 100644 --- a/openlp/plugins/images/lib/mediaitem.py +++ b/openlp/plugins/images/lib/mediaitem.py @@ -26,11 +26,10 @@ import os from PyQt5 import QtCore, QtGui, QtWidgets from openlp.core.common import Registry, AppLocation, Settings, UiStrings, check_directory_exists, translate, \ - delete_file + delete_file, get_images_filter from openlp.core.lib import ItemCapabilities, MediaManagerItem, ServiceItemContext, StringContent, TreeWidgetWithDnD,\ build_icon, check_item_selected, create_thumb, validate_thumb from openlp.core.lib.ui import create_widget_action, critical_error_message_box -from openlp.core.utils import get_images_filter from openlp.core.common.languagemanager import get_locale_key from openlp.plugins.images.forms import AddGroupForm, ChooseGroupForm from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index ce80c4b1e..1d45f52b2 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -29,9 +29,8 @@ import re from PyQt5 import QtWidgets -from openlp.core.common import AppLocation +from openlp.core.common import AppLocation, CONTROL_CHARS from openlp.core.lib import translate -from openlp.core.utils import CONTROL_CHARS from openlp.plugins.songs.lib.db import MediaFile, Song from .db import Author from .ui import SongStrings diff --git a/openlp/plugins/songs/lib/openlyricsexport.py b/openlp/plugins/songs/lib/openlyricsexport.py index a8cffb418..d5ca31e18 100644 --- a/openlp/plugins/songs/lib/openlyricsexport.py +++ b/openlp/plugins/songs/lib/openlyricsexport.py @@ -28,8 +28,7 @@ import os from lxml import etree -from openlp.core.common import RegistryProperties, check_directory_exists, translate -from openlp.core.utils import clean_filename +from openlp.core.common import RegistryProperties, check_directory_exists, translate, clean_filename from openlp.plugins.songs.lib.openlyricsxml import OpenLyrics log = logging.getLogger(__name__) diff --git a/tests/functional/openlp_core_utils/test_first_time.py b/tests/functional/openlp_core_utils/test_first_time.py deleted file mode 100644 index b9a7622a2..000000000 --- a/tests/functional/openlp_core_utils/test_first_time.py +++ /dev/null @@ -1,57 +0,0 @@ -# -*- coding: utf-8 -*- -# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 - -############################################################################### -# OpenLP - Open Source Lyrics Projection # -# --------------------------------------------------------------------------- # -# Copyright (c) 2008-2016 OpenLP Developers # -# --------------------------------------------------------------------------- # -# 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 # -############################################################################### -""" -Package to test the openlp.core.utils.__init__ package. -""" - -from unittest import TestCase -import urllib.request -import urllib.error -import urllib.parse - -from tests.functional import MagicMock, patch -from tests.helpers.testmixin import TestMixin - -from openlp.core.utils import CONNECTION_TIMEOUT, CONNECTION_RETRIES, get_web_page - - -class TestFirstTimeWizard(TestMixin, TestCase): - """ - Test First Time Wizard import functions - """ - def webpage_connection_retry_test(self): - """ - Test get_web_page will attempt CONNECTION_RETRIES+1 connections - bug 1409031 - """ - # GIVEN: Initial settings and mocks - with patch.object(urllib.request, 'urlopen') as mocked_urlopen: - mocked_urlopen.side_effect = ConnectionError - - # WHEN: A webpage is requested - try: - get_web_page(url='http://localhost') - except: - pass - - # THEN: urlopen should have been called CONNECTION_RETRIES + 1 count - self.assertEquals(mocked_urlopen.call_count, CONNECTION_RETRIES + 1, - 'get_web_page() should have tried {} times'.format(CONNECTION_RETRIES)) diff --git a/tests/functional/openlp_core_utils/test_utils.py b/tests/functional/openlp_core_utils/test_utils.py index d0bb07f95..614be7bb9 100644 --- a/tests/functional/openlp_core_utils/test_utils.py +++ b/tests/functional/openlp_core_utils/test_utils.py @@ -25,8 +25,8 @@ Functional tests to test the AppLocation class and related methods. import os from unittest import TestCase -from openlp.core.utils import clean_filename, _get_user_agent, get_web_page -from openlp.core.common import get_filesystem_encoding, split_filename, delete_file +from openlp.core.utils import _get_user_agent, get_web_page +from openlp.core.common import get_filesystem_encoding, split_filename, delete_file, clean_filename from tests.functional import MagicMock, patch diff --git a/tests/interfaces/openlp_core_utils/test_utils.py b/tests/interfaces/openlp_core_utils/test_utils.py index 1124abbb7..2c0d9a572 100644 --- a/tests/interfaces/openlp_core_utils/test_utils.py +++ b/tests/interfaces/openlp_core_utils/test_utils.py @@ -25,7 +25,7 @@ Functional tests to test the AppLocation class and related methods. import os from unittest import TestCase -from openlp.core.utils import is_not_image_file +from openlp.core.common import is_not_image_file from tests.utils.constants import TEST_RESOURCES_PATH from tests.helpers.testmixin import TestMixin