2009-09-08 19:58:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-12-29 09:35:24 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
2008-10-23 19:49:13 +00:00
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2013-12-24 08:56:50 +00:00
|
|
|
# Copyright (c) 2008-2014 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan #
|
2012-06-22 14:14:53 +00:00
|
|
|
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
|
2012-11-11 21:16:14 +00:00
|
|
|
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
|
2012-10-21 13:16:22 +00:00
|
|
|
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
|
|
|
|
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
|
2012-12-01 07:57:54 +00:00
|
|
|
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
|
2009-09-08 19:58:05 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
2013-01-16 21:03:01 +00:00
|
|
|
"""
|
2013-10-13 13:51:13 +00:00
|
|
|
The :mod:`common` module contains most of the components and libraries that make
|
|
|
|
OpenLP work.
|
2013-01-16 21:03:01 +00:00
|
|
|
"""
|
2014-10-06 19:10:03 +00:00
|
|
|
import hashlib
|
2013-12-13 19:44:17 +00:00
|
|
|
import re
|
2013-10-13 13:51:13 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2013-10-13 17:02:12 +00:00
|
|
|
import sys
|
2013-12-13 19:44:17 +00:00
|
|
|
import traceback
|
2014-10-06 19:10:03 +00:00
|
|
|
from ipaddress import IPv4Address, IPv6Address, AddressValueError
|
|
|
|
from codecs import decode, encode
|
2008-10-23 19:49:13 +00:00
|
|
|
|
2013-10-13 20:36:42 +00:00
|
|
|
from PyQt4 import QtCore
|
2014-10-06 19:10:03 +00:00
|
|
|
from PyQt4.QtCore import QCryptographicHash as QHash
|
2013-10-13 20:36:42 +00:00
|
|
|
|
2014-04-12 20:19:22 +00:00
|
|
|
log = logging.getLogger(__name__ + '.__init__')
|
2013-02-01 19:58:18 +00:00
|
|
|
|
2013-10-13 13:51:13 +00:00
|
|
|
|
2013-12-13 19:44:17 +00:00
|
|
|
FIRST_CAMEL_REGEX = re.compile('(.)([A-Z][a-z]+)')
|
|
|
|
SECOND_CAMEL_REGEX = re.compile('([a-z0-9])([A-Z])')
|
|
|
|
|
|
|
|
|
|
|
|
def trace_error_handler(logger):
|
|
|
|
"""
|
|
|
|
Log the calling path of an exception
|
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param logger: logger to use so traceback is logged to correct class
|
2013-12-13 19:44:17 +00:00
|
|
|
"""
|
2014-04-01 17:32:19 +00:00
|
|
|
log_string = "OpenLP Error trace"
|
2013-12-13 19:44:17 +00:00
|
|
|
for tb in traceback.extract_stack():
|
2014-04-04 20:20:00 +00:00
|
|
|
log_string = '%s\n File %s at line %d \n\t called %s' % (log_string, tb[0], tb[1], tb[3])
|
2014-04-01 17:32:19 +00:00
|
|
|
logger.error(log_string)
|
2013-12-13 19:44:17 +00:00
|
|
|
|
2013-12-15 16:50:09 +00:00
|
|
|
|
2013-10-13 13:51:13 +00:00
|
|
|
def check_directory_exists(directory, do_not_log=False):
|
|
|
|
"""
|
|
|
|
Check a theme directory exists and if not create it
|
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param directory: The directory to make sure exists
|
|
|
|
:param do_not_log: To not log anything. This is need for the start up, when the log isn't ready.
|
2013-10-13 13:51:13 +00:00
|
|
|
"""
|
|
|
|
if not do_not_log:
|
|
|
|
log.debug('check_directory_exists %s' % directory)
|
|
|
|
try:
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
2013-10-13 17:02:12 +00:00
|
|
|
|
|
|
|
def get_frozen_path(frozen_option, non_frozen_option):
|
|
|
|
"""
|
|
|
|
Return a path based on the system status.
|
2014-04-01 23:49:58 +00:00
|
|
|
|
|
|
|
:param frozen_option:
|
|
|
|
:param non_frozen_option:
|
2013-10-13 17:02:12 +00:00
|
|
|
"""
|
|
|
|
if hasattr(sys, 'frozen') and sys.frozen == 1:
|
|
|
|
return frozen_option
|
|
|
|
return non_frozen_option
|
|
|
|
|
2013-10-13 20:36:42 +00:00
|
|
|
|
|
|
|
class ThemeLevel(object):
|
|
|
|
"""
|
|
|
|
Provides an enumeration for the level a theme applies to
|
|
|
|
"""
|
|
|
|
Global = 1
|
|
|
|
Service = 2
|
|
|
|
Song = 3
|
|
|
|
|
|
|
|
|
|
|
|
def translate(context, text, comment=None, encoding=QtCore.QCoreApplication.CodecForTr, n=-1,
|
|
|
|
qt_translate=QtCore.QCoreApplication.translate):
|
|
|
|
"""
|
|
|
|
A special shortcut method to wrap around the Qt4 translation functions. This abstracts the translation procedure so
|
|
|
|
that we can change it if at a later date if necessary, without having to redo the whole of OpenLP.
|
|
|
|
|
2014-03-17 19:05:55 +00:00
|
|
|
:param context: The translation context, used to give each string a context or a namespace.
|
|
|
|
:param text: The text to put into the translation tables for translation.
|
|
|
|
:param comment: An identifying string for when the same text is used in different roles within the same context.
|
|
|
|
:param encoding:
|
|
|
|
:param n:
|
|
|
|
:param qt_translate:
|
2013-10-13 20:36:42 +00:00
|
|
|
"""
|
|
|
|
return qt_translate(context, text, comment, encoding, n)
|
|
|
|
|
|
|
|
|
|
|
|
class SlideLimits(object):
|
|
|
|
"""
|
|
|
|
Provides an enumeration for behaviour of OpenLP at the end limits of each service item when pressing the up/down
|
|
|
|
arrow keys
|
|
|
|
"""
|
|
|
|
End = 1
|
|
|
|
Wrap = 2
|
|
|
|
Next = 3
|
|
|
|
|
2013-12-13 19:44:17 +00:00
|
|
|
|
|
|
|
def de_hump(name):
|
|
|
|
"""
|
|
|
|
Change any Camel Case string to python string
|
|
|
|
"""
|
|
|
|
sub_name = FIRST_CAMEL_REGEX.sub(r'\1_\2', name)
|
|
|
|
return SECOND_CAMEL_REGEX.sub(r'\1_\2', sub_name).lower()
|
|
|
|
|
2014-08-27 23:18:06 +00:00
|
|
|
|
|
|
|
def is_win():
|
|
|
|
"""
|
|
|
|
Returns true if running on a system with a nt kernel e.g. Windows, Wine
|
|
|
|
|
|
|
|
:return: True if system is running a nt kernel false otherwise
|
|
|
|
"""
|
|
|
|
return os.name.startswith('nt')
|
|
|
|
|
|
|
|
|
|
|
|
def is_macosx():
|
|
|
|
"""
|
|
|
|
Returns true if running on a system with a darwin kernel e.g. Mac OS X
|
|
|
|
|
|
|
|
:return: True if system is running a darwin kernel false otherwise
|
|
|
|
"""
|
|
|
|
return sys.platform.startswith('darwin')
|
|
|
|
|
|
|
|
|
|
|
|
def is_linux():
|
|
|
|
"""
|
|
|
|
Returns true if running on a system with a linux kernel e.g. Ubuntu, Debian, etc
|
|
|
|
|
|
|
|
:return: True if system is running a linux kernel false otherwise
|
|
|
|
"""
|
|
|
|
return sys.platform.startswith('linux')
|
|
|
|
|
2014-10-06 19:10:03 +00:00
|
|
|
|
|
|
|
def verify_ipv4(addr):
|
|
|
|
"""
|
|
|
|
Validate an IPv4 address
|
|
|
|
|
|
|
|
:param addr: Address to validate
|
|
|
|
:returns: bool
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
valid = IPv4Address(addr)
|
|
|
|
return True
|
|
|
|
except AddressValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def verify_ipv6(addr):
|
|
|
|
"""
|
|
|
|
Validate an IPv6 address
|
|
|
|
|
|
|
|
:param addr: Address to validate
|
|
|
|
:returns: bool
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
valid = IPv6Address(addr)
|
|
|
|
return True
|
|
|
|
except AddressValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def verify_ip_address(addr):
|
|
|
|
"""
|
|
|
|
Validate an IP address as either IPv4 or IPv6
|
|
|
|
|
|
|
|
:param addr: Address to validate
|
|
|
|
:returns: bool
|
|
|
|
"""
|
|
|
|
return True if verify_ipv4(addr) else verify_ipv6(addr)
|
|
|
|
|
|
|
|
|
|
|
|
def md5_hash(salt, data):
|
|
|
|
"""
|
|
|
|
Returns the hashed output of md5sum on salt,data
|
|
|
|
using Python3 hashlib
|
|
|
|
|
|
|
|
:param salt: Initial salt
|
|
|
|
:param data: Data to hash
|
|
|
|
:returns: str
|
|
|
|
"""
|
|
|
|
log.debug('md5_hash(salt="%s")' % salt)
|
|
|
|
hash_obj = hashlib.new('md5')
|
|
|
|
hash_obj.update(salt.encode('ascii'))
|
|
|
|
hash_obj.update(data.encode('ascii'))
|
|
|
|
hash_value = hash_obj.hexdigest()
|
|
|
|
log.debug('md5_hash() returning "%s"' % hash_value)
|
|
|
|
return hash_value
|
|
|
|
|
|
|
|
|
|
|
|
def qmd5_hash(salt, data):
|
|
|
|
"""
|
2014-10-09 20:30:07 +00:00
|
|
|
Returns the hashed output of MD5Sum on salt, data
|
|
|
|
using PyQt4.QCryptographicHash.
|
2014-10-06 19:10:03 +00:00
|
|
|
|
|
|
|
:param salt: Initial salt
|
|
|
|
:param data: Data to hash
|
|
|
|
:returns: str
|
|
|
|
"""
|
|
|
|
log.debug('qmd5_hash(salt="%s"' % salt)
|
|
|
|
hash_obj = QHash(QHash.Md5)
|
|
|
|
hash_obj.addData(salt)
|
|
|
|
hash_obj.addData(data)
|
|
|
|
hash_value = hash_obj.result().toHex()
|
|
|
|
log.debug('qmd5_hash() returning "%s"' % hash_value)
|
|
|
|
return decode(hash_value.data(), 'ascii')
|
|
|
|
|
2014-10-31 20:12:06 +00:00
|
|
|
|
2014-10-31 19:47:36 +00:00
|
|
|
def clean_button_text(button_text):
|
|
|
|
"""
|
|
|
|
Clean the & and other characters out of button text
|
2014-10-31 20:12:06 +00:00
|
|
|
|
2014-10-31 19:47:36 +00:00
|
|
|
:param button_text: The text to clean
|
|
|
|
"""
|
|
|
|
return button_text.replace('&', '').replace('< ', '').replace(' >', '')
|
2014-10-06 19:10:03 +00:00
|
|
|
|
2014-10-31 20:12:06 +00:00
|
|
|
|
2013-12-13 19:44:17 +00:00
|
|
|
from .openlpmixin import OpenLPMixin
|
2013-12-13 17:44:05 +00:00
|
|
|
from .registry import Registry
|
2013-12-15 16:50:09 +00:00
|
|
|
from .registrymixin import RegistryMixin
|
2014-03-12 17:41:52 +00:00
|
|
|
from .registryproperties import RegistryProperties
|
2013-10-13 20:36:42 +00:00
|
|
|
from .uistrings import UiStrings
|
|
|
|
from .settings import Settings
|
2013-10-13 15:52:04 +00:00
|
|
|
from .applocation import AppLocation
|
2014-03-11 18:58:49 +00:00
|
|
|
from .historycombobox import HistoryComboBox
|