Make methods static that don't need to rely on instance data

This commit is contained in:
Simon Hanna 2016-01-04 13:21:58 +01:00
parent 7ed23514b4
commit c467c321b4
5 changed files with 47 additions and 39 deletions

View File

@ -273,7 +273,7 @@ class Renderer(OpenLPMixin, RegistryMixin, RegistryProperties):
except ValueError:
text_to_render = text.split('\n[---]\n')[0]
text = ''
text_to_render, raw_tags, html_tags = self._get_start_tags(text_to_render)
text_to_render, raw_tags, html_tags = Renderer._get_start_tags(text_to_render)
if text:
text = raw_tags + text
else:
@ -441,7 +441,7 @@ class Renderer(OpenLPMixin, RegistryMixin, RegistryProperties):
previous_raw = line + line_end
continue
# Figure out how many words of the line will fit on screen as the line will not fit as a whole.
raw_words = self._words_split(line)
raw_words = Renderer._words_split(line)
html_words = list(map(expand_tags, raw_words))
previous_html, previous_raw = \
self._binary_chop(formatted, previous_html, previous_raw, html_words, raw_words, ' ', line_end)
@ -451,7 +451,8 @@ class Renderer(OpenLPMixin, RegistryMixin, RegistryProperties):
formatted.append(previous_raw)
return formatted
def _get_start_tags(self, raw_text):
@staticmethod
def _get_start_tags(raw_text):
"""
Tests the given text for not closed formatting tags and returns a tuple consisting of three unicode strings::
@ -521,7 +522,7 @@ class Renderer(OpenLPMixin, RegistryMixin, RegistryProperties):
if smallest_index == index or highest_index == index:
index = smallest_index
text = previous_raw.rstrip('<br>') + separator.join(raw_list[:index + 1])
text, raw_tags, html_tags = self._get_start_tags(text)
text, raw_tags, html_tags = Renderer._get_start_tags(text)
formatted.append(text)
previous_html = ''
previous_raw = ''
@ -556,7 +557,8 @@ class Renderer(OpenLPMixin, RegistryMixin, RegistryProperties):
self.web_frame.evaluateJavaScript('show_text("%s")' % text.replace('\\', '\\\\').replace('\"', '\\\"'))
return self.web_frame.contentsSize().height() <= self.empty_height
def _words_split(self, line):
@staticmethod
def _words_split(line):
"""
Split the slide up by word so can wrap better

View File

