forked from openlp/openlp
move methods and clean up
This commit is contained in:
parent
b7da0be71e
commit
3e8e72be85
@ -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):
|
||||
@ -327,3 +330,44 @@ def delete_file(file_path_name):
|
||||
except (IOError, OSError):
|
||||
log.exception("Unable to delete file %s" % file_path_name)
|
||||
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))
|
@ -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)
|
||||
|
||||
|
||||
|
@ -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__)
|
||||
|
||||
|
@ -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__)
|
||||
|
@ -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']
|
||||
|
@ -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
|
||||
|
@ -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__)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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__)
|
||||
|
@ -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))
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user