code standards

This commit is contained in:
Andreas Preikschat 2013-04-19 21:07:25 +02:00
parent b03c3c6f4f
commit 582f1aa003
2 changed files with 37 additions and 38 deletions

View File

@ -94,7 +94,7 @@ class ImageMediaItem(MediaManagerItem):
self.servicePath = os.path.join(AppLocation.get_section_data_path(self.settings_section), u'thumbnails') self.servicePath = os.path.join(AppLocation.get_section_data_path(self.settings_section), u'thumbnails')
check_directory_exists(self.servicePath) check_directory_exists(self.servicePath)
# Load images from the database # Load images from the database
self.loadFullList( self.load_full_list(
self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename), initial_load=True) self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename), initial_load=True)
def add_list_view_to_toolbar(self): def add_list_view_to_toolbar(self):
@ -163,7 +163,7 @@ class ImageMediaItem(MediaManagerItem):
""" """
create_widget_action(self.list_view, separator=True) create_widget_action(self.list_view, separator=True)
create_widget_action(self.list_view, create_widget_action(self.list_view,
text=UiStrings().AddGroup, icon=u':/images/image_new_group.png', triggers=self.onAddGroupClick) text=UiStrings().AddGroup, icon=u':/images/image_new_group.png', triggers=self.on_add_group_click)
create_widget_action(self.list_view, create_widget_action(self.list_view,
text=self.plugin.get_string(StringContent.Load)[u'tooltip'], text=self.plugin.get_string(StringContent.Load)[u'tooltip'],
icon=u':/general/general_open.png', triggers=self.on_file_click) icon=u':/general/general_open.png', triggers=self.on_file_click)
@ -173,16 +173,16 @@ class ImageMediaItem(MediaManagerItem):
Add custom buttons to the start of the toolbar. Add custom buttons to the start of the toolbar.
""" """
self.addGroupAction = self.toolbar.add_toolbar_action(u'addGroupAction', self.addGroupAction = self.toolbar.add_toolbar_action(u'addGroupAction',
icon=u':/images/image_new_group.png', triggers=self.onAddGroupClick) icon=u':/images/image_new_group.png', triggers=self.on_add_group_click)
def add_end_header_bar(self): def add_end_header_bar(self):
""" """
Add custom buttons to the end of the toolbar Add custom buttons to the end of the toolbar
""" """
self.replaceAction = self.toolbar.add_toolbar_action(u'replaceAction', self.replaceAction = self.toolbar.add_toolbar_action(u'replaceAction',
icon=u':/slides/slide_blank.png', triggers=self.onReplaceClick) icon=u':/slides/slide_blank.png', triggers=self.on_replace_click)
self.resetAction = self.toolbar.add_toolbar_action(u'resetAction', self.resetAction = self.toolbar.add_toolbar_action(u'resetAction',
icon=u':/system/system_close.png', visible=False, triggers=self.onResetClick) icon=u':/system/system_close.png', visible=False, triggers=self.on_reset_click)
def recursively_delete_group(self, image_group): def recursively_delete_group(self, image_group):
""" """
@ -311,29 +311,29 @@ class ImageMediaItem(MediaManagerItem):
return True return True
return return_value return return_value
def loadFullList(self, images, initial_load=False, open_group=None): def load_full_list(self, images, initial_load=False, open_group=None):
""" """
Replace the list of images and groups in the interface. Replace the list of images and groups in the interface.
``images`` ``images``
A List of ImageFilenames objects that will be used to reload the mediamanager list A List of ImageFilenames objects that will be used to reload the mediamanager list.
``initial_load`` ``initial_load``
When set to False, the busy cursor and progressbar will be shown while loading images When set to False, the busy cursor and progressbar will be shown while loading images.
``open_group`` ``open_group``
ImageGroups object of the group that must be expanded after reloading the list in the interface ImageGroups object of the group that must be expanded after reloading the list in the interface.
""" """
if not initial_load: if not initial_load:
self.application.set_busy_cursor() self.application.set_busy_cursor()
self.main_window.display_progress_bar(len(images)) self.main_window.display_progress_bar(len(images))
self.list_view.clear() self.list_view.clear()
# Load the list of groups and add them to the treeView # Load the list of groups and add them to the treeView.
group_items = {} group_items = {}
self.add_sub_groups(group_items, parent_group_id=0) self.add_sub_groups(group_items, parent_group_id=0)
if open_group is not None: if open_group is not None:
self.expand_group(open_group.id) self.expand_group(open_group.id)
# Sort the images by its filename considering language specific # Sort the images by its filename considering language specific.
# characters. # characters.
images.sort(key=lambda image_object: get_locale_key(os.path.split(unicode(image_object.filename))[1])) images.sort(key=lambda image_object: get_locale_key(os.path.split(unicode(image_object.filename))[1]))
for imageFile in images: for imageFile in images:
@ -452,7 +452,7 @@ class ImageMediaItem(MediaManagerItem):
self.main_window.display_progress_bar(len(images)) self.main_window.display_progress_bar(len(images))
# Save the new images in the database # Save the new images in the database
self.save_new_images_list(images, group_id=parent_group.id, reload_list=False) self.save_new_images_list(images, group_id=parent_group.id, reload_list=False)
self.loadFullList(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename), self.load_full_list(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename),
initial_load=initial_load, open_group=parent_group) initial_load=initial_load, open_group=parent_group)
self.application.set_normal_cursor() self.application.set_normal_cursor()
@ -479,7 +479,7 @@ class ImageMediaItem(MediaManagerItem):
self.manager.save_object(imageFile) self.manager.save_object(imageFile)
self.main_window.increment_progress_bar() self.main_window.increment_progress_bar()
if reload_list and images_list: if reload_list and images_list:
self.loadFullList(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename)) self.load_full_list(self.manager.get_all_objects(ImageFilenames, order_by_ref=ImageFilenames.filename))
def dnd_move_internal(self, target): def dnd_move_internal(self, target):
""" """
@ -527,7 +527,7 @@ class ImageMediaItem(MediaManagerItem):
image_items.sort(key=lambda item: get_locale_key(item.text(0))) image_items.sort(key=lambda item: get_locale_key(item.text(0)))
target_group.addChildren(image_items) target_group.addChildren(image_items)
def generate_slide_data(self, service_item, item=None, xmlVersion=False, def generate_slide_data(self, service_item, item=None, xml_version=False,
remote=False, context=ServiceItemContext.Service): remote=False, context=ServiceItemContext.Service):
""" """
Generate the slide data. Needs to be implemented by the plugin. Generate the slide data. Needs to be implemented by the plugin.
@ -605,7 +605,7 @@ class ImageMediaItem(MediaManagerItem):
else: else:
return False return False
def onAddGroupClick(self): def on_add_group_click(self):
""" """
Called to add a new group Called to add a new group
""" """
@ -626,7 +626,7 @@ class ImageMediaItem(MediaManagerItem):
group_name=self.add_group_form.name_edit.text()) group_name=self.add_group_form.name_edit.text())
if not self.check_group_exists(new_group): if not self.check_group_exists(new_group):
if self.manager.save_object(new_group): if self.manager.save_object(new_group):
self.loadFullList(self.manager.get_all_objects(ImageFilenames, self.load_full_list(self.manager.get_all_objects(ImageFilenames,
order_by_ref=ImageFilenames.filename)) order_by_ref=ImageFilenames.filename))
self.expand_group(new_group.id) self.expand_group(new_group.id)
self.fill_groups_combobox(self.choose_group_form.group_combobox) self.fill_groups_combobox(self.choose_group_form.group_combobox)
@ -635,23 +635,22 @@ class ImageMediaItem(MediaManagerItem):
critical_error_message_box( critical_error_message_box(
message=translate('ImagePlugin.AddGroupForm', 'Could not add the new group.')) message=translate('ImagePlugin.AddGroupForm', 'Could not add the new group.'))
else: else:
critical_error_message_box( critical_error_message_box(message=translate('ImagePlugin.AddGroupForm', 'This group already exists.'))
message=translate('ImagePlugin.AddGroupForm', 'This group already exists.'))
def onResetClick(self): def on_reset_click(self):
""" """
Called to reset the Live background with the image selected, Called to reset the Live background with the image selected.
""" """
self.resetAction.setVisible(False) self.resetAction.setVisible(False)
self.live_controller.display.reset_image() self.live_controller.display.reset_image()
def live_theme_changed(self): def live_theme_changed(self):
""" """
Triggered by the change of theme in the slide controller Triggered by the change of theme in the slide controller.
""" """
self.resetAction.setVisible(False) self.resetAction.setVisible(False)
def onReplaceClick(self): def on_replace_click(self):
""" """
Called to replace Live backgound with the image selected. Called to replace Live backgound with the image selected.
""" """
@ -660,7 +659,7 @@ class ImageMediaItem(MediaManagerItem):
background = QtGui.QColor(Settings().value(self.settings_section + u'/background color')) background = QtGui.QColor(Settings().value(self.settings_section + u'/background color'))
bitem = self.list_view.selectedItems()[0] bitem = self.list_view.selectedItems()[0]
if not isinstance(bitem.data(0, QtCore.Qt.UserRole), ImageFilenames): if not isinstance(bitem.data(0, QtCore.Qt.UserRole), ImageFilenames):
# Only continue when an image is selected # Only continue when an image is selected.
return return
filename = bitem.data(0, QtCore.Qt.UserRole).filename filename = bitem.data(0, QtCore.Qt.UserRole).filename
if os.path.exists(filename): if os.path.exists(filename):

View File

@ -35,7 +35,7 @@ class TestImageMediaItem(TestCase):
""" """
# GIVEN: An empty image_list # GIVEN: An empty image_list
image_list = [] image_list = []
with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_loadFullList:
self.media_item.manager = MagicMock() self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with the empty list # WHEN: We run save_new_images_list with the empty list
@ -47,37 +47,37 @@ class TestImageMediaItem(TestCase):
def save_new_images_list_single_image_with_reload_test(self): def save_new_images_list_single_image_with_reload_test(self):
""" """
Test that the save_new_images_list() calls loadFullList() when reload_list is set to True Test that the save_new_images_list() calls load_full_list() when reload_list is set to True
""" """
# GIVEN: A list with 1 image # GIVEN: A list with 1 image
image_list = [ u'test_image.jpg' ] image_list = [ u'test_image.jpg' ]
with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_loadFullList:
ImageFilenames.filename = '' ImageFilenames.filename = ''
self.media_item.manager = MagicMock() self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with reload_list=True # WHEN: We run save_new_images_list with reload_list=True
self.media_item.save_new_images_list(image_list, reload_list=True) self.media_item.save_new_images_list(image_list, reload_list=True)
# THEN: loadFullList() should have been called # THEN: load_full_list() should have been called
assert mocked_loadFullList.call_count == 1, u'loadFullList() should have been called' assert mocked_loadFullList.call_count == 1, u'load_full_list() should have been called'
# CLEANUP: Remove added attribute from ImageFilenames # CLEANUP: Remove added attribute from ImageFilenames
delattr(ImageFilenames, 'filename') delattr(ImageFilenames, 'filename')
def save_new_images_list_single_image_without_reload_test(self): def save_new_images_list_single_image_without_reload_test(self):
""" """
Test that the save_new_images_list() doesn't call loadFullList() when reload_list is set to False Test that the save_new_images_list() doesn't call load_full_list() when reload_list is set to False
""" """
# GIVEN: A list with 1 image # GIVEN: A list with 1 image
image_list = [ u'test_image.jpg' ] image_list = [ u'test_image.jpg' ]
with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_loadFullList:
self.media_item.manager = MagicMock() self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with reload_list=False # WHEN: We run save_new_images_list with reload_list=False
self.media_item.save_new_images_list(image_list, reload_list=False) self.media_item.save_new_images_list(image_list, reload_list=False)
# THEN: loadFullList() should not have been called # THEN: load_full_list() should not have been called
assert mocked_loadFullList.call_count == 0, u'loadFullList() should not have been called' assert mocked_loadFullList.call_count == 0, u'load_full_list() should not have been called'
def save_new_images_list_multiple_images_test(self): def save_new_images_list_multiple_images_test(self):
""" """
@ -85,15 +85,15 @@ class TestImageMediaItem(TestCase):
""" """
# GIVEN: A list with 3 images # GIVEN: A list with 3 images
image_list = [ u'test_image_1.jpg', u'test_image_2.jpg', u'test_image_3.jpg' ] image_list = [ u'test_image_1.jpg', u'test_image_2.jpg', u'test_image_3.jpg' ]
with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_loadFullList:
self.media_item.manager = MagicMock() self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with the list of 3 images # WHEN: We run save_new_images_list with the list of 3 images
self.media_item.save_new_images_list(image_list, reload_list=False) self.media_item.save_new_images_list(image_list, reload_list=False)
# THEN: loadFullList() should not have been called # THEN: load_full_list() should not have been called
assert self.media_item.manager.save_object.call_count == 3, \ assert self.media_item.manager.save_object.call_count == 3, \
u'loadFullList() should have been called three times' u'load_full_list() should have been called three times'
def save_new_images_list_other_objects_in_list_test(self): def save_new_images_list_other_objects_in_list_test(self):
""" """
@ -101,12 +101,12 @@ class TestImageMediaItem(TestCase):
""" """
# GIVEN: A list with images and objects # GIVEN: A list with images and objects
image_list = [ u'test_image_1.jpg', None, True, ImageFilenames(), 'test_image_2.jpg' ] image_list = [ u'test_image_1.jpg', None, True, ImageFilenames(), 'test_image_2.jpg' ]
with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.loadFullList') as mocked_loadFullList: with patch(u'openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_loadFullList:
self.media_item.manager = MagicMock() self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with the list of images and objects # WHEN: We run save_new_images_list with the list of images and objects
self.media_item.save_new_images_list(image_list, reload_list=False) self.media_item.save_new_images_list(image_list, reload_list=False)
# THEN: loadFullList() should not have been called # THEN: load_full_list() should not have been called
assert self.media_item.manager.save_object.call_count == 2, \ assert self.media_item.manager.save_object.call_count == 2, \
u'loadFullList() should have been called only once' u'load_full_list() should have been called only once'