move code from presentations

This commit is contained in:
Tim Bentley 2016-04-16 14:51:42 +01:00
parent 317364ca7e
commit b8bbb4391c
3 changed files with 109 additions and 23 deletions

View File

@ -24,6 +24,7 @@ The :mod:`common` module contains most of the components and libraries that make
OpenLP work.
"""
import hashlib
import logging
import os
import re
@ -31,6 +32,7 @@ import sys
import traceback
from ipaddress import IPv4Address, IPv6Address, AddressValueError
from shutil import which
from subprocess import check_output, CalledProcessError, STDOUT
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import QCryptographicHash as QHash
@ -247,6 +249,10 @@ from .applocation import AppLocation
from .actions import ActionList
from .languagemanager import LanguageManager
if is_win():
from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW
def add_actions(target, actions):
"""
@ -371,3 +377,29 @@ def clean_filename(filename):
if not isinstance(filename, str):
filename = str(filename, 'utf-8')
return INVALID_FILE_CHARS.sub('_', CONTROL_CHARS.sub('', filename))
def check_binary(program_path):
"""
Function that checks whether a binary exists.
:param program_path:The full path to the binary to check.
:return: program output to be parsed
"""
log.debug('testing program_path: %s', program_path)
try:
# Setup startupinfo options for check_output to avoid console popping up on windows
if is_win():
startupinfo = STARTUPINFO()
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
else:
startupinfo = None
runlog = check_output([program_path, '--help'], stderr=STDOUT, startupinfo=startupinfo)
except CalledProcessError as e:
runlog = e.output
except Exception:
trace_error_handler(log)
runlog = ''
log.debug('check_output returned: %s' % runlog)
return runlog

View File

@ -27,7 +27,7 @@ import logging
from PyQt5 import QtCore
from openlp.core.common import Settings, translate
from openlp.core.common import Settings, translate, check_binary
from openlp.core.lib import Plugin, StringContent, build_icon
from openlp.plugins.media.lib import MediaMediaItem, MediaTab
@ -62,6 +62,51 @@ class MediaPlugin(Plugin):
"""
super().initialise()
def check_pre_conditions(self):
"""
Check it we have a valid environment.
:return: true or false
"""
log.debug('check_installed Pdf')
self.mudrawbin = ''
self.gsbin = ''
self.also_supports = []
# Use the user defined program if given
if Settings().value('presentations/enable_pdf_program'):
pdf_program = Settings().value('presentations/pdf_program')
program_type = self.check_binary('mediainfo')
if program_type == 'gs':
self.gsbin = pdf_program
elif program_type == 'mudraw':
self.mudrawbin = pdf_program
else:
# Fallback to autodetection
application_path = AppLocation.get_directory(AppLocation.AppDir)
if is_win():
# for windows we only accept mudraw.exe in the base folder
application_path = AppLocation.get_directory(AppLocation.AppDir)
if os.path.isfile(os.path.join(application_path, 'mudraw.exe')):
self.mudrawbin = os.path.join(application_path, 'mudraw.exe')
else:
DEVNULL = open(os.devnull, 'wb')
# First try to find mupdf
self.mudrawbin = which('mudraw')
# if mupdf isn't installed, fallback to ghostscript
if not self.mudrawbin:
self.gsbin = which('gs')
# Last option: check if mudraw is placed in OpenLP base folder
if not self.mudrawbin and not self.gsbin:
application_path = AppLocation.get_directory(AppLocation.AppDir)
if os.path.isfile(os.path.join(application_path, 'mudraw')):
self.mudrawbin = os.path.join(application_path, 'mudraw')
if self.mudrawbin:
self.also_supports = ['xps', 'oxps']
return True
elif self.gsbin:
return True
else:
return False
def app_startup(self):
"""
Override app_startup() in order to do nothing
@ -137,3 +182,28 @@ class MediaPlugin(Plugin):
Add html code to htmlbuilder.
"""
return self.media_controller.get_media_display_html()
def process_check_binary(program_path):
"""
Function that checks whether a binary is either ghostscript or mudraw or neither.
Is also used from presentationtab.py
:param program_path:The full path to the binary to check.
:return: Type of the binary, 'gs' if ghostscript, 'mudraw' if mudraw, None if invalid.
"""
program_type = None
runlog = check_binary(program_path)
# Analyse the output to see it the program is mudraw, ghostscript or neither
for line in runlog.splitlines():
decoded_line = line.decode()
found_mudraw = re.search('usage: mudraw.*', decoded_line, re.IGNORECASE)
if found_mudraw:
program_type = 'mudraw'
break
found_gs = re.search('GPL Ghostscript.*', decoded_line, re.IGNORECASE)
if found_gs:
program_type = 'gs'
break
log.debug('in check_binary, found: %s', program_type)
return program_type

View File

@ -22,13 +22,12 @@
import os
import logging
from tempfile import NamedTemporaryFile
import re
from shutil import which
from subprocess import check_output, CalledProcessError, STDOUT
from subprocess import check_output, CalledProcessError
from openlp.core.common import AppLocation
from openlp.core.common import Settings, is_win, trace_error_handler
from openlp.core.common import AppLocation, check_binary
from openlp.core.common import Settings, is_win
from openlp.core.lib import ScreenList
from .presentationcontroller import PresentationController, PresentationDocument
@ -61,7 +60,7 @@ class PdfController(PresentationController):
self.check_installed()
@staticmethod
def check_binary(program_path):
def process_check_binary(program_path):
"""
Function that checks whether a binary is either ghostscript or mudraw or neither.
Is also used from presentationtab.py
@ -70,22 +69,7 @@ class PdfController(PresentationController):
:return: Type of the binary, 'gs' if ghostscript, 'mudraw' if mudraw, None if invalid.
"""
program_type = None
runlog = ''
log.debug('testing program_path: %s', program_path)
try:
# Setup startupinfo options for check_output to avoid console popping up on windows
if is_win():
startupinfo = STARTUPINFO()
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
else:
startupinfo = None
runlog = check_output([program_path, '--help'], stderr=STDOUT, startupinfo=startupinfo)
except CalledProcessError as e:
runlog = e.output
except Exception:
trace_error_handler(log)
runlog = ''
log.debug('check_output returned: %s' % runlog)
runlog = check_binary(program_path)
# Analyse the output to see it the program is mudraw, ghostscript or neither
for line in runlog.splitlines():
decoded_line = line.decode()
@ -122,7 +106,7 @@ class PdfController(PresentationController):
# Use the user defined program if given
if Settings().value('presentations/enable_pdf_program'):
pdf_program = Settings().value('presentations/pdf_program')
program_type = self.check_binary(pdf_program)
program_type = self.process_check_binary(pdf_program)
if program_type == 'gs':
self.gsbin = pdf_program
elif program_type == 'mudraw':