From 568904a6cfced024a01ead8ea1704cd3de716b4e Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Sat, 17 Oct 2015 23:07:28 +0100 Subject: [PATCH 1/3] Fixes for python 3.5. --- openlp/plugins/bibles/lib/http.py | 5 ++--- openlp/plugins/songs/lib/songselect.py | 11 ++++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/openlp/plugins/bibles/lib/http.py b/openlp/plugins/bibles/lib/http.py index 9d7ad916d..fcf4b049d 100644 --- a/openlp/plugins/bibles/lib/http.py +++ b/openlp/plugins/bibles/lib/http.py @@ -27,7 +27,6 @@ import re import socket import urllib.parse import urllib.error -from html.parser import HTMLParseError from bs4 import BeautifulSoup, NavigableString, Tag @@ -290,7 +289,7 @@ class BGExtract(RegistryProperties): page_source = str(page_source, 'cp1251') try: soup = BeautifulSoup(page_source) - except HTMLParseError: + except Exception: log.error('BeautifulSoup could not parse the Bible page.') send_error_message('parse') return None @@ -762,7 +761,7 @@ def get_soup_for_bible_ref(reference_url, header=None, pre_parse_regex=None, pre try: soup = BeautifulSoup(page_source) CLEANER_REGEX.sub('', str(soup)) - except HTMLParseError: + except Exception: log.exception('BeautifulSoup could not parse the bible page.') if not soup: send_error_message('parse') diff --git a/openlp/plugins/songs/lib/songselect.py b/openlp/plugins/songs/lib/songselect.py index c09be65c0..6df493dd2 100644 --- a/openlp/plugins/songs/lib/songselect.py +++ b/openlp/plugins/songs/lib/songselect.py @@ -27,6 +27,7 @@ from http.cookiejar import CookieJar from urllib.parse import urlencode from urllib.request import HTTPCookieProcessor, URLError, build_opener from html.parser import HTMLParser +from html import unescape from bs4 import BeautifulSoup, NavigableString @@ -130,8 +131,8 @@ class SongSelectImport(object): break for result in search_results: song = { - 'title': self.html_parser.unescape(result.find('h3').string), - 'authors': [self.html_parser.unescape(author.string) for author in result.find_all('li')], + 'title': unescape(result.find('h3').string), + 'authors': [unescape(author.string) for author in result.find_all('li')], 'link': BASE_URL + result.find('a')['href'] } if callback: @@ -167,7 +168,7 @@ class SongSelectImport(object): if callback: callback() song['copyright'] = '/'.join([li.string for li in song_page.find('ul', 'copyright').find_all('li')]) - song['copyright'] = self.html_parser.unescape(song['copyright']) + song['copyright'] = unescape(song['copyright']) song['ccli_number'] = song_page.find('ul', 'info').find('li').string.split(':')[1].strip() song['verses'] = [] verses = lyrics_page.find('section', 'lyrics').find_all('p') @@ -180,9 +181,9 @@ class SongSelectImport(object): else: verse['lyrics'] += '\n' verse['lyrics'] = verse['lyrics'].strip(' \n\r\t') - song['verses'].append(self.html_parser.unescape(verse)) + song['verses'].append(unescape(verse)) for counter, author in enumerate(song['authors']): - song['authors'][counter] = self.html_parser.unescape(author) + song['authors'][counter] = unescape(author) return song def save_song(self, song): From 7470205185c49db5b0086d4107a6b8bd7897fbdd Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 19 Oct 2015 23:26:58 +0100 Subject: [PATCH 2/3] Make songselect import work on both python 3.3 and 3.5 --- openlp/plugins/songs/lib/songselect.py | 36 +++++++++++++++++++------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/openlp/plugins/songs/lib/songselect.py b/openlp/plugins/songs/lib/songselect.py index 6df493dd2..e7639ca21 100644 --- a/openlp/plugins/songs/lib/songselect.py +++ b/openlp/plugins/songs/lib/songselect.py @@ -23,11 +23,13 @@ The :mod:`~openlp.plugins.songs.lib.songselect` module contains the SongSelect importer itself. """ import logging +import sys from http.cookiejar import CookieJar from urllib.parse import urlencode from urllib.request import HTTPCookieProcessor, URLError, build_opener from html.parser import HTMLParser -from html import unescape +if sys.version_info > (3,4): + from html import unescape from bs4 import BeautifulSoup, NavigableString @@ -130,11 +132,18 @@ class SongSelectImport(object): if not search_results: break for result in search_results: - song = { - 'title': unescape(result.find('h3').string), - 'authors': [unescape(author.string) for author in result.find_all('li')], - 'link': BASE_URL + result.find('a')['href'] - } + if sys.version_info > (3,4): + song = { + 'title': unescape(result.find('h3').string), + 'authors': [unescape(author.string) for author in result.find_all('li')], + 'link': BASE_URL + result.find('a')['href'] + } + else: + song = { + 'title': self.html_parser.unescape(result.find('h3').string), + 'authors': [self.html_parser.unescape(author.string) for author in result.find_all('li')], + 'link': BASE_URL + result.find('a')['href'] + } if callback: callback(song) songs.append(song) @@ -168,7 +177,10 @@ class SongSelectImport(object): if callback: callback() song['copyright'] = '/'.join([li.string for li in song_page.find('ul', 'copyright').find_all('li')]) - song['copyright'] = unescape(song['copyright']) + if sys.version_info > (3,4): + song['copyright'] = unescape(song['copyright']) + else: + song['copyright'] = self.html_parser.unescape(song['copyright']) song['ccli_number'] = song_page.find('ul', 'info').find('li').string.split(':')[1].strip() song['verses'] = [] verses = lyrics_page.find('section', 'lyrics').find_all('p') @@ -181,9 +193,15 @@ class SongSelectImport(object): else: verse['lyrics'] += '\n' verse['lyrics'] = verse['lyrics'].strip(' \n\r\t') - song['verses'].append(unescape(verse)) + if sys.version_info > (3,4): + song['verses'].append(unescape(verse)) + else: + song['verses'].append(self.html_parser.unescape(verse)) for counter, author in enumerate(song['authors']): - song['authors'][counter] = unescape(author) + if sys.version_info > (3,4): + song['authors'][counter] = unescape(author) + else: + song['authors'][counter] = self.html_parser.unescape(author) return song def save_song(self, song): From b4669e0bf698cd2e5c1019eb5d0cc61a9e847ce8 Mon Sep 17 00:00:00 2001 From: Tomas Groth Date: Mon, 19 Oct 2015 23:32:26 +0100 Subject: [PATCH 3/3] pep8 fixes --- openlp/plugins/songs/lib/songselect.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/songs/lib/songselect.py b/openlp/plugins/songs/lib/songselect.py index e7639ca21..e1641ae0c 100644 --- a/openlp/plugins/songs/lib/songselect.py +++ b/openlp/plugins/songs/lib/songselect.py @@ -28,7 +28,7 @@ from http.cookiejar import CookieJar from urllib.parse import urlencode from urllib.request import HTTPCookieProcessor, URLError, build_opener from html.parser import HTMLParser -if sys.version_info > (3,4): +if sys.version_info > (3, 4): from html import unescape @@ -132,7 +132,7 @@ class SongSelectImport(object): if not search_results: break for result in search_results: - if sys.version_info > (3,4): + if sys.version_info > (3, 4): song = { 'title': unescape(result.find('h3').string), 'authors': [unescape(author.string) for author in result.find_all('li')], @@ -177,7 +177,7 @@ class SongSelectImport(object): if callback: callback() song['copyright'] = '/'.join([li.string for li in song_page.find('ul', 'copyright').find_all('li')]) - if sys.version_info > (3,4): + if sys.version_info > (3, 4): song['copyright'] = unescape(song['copyright']) else: song['copyright'] = self.html_parser.unescape(song['copyright']) @@ -193,12 +193,12 @@ class SongSelectImport(object): else: verse['lyrics'] += '\n' verse['lyrics'] = verse['lyrics'].strip(' \n\r\t') - if sys.version_info > (3,4): + if sys.version_info > (3, 4): song['verses'].append(unescape(verse)) else: song['verses'].append(self.html_parser.unescape(verse)) for counter, author in enumerate(song['authors']): - if sys.version_info > (3,4): + if sys.version_info > (3, 4): song['authors'][counter] = unescape(author) else: song['authors'][counter] = self.html_parser.unescape(author)