@ -144,8 +144,8 @@ class Ui_ServiceManager(object):
self.service_manager_list.customContextMenuRequested.connect(self.context_menu)
self.service_manager_list.setObjectName('service_manager_list')
# enable drop
self.service_manager_list.__class__.dragEnterEvent = self.drag_enter_event
self.service_manager_list.__class__.dragMoveEvent = self.drag_enter_event
self.service_manager_list.__class__.dragEnterEvent = Ui_ServiceManager.drag_enter_event
self.service_manager_list.__class__.dragMoveEvent = Ui_ServiceManager.drag_enter_event
self.service_manager_list.__class__.dropEvent = self.drop_event
self.layout.addWidget(self.service_manager_list)
# Add the bottom toolbar
@ -293,7 +293,8 @@ class Ui_ServiceManager(object):
Registry().register_function('theme_update_global', self.theme_change)
Registry().register_function('mediaitem_suffix_reset', self.reset_supported_suffixes)
def drag_enter_event(self, event):
@staticmethod
def drag_enter_event(event):
"""
Accept Drag events
@ -1585,7 +1586,7 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ServiceMa
if item is None:
end_pos = len(self.service_items)
else:
end_pos = self._get_parent_item_data(item) - 1
end_pos = ServiceManager._get_parent_item_data(item) - 1
service_item = self.service_items[start_pos]
self.service_items.remove(service_item)
self.service_items.insert(end_pos, service_item)
@ -1598,21 +1599,21 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ServiceMa
self.drop_position = len(self.service_items)
else:
# we are over something so lets investigate
pos = self._get_parent_item_data(item) - 1
pos = ServiceManager._get_parent_item_data(item) - 1
service_item = self.service_items[pos]
if (plugin == service_item['service_item'].name and
service_item['service_item'].is_capable(ItemCapabilities.CanAppend)):
action = self.dnd_menu.exec(QtGui.QCursor.pos())
# New action required
if action == self.new_action:
self.drop_position = self._get_parent_item_data(item)
self.drop_position = ServiceManager._get_parent_item_data(item)
# Append to existing action
if action == self.add_to_action:
self.drop_position = self._get_parent_item_data(item)
self.drop_position = ServiceManager._get_parent_item_data(item)
item.setSelected(True)
replace = True
else:
self.drop_position = self._get_parent_item_data(item) - 1
self.drop_position = ServiceManager._get_parent_item_data(item) - 1
Registry().execute('%s_add_service_item' % plugin, replace)
def update_theme_list(self, theme_list):
@ -1656,7 +1657,8 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ServiceMa
self.service_items[item]['service_item'].update_theme(theme)
self.regenerate_service_items(True)
def _get_parent_item_data(self, item):
@staticmethod
def _get_parent_item_data(item):
"""
Finds and returns the parent item for any item
@ -1668,7 +1670,8 @@ class ServiceManager(OpenLPMixin, RegistryMixin, QtWidgets.QWidget, Ui_ServiceMa
else:
return parent_item.data(0, QtCore.Qt.UserRole)
def print_service_order(self, field=None):
@staticmethod
def print_service_order(field=None):
"""
Print a Service Order Sheet.
"""

View File

@ -72,7 +72,7 @@ class SongExportForm(OpenLPWizard):
"""
Song wizard specific signals.
"""
self.available_list_widget.itemActivated.connect(self.on_item_activated)
self.available_list_widget.itemActivated.connect(SongExportForm.on_item_activated)
self.search_line_edit.textEdited.connect(self.on_search_line_edit_changed)
self.uncheck_button.clicked.connect(self.on_uncheck_button_clicked)
self.check_button.clicked.connect(self.on_check_button_clicked)
@ -172,7 +172,7 @@ class SongExportForm(OpenLPWizard):
return True
elif self.currentPage() == self.available_songs_page:
items = [
item for item in self._find_list_widget_items(self.available_list_widget) if item.checkState()
item for item in SongExportForm._find_list_widget_items(self.available_list_widget) if item.checkState()
]
if not items:
critical_error_message_box(
@ -241,7 +241,7 @@ class SongExportForm(OpenLPWizard):
"""
songs = [
song.data(QtCore.Qt.UserRole)
for song in self._find_list_widget_items(self.selected_list_widget)
for song in SongExportForm._find_list_widget_items(self.selected_list_widget)
]
exporter = OpenLyricsExport(self, songs, self.directory_line_edit.text())
try:
@ -255,7 +255,8 @@ class SongExportForm(OpenLPWizard):
self.progress_label.setText(translate('SongsPlugin.SongExportForm', 'Your song export failed because this '
'error occurred: %s') % ose.strerror)
def _find_list_widget_items(self, list_widget, text=''):
@staticmethod
def _find_list_widget_items(list_widget, text=''):
"""
Returns a list of *QListWidgetItem*s of the ``list_widget``. Note, that hidden items are included.
@ -266,7 +267,8 @@ class SongExportForm(OpenLPWizard):
item for item in list_widget.findItems(text, QtCore.Qt.MatchContains)
]
def on_item_activated(self, item):
@staticmethod
def on_item_activated(item):
"""
Called, when an item in the *available_list_widget* has been triggered.
The item is check if it was not checked, whereas it is unchecked when it
@ -286,9 +288,9 @@ class SongExportForm(OpenLPWizard):
:param text: The text of the *search_line_edit*.
"""
search_result = [
song for song in self._find_list_widget_items(self.available_list_widget, text)
song for song in SongExportForm._find_list_widget_items(self.available_list_widget, text)
]
for item in self._find_list_widget_items(self.available_list_widget):
for item in SongExportForm._find_list_widget_items(self.available_list_widget):
item.setHidden(item not in search_result)
def on_uncheck_button_clicked(self):

View File

@ -234,7 +234,8 @@ class FoilPresenter(object):
clean_song(self.manager, song)
self.manager.save_object(song)
def _child(self, element):
@staticmethod
def _child(element):
"""
This returns the text of an element as unicode string.
@ -253,7 +254,7 @@ class FoilPresenter(object):
"""
authors = []
try:
copyright = self._child(foilpresenterfolie.copyright.text_)
copyright = FoilPresenter._child(foilpresenterfolie.copyright.text_)
except AttributeError:
copyright = None
if copyright:
@ -346,7 +347,7 @@ class FoilPresenter(object):
:param song: The song object.
"""
try:
song.ccli_number = self._child(foilpresenterfolie.ccliid)
song.ccli_number = FoilPresenter._child(foilpresenterfolie.ccliid)
except AttributeError:
song.ccli_number = ''
@ -358,7 +359,7 @@ class FoilPresenter(object):
:param song: The song object.
"""
try:
song.comments = self._child(foilpresenterfolie.notiz)
song.comments = FoilPresenter._child(foilpresenterfolie.notiz)
except AttributeError:
song.comments = ''
@ -370,7 +371,7 @@ class FoilPresenter(object):
:param song: The song object.
"""
try:
song.copyright = self._child(foilpresenterfolie.copyright.text_)
song.copyright = FoilPresenter._child(foilpresenterfolie.copyright.text_)
except AttributeError:
song.copyright = ''
@ -396,19 +397,19 @@ class FoilPresenter(object):
VerseType.tags[VerseType.PreChorus]: 1
}
if not hasattr(foilpresenterfolie.strophen, 'strophe'):
self.importer.log_error(self._child(foilpresenterfolie.titel),
self.importer.log_error(FoilPresenter._child(foilpresenterfolie.titel),
str(translate('SongsPlugin.FoilPresenterSongImport',
'Invalid Foilpresenter song file. No verses found.')))
self.save_song = False
return
for strophe in foilpresenterfolie.strophen.strophe:
text = self._child(strophe.text_) if hasattr(strophe, 'text_') else ''
verse_name = self._child(strophe.key)
text = FoilPresenter._child(strophe.text_) if hasattr(strophe, 'text_') else ''
verse_name = FoilPresenter._child(strophe.key)
children = strophe.getchildren()
sortnr = False
for child in children:
if child.tag == 'sortnr':
verse_sortnr = self._child(strophe.sortnr)
verse_sortnr = FoilPresenter._child(strophe.sortnr)
sortnr = True
# In older Version there is no sortnr, but we need one
if not sortnr:
@ -484,7 +485,7 @@ class FoilPresenter(object):
song.song_number = ''
try:
for bucheintrag in foilpresenterfolie.buch.bucheintrag:
book_name = self._child(bucheintrag.name)
book_name = FoilPresenter._child(bucheintrag.name)
if book_name:
book = self.manager.get_object_filtered(Book, Book.name == book_name)
if book is None:
@ -493,8 +494,8 @@ class FoilPresenter(object):
self.manager.save_object(book)
song.song_book_id = book.id
try:
if self._child(bucheintrag.nummer):
song.song_number = self._child(bucheintrag.nummer)
if FoilPresenter._child(bucheintrag.nummer):
song.song_number = FoilPresenter._child(bucheintrag.nummer)
except AttributeError:
pass
# We only support one song book, so take the first one.
@ -512,13 +513,13 @@ class FoilPresenter(object):
try:
for title_string in foilpresenterfolie.titel.titelstring:
if not song.title:
song.title = self._child(title_string)
song.title = FoilPresenter._child(title_string)
song.alternate_title = ''
else:
song.alternate_title = self._child(title_string)
song.alternate_title = FoilPresenter._child(title_string)
except AttributeError:
# Use first line of first verse
first_line = self._child(foilpresenterfolie.strophen.strophe.text_)
first_line = FoilPresenter._child(foilpresenterfolie.strophen.strophe.text_)
song.title = first_line.split('\n')[0]
def _process_topics(self, foilpresenterfolie, song):
@ -530,7 +531,7 @@ class FoilPresenter(object):
"""
try:
for name in foilpresenterfolie.kategorien.name:
topic_text = self._child(name)
topic_text = FoilPresenter._child(name)
if topic_text:
topic = self.manager.get_object_filtered(Topic, Topic.name == topic_text)
if topic is None:

View File

@ -297,7 +297,7 @@ class SongsPlugin(Plugin):
if sfile.startswith('songs_') and sfile.endswith('.sqlite'):
self.application.process_events()
song_dbs.append(os.path.join(db_dir, sfile))
song_count += self._count_songs(os.path.join(db_dir, sfile))
song_count += SongsPlugin._count_songs(os.path.join(db_dir, sfile))
if not song_dbs:
return
self.application.process_events()