Merge lp:~phill-ridout/openlp/import-depreciations into lp:openlp
- import-depreciations
- Merge into trunk
Proposed by
Phill
Status: | Superseded |
---|---|
Proposed branch: | lp:~phill-ridout/openlp/import-depreciations |
Merge into: | lp:openlp |
Diff against target: |
346 lines (+118/-64) 10 files modified
openlp/core/common/__init__.py (+34/-1) openlp/core/lib/pluginmanager.py (+3/-28) openlp/core/ui/media/mediacontroller.py (+5/-13) openlp/plugins/presentations/lib/impresscontroller.py (+2/-1) openlp/plugins/presentations/lib/pdfcontroller.py (+1/-1) openlp/plugins/presentations/lib/powerpointcontroller.py (+1/-1) openlp/plugins/presentations/lib/pptviewcontroller.py (+1/-1) openlp/plugins/presentations/lib/presentationtab.py (+1/-1) openlp/plugins/presentations/presentationplugin.py (+5/-14) tests/functional/openlp_core_common/test_common.py (+65/-3) |
To merge this branch: | bzr merge lp:~phill-ridout/openlp/import-depreciations |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Tim Bentley | Needs Fixing | ||
Review via email:
|
This proposal has been superseded by a proposal from 2017-05-14.
Commit message
Description of the change
Fix the depreciated code, and refactor it.
Add this to your merge proposal:
-------
lp:~phill-ridout/openlp/import-depreciations (revision 2736)
[SUCCESS] https:/
[SUCCESS] https:/
[SUCCESS] https:/
[SUCCESS] https:/
[FAILURE] https:/
Stopping after failure
To post a comment you must log in.
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'openlp/core/common/__init__.py' | |||
2 | --- openlp/core/common/__init__.py 2016-12-31 11:01:36 +0000 | |||
3 | +++ openlp/core/common/__init__.py 2017-05-14 07:11:54 +0000 | |||
4 | @@ -23,8 +23,9 @@ | |||
5 | 23 | The :mod:`common` module contains most of the components and libraries that make | 23 | The :mod:`common` module contains most of the components and libraries that make |
6 | 24 | OpenLP work. | 24 | OpenLP work. |
7 | 25 | """ | 25 | """ |
8 | 26 | import glob | ||
9 | 26 | import hashlib | 27 | import hashlib |
11 | 27 | 28 | import importlib | |
12 | 28 | import logging | 29 | import logging |
13 | 29 | import os | 30 | import os |
14 | 30 | import re | 31 | import re |
15 | @@ -79,6 +80,38 @@ | |||
16 | 79 | log.exception('failed to check if directory exists or create directory') | 80 | log.exception('failed to check if directory exists or create directory') |
17 | 80 | 81 | ||
18 | 81 | 82 | ||
19 | 83 | def extension_loader(glob_pattern, excluded_files=[]): | ||
20 | 84 | """ | ||
21 | 85 | A utility function to find and load OpenLP extensions, such as plugins, presentation and media controllers and | ||
22 | 86 | importers. | ||
23 | 87 | |||
24 | 88 | :param glob_pattern: A glob pattern used to find the extension(s) to be imported. | ||
25 | 89 | i.e. openlp_app_dir/plugins/*/*plugin.py | ||
26 | 90 | :type glob_pattern: str | ||
27 | 91 | :param excluded_files: A list of file names to exclude that the glob pattern may find. | ||
28 | 92 | :type excluded_files: list of strings | ||
29 | 93 | |||
30 | 94 | :return: None | ||
31 | 95 | :rtype: None | ||
32 | 96 | """ | ||
33 | 97 | for extension_path in glob.iglob(glob_pattern): | ||
34 | 98 | filename = os.path.split(extension_path)[1] | ||
35 | 99 | if filename in excluded_files: | ||
36 | 100 | continue | ||
37 | 101 | module_name = os.path.splitext(filename)[0] | ||
38 | 102 | try: | ||
39 | 103 | loader = importlib.machinery.SourceFileLoader(module_name, extension_path) | ||
40 | 104 | loader.load_module() | ||
41 | 105 | # TODO: A better way to do this (once we drop python 3.4 support) | ||
42 | 106 | # spec = importlib.util.spec_from_file_location('what.ever', 'foo.py') | ||
43 | 107 | # module = importlib.util.module_from_spec(spec) | ||
44 | 108 | # spec.loader.exec_module(module) | ||
45 | 109 | except (ImportError, OSError): | ||
46 | 110 | # On some platforms importing vlc.py might cause OSError exceptions. (e.g. Mac OS X) | ||
47 | 111 | log.warning('Failed to import {module_name} on path {extension_path}' | ||
48 | 112 | .format(module_name=module_name, extension_path=extension_path)) | ||
49 | 113 | |||
50 | 114 | |||
51 | 82 | def get_frozen_path(frozen_option, non_frozen_option): | 115 | def get_frozen_path(frozen_option, non_frozen_option): |
52 | 83 | """ | 116 | """ |
53 | 84 | Return a path based on the system status. | 117 | Return a path based on the system status. |
54 | 85 | 118 | ||
55 | === modified file 'openlp/core/lib/pluginmanager.py' | |||
56 | --- openlp/core/lib/pluginmanager.py 2016-12-31 11:01:36 +0000 | |||
57 | +++ openlp/core/lib/pluginmanager.py 2017-05-14 07:11:54 +0000 | |||
58 | @@ -23,10 +23,9 @@ | |||
59 | 23 | Provide plugin management | 23 | Provide plugin management |
60 | 24 | """ | 24 | """ |
61 | 25 | import os | 25 | import os |
62 | 26 | import imp | ||
63 | 27 | 26 | ||
64 | 28 | from openlp.core.lib import Plugin, PluginStatus | 27 | from openlp.core.lib import Plugin, PluginStatus |
66 | 29 | from openlp.core.common import AppLocation, RegistryProperties, OpenLPMixin, RegistryMixin | 28 | from openlp.core.common import AppLocation, RegistryProperties, OpenLPMixin, RegistryMixin, extension_loader |
67 | 30 | 29 | ||
68 | 31 | 30 | ||
69 | 32 | class PluginManager(RegistryMixin, OpenLPMixin, RegistryProperties): | 31 | class PluginManager(RegistryMixin, OpenLPMixin, RegistryProperties): |
70 | @@ -70,32 +69,8 @@ | |||
71 | 70 | """ | 69 | """ |
72 | 71 | Scan a directory for objects inheriting from the ``Plugin`` class. | 70 | Scan a directory for objects inheriting from the ``Plugin`` class. |
73 | 72 | """ | 71 | """ |
100 | 73 | start_depth = len(os.path.abspath(self.base_path).split(os.sep)) | 72 | glob_pattern = os.path.join(self.base_path, '*', '*plugin.py') |
101 | 74 | present_plugin_dir = os.path.join(self.base_path, 'presentations') | 73 | extension_loader(glob_pattern) |
76 | 75 | self.log_debug('finding plugins in {path} at depth {depth:d}'.format(path=self.base_path, depth=start_depth)) | ||
77 | 76 | for root, dirs, files in os.walk(self.base_path): | ||
78 | 77 | for name in files: | ||
79 | 78 | if name.endswith('.py') and not name.startswith('__'): | ||
80 | 79 | path = os.path.abspath(os.path.join(root, name)) | ||
81 | 80 | this_depth = len(path.split(os.sep)) | ||
82 | 81 | if this_depth - start_depth > 2: | ||
83 | 82 | # skip anything lower down | ||
84 | 83 | break | ||
85 | 84 | module_name = name[:-3] | ||
86 | 85 | # import the modules | ||
87 | 86 | self.log_debug('Importing {name} from {root}. Depth {depth:d}'.format(name=module_name, | ||
88 | 87 | root=root, | ||
89 | 88 | depth=this_depth)) | ||
90 | 89 | try: | ||
91 | 90 | # Use the "imp" library to try to get around a problem with the PyUNO library which | ||
92 | 91 | # monkey-patches the __import__ function to do some magic. This causes issues with our tests. | ||
93 | 92 | # First, try to find the module we want to import, searching the directory in root | ||
94 | 93 | fp, path_name, description = imp.find_module(module_name, [root]) | ||
95 | 94 | # Then load the module (do the actual import) using the details from find_module() | ||
96 | 95 | imp.load_module(module_name, fp, path_name, description) | ||
97 | 96 | except ImportError as e: | ||
98 | 97 | self.log_exception('Failed to import module {name} on path {path}: ' | ||
99 | 98 | '{args}'.format(name=module_name, path=path, args=e.args[0])) | ||
102 | 99 | plugin_classes = Plugin.__subclasses__() | 74 | plugin_classes = Plugin.__subclasses__() |
103 | 100 | plugin_objects = [] | 75 | plugin_objects = [] |
104 | 101 | for p in plugin_classes: | 76 | for p in plugin_classes: |
105 | 102 | 77 | ||
106 | === modified file 'openlp/core/ui/media/mediacontroller.py' | |||
107 | --- openlp/core/ui/media/mediacontroller.py 2016-12-31 11:01:36 +0000 | |||
108 | +++ openlp/core/ui/media/mediacontroller.py 2017-05-14 07:11:54 +0000 | |||
109 | @@ -28,7 +28,8 @@ | |||
110 | 28 | import datetime | 28 | import datetime |
111 | 29 | from PyQt5 import QtCore, QtWidgets | 29 | from PyQt5 import QtCore, QtWidgets |
112 | 30 | 30 | ||
114 | 31 | from openlp.core.common import OpenLPMixin, Registry, RegistryMixin, RegistryProperties, Settings, UiStrings, translate | 31 | from openlp.core.common import OpenLPMixin, Registry, RegistryMixin, RegistryProperties, Settings, UiStrings, \ |
115 | 32 | extension_loader, translate | ||
116 | 32 | from openlp.core.lib import ItemCapabilities | 33 | from openlp.core.lib import ItemCapabilities |
117 | 33 | from openlp.core.lib.ui import critical_error_message_box | 34 | from openlp.core.lib.ui import critical_error_message_box |
118 | 34 | from openlp.core.common import AppLocation | 35 | from openlp.core.common import AppLocation |
119 | @@ -39,6 +40,7 @@ | |||
120 | 39 | parse_optical_path | 40 | parse_optical_path |
121 | 40 | from openlp.core.ui.lib.toolbar import OpenLPToolbar | 41 | from openlp.core.ui.lib.toolbar import OpenLPToolbar |
122 | 41 | 42 | ||
123 | 43 | |||
124 | 42 | log = logging.getLogger(__name__) | 44 | log = logging.getLogger(__name__) |
125 | 43 | 45 | ||
126 | 44 | TICK_TIME = 200 | 46 | TICK_TIME = 200 |
127 | @@ -173,18 +175,8 @@ | |||
128 | 173 | """ | 175 | """ |
129 | 174 | log.debug('_check_available_media_players') | 176 | log.debug('_check_available_media_players') |
130 | 175 | controller_dir = os.path.join(AppLocation.get_directory(AppLocation.AppDir), 'core', 'ui', 'media') | 177 | controller_dir = os.path.join(AppLocation.get_directory(AppLocation.AppDir), 'core', 'ui', 'media') |
143 | 176 | for filename in os.listdir(controller_dir): | 178 | glob_pattern = os.path.join(controller_dir, '*player.py') |
144 | 177 | if filename.endswith('player.py') and filename != 'mediaplayer.py': | 179 | extension_loader(glob_pattern, ['mediaplayer.py']) |
133 | 178 | path = os.path.join(controller_dir, filename) | ||
134 | 179 | if os.path.isfile(path): | ||
135 | 180 | module_name = 'openlp.core.ui.media.' + os.path.splitext(filename)[0] | ||
136 | 181 | log.debug('Importing controller %s', module_name) | ||
137 | 182 | try: | ||
138 | 183 | __import__(module_name, globals(), locals(), []) | ||
139 | 184 | # On some platforms importing vlc.py might cause | ||
140 | 185 | # also OSError exceptions. (e.g. Mac OS X) | ||
141 | 186 | except (ImportError, OSError): | ||
142 | 187 | log.warning('Failed to import %s on path %s', module_name, path) | ||
145 | 188 | player_classes = MediaPlayer.__subclasses__() | 180 | player_classes = MediaPlayer.__subclasses__() |
146 | 189 | for player_class in player_classes: | 181 | for player_class in player_classes: |
147 | 190 | self.register_players(player_class(self)) | 182 | self.register_players(player_class(self)) |
148 | 191 | 183 | ||
149 | === modified file 'openlp/plugins/presentations/lib/impresscontroller.py' | |||
150 | --- openlp/plugins/presentations/lib/impresscontroller.py 2016-12-31 11:01:36 +0000 | |||
151 | +++ openlp/plugins/presentations/lib/impresscontroller.py 2017-05-14 07:11:54 +0000 | |||
152 | @@ -58,7 +58,8 @@ | |||
153 | 58 | 58 | ||
154 | 59 | from openlp.core.lib import ScreenList | 59 | from openlp.core.lib import ScreenList |
155 | 60 | from openlp.core.common import get_uno_command, get_uno_instance | 60 | from openlp.core.common import get_uno_command, get_uno_instance |
157 | 61 | from .presentationcontroller import PresentationController, PresentationDocument, TextType | 61 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument, \ |
158 | 62 | TextType | ||
159 | 62 | 63 | ||
160 | 63 | 64 | ||
161 | 64 | log = logging.getLogger(__name__) | 65 | log = logging.getLogger(__name__) |
162 | 65 | 66 | ||
163 | === modified file 'openlp/plugins/presentations/lib/pdfcontroller.py' | |||
164 | --- openlp/plugins/presentations/lib/pdfcontroller.py 2016-12-31 11:01:36 +0000 | |||
165 | +++ openlp/plugins/presentations/lib/pdfcontroller.py 2017-05-14 07:11:54 +0000 | |||
166 | @@ -29,7 +29,7 @@ | |||
167 | 29 | from openlp.core.common import AppLocation, check_binary_exists | 29 | from openlp.core.common import AppLocation, check_binary_exists |
168 | 30 | from openlp.core.common import Settings, is_win | 30 | from openlp.core.common import Settings, is_win |
169 | 31 | from openlp.core.lib import ScreenList | 31 | from openlp.core.lib import ScreenList |
171 | 32 | from .presentationcontroller import PresentationController, PresentationDocument | 32 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument |
172 | 33 | 33 | ||
173 | 34 | if is_win(): | 34 | if is_win(): |
174 | 35 | from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW | 35 | from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW |
175 | 36 | 36 | ||
176 | === modified file 'openlp/plugins/presentations/lib/powerpointcontroller.py' | |||
177 | --- openlp/plugins/presentations/lib/powerpointcontroller.py 2016-12-31 11:01:36 +0000 | |||
178 | +++ openlp/plugins/presentations/lib/powerpointcontroller.py 2017-05-14 07:11:54 +0000 | |||
179 | @@ -43,7 +43,7 @@ | |||
180 | 43 | from openlp.core.lib import ScreenList | 43 | from openlp.core.lib import ScreenList |
181 | 44 | from openlp.core.lib.ui import UiStrings, critical_error_message_box, translate | 44 | from openlp.core.lib.ui import UiStrings, critical_error_message_box, translate |
182 | 45 | from openlp.core.common import trace_error_handler, Registry | 45 | from openlp.core.common import trace_error_handler, Registry |
184 | 46 | from .presentationcontroller import PresentationController, PresentationDocument | 46 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument |
185 | 47 | 47 | ||
186 | 48 | log = logging.getLogger(__name__) | 48 | log = logging.getLogger(__name__) |
187 | 49 | 49 | ||
188 | 50 | 50 | ||
189 | === modified file 'openlp/plugins/presentations/lib/pptviewcontroller.py' | |||
190 | --- openlp/plugins/presentations/lib/pptviewcontroller.py 2016-12-31 11:01:36 +0000 | |||
191 | +++ openlp/plugins/presentations/lib/pptviewcontroller.py 2017-05-14 07:11:54 +0000 | |||
192 | @@ -35,7 +35,7 @@ | |||
193 | 35 | 35 | ||
194 | 36 | from openlp.core.common import AppLocation | 36 | from openlp.core.common import AppLocation |
195 | 37 | from openlp.core.lib import ScreenList | 37 | from openlp.core.lib import ScreenList |
197 | 38 | from .presentationcontroller import PresentationController, PresentationDocument | 38 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument |
198 | 39 | 39 | ||
199 | 40 | 40 | ||
200 | 41 | log = logging.getLogger(__name__) | 41 | log = logging.getLogger(__name__) |
201 | 42 | 42 | ||
202 | === modified file 'openlp/plugins/presentations/lib/presentationtab.py' | |||
203 | --- openlp/plugins/presentations/lib/presentationtab.py 2016-12-31 11:01:36 +0000 | |||
204 | +++ openlp/plugins/presentations/lib/presentationtab.py 2017-05-14 07:11:54 +0000 | |||
205 | @@ -25,7 +25,7 @@ | |||
206 | 25 | from openlp.core.common import Settings, UiStrings, translate | 25 | from openlp.core.common import Settings, UiStrings, translate |
207 | 26 | from openlp.core.lib import SettingsTab, build_icon | 26 | from openlp.core.lib import SettingsTab, build_icon |
208 | 27 | from openlp.core.lib.ui import critical_error_message_box | 27 | from openlp.core.lib.ui import critical_error_message_box |
210 | 28 | from .pdfcontroller import PdfController | 28 | from openlp.plugins.presentations.lib.pdfcontroller import PdfController |
211 | 29 | 29 | ||
212 | 30 | 30 | ||
213 | 31 | class PresentationTab(SettingsTab): | 31 | class PresentationTab(SettingsTab): |
214 | 32 | 32 | ||
215 | === modified file 'openlp/plugins/presentations/presentationplugin.py' | |||
216 | --- openlp/plugins/presentations/presentationplugin.py 2016-12-31 11:01:36 +0000 | |||
217 | +++ openlp/plugins/presentations/presentationplugin.py 2017-05-14 07:11:54 +0000 | |||
218 | @@ -20,19 +20,18 @@ | |||
219 | 20 | # Temple Place, Suite 330, Boston, MA 02111-1307 USA # | 20 | # Temple Place, Suite 330, Boston, MA 02111-1307 USA # |
220 | 21 | ############################################################################### | 21 | ############################################################################### |
221 | 22 | """ | 22 | """ |
224 | 23 | The :mod:`presentationplugin` module provides the ability for OpenLP to display presentations from a variety of document | 23 | The :mod:`openlp.plugins.presentations.presentationplugin` module provides the ability for OpenLP to display |
225 | 24 | formats. | 24 | presentations from a variety of document formats. |
226 | 25 | """ | 25 | """ |
227 | 26 | import os | 26 | import os |
228 | 27 | import logging | 27 | import logging |
229 | 28 | 28 | ||
230 | 29 | from PyQt5 import QtCore | 29 | from PyQt5 import QtCore |
231 | 30 | 30 | ||
233 | 31 | from openlp.core.common import AppLocation, translate | 31 | from openlp.core.common import AppLocation, extension_loader, translate |
234 | 32 | from openlp.core.lib import Plugin, StringContent, build_icon | 32 | from openlp.core.lib import Plugin, StringContent, build_icon |
235 | 33 | from openlp.plugins.presentations.lib import PresentationController, PresentationMediaItem, PresentationTab | 33 | from openlp.plugins.presentations.lib import PresentationController, PresentationMediaItem, PresentationTab |
236 | 34 | 34 | ||
237 | 35 | |||
238 | 36 | log = logging.getLogger(__name__) | 35 | log = logging.getLogger(__name__) |
239 | 37 | 36 | ||
240 | 38 | 37 | ||
241 | @@ -123,16 +122,8 @@ | |||
242 | 123 | """ | 122 | """ |
243 | 124 | log.debug('check_pre_conditions') | 123 | log.debug('check_pre_conditions') |
244 | 125 | controller_dir = os.path.join(AppLocation.get_directory(AppLocation.PluginsDir), 'presentations', 'lib') | 124 | controller_dir = os.path.join(AppLocation.get_directory(AppLocation.PluginsDir), 'presentations', 'lib') |
255 | 126 | for filename in os.listdir(controller_dir): | 125 | glob_pattern = os.path.join(controller_dir, '*controller.py') |
256 | 127 | if filename.endswith('controller.py') and filename != 'presentationcontroller.py': | 126 | extension_loader(glob_pattern, ['presentationcontroller.py']) |
247 | 128 | path = os.path.join(controller_dir, filename) | ||
248 | 129 | if os.path.isfile(path): | ||
249 | 130 | module_name = 'openlp.plugins.presentations.lib.' + os.path.splitext(filename)[0] | ||
250 | 131 | log.debug('Importing controller {name}'.format(name=module_name)) | ||
251 | 132 | try: | ||
252 | 133 | __import__(module_name, globals(), locals(), []) | ||
253 | 134 | except ImportError: | ||
254 | 135 | log.warning('Failed to import {name} on path {path}'.format(name=module_name, path=path)) | ||
257 | 136 | controller_classes = PresentationController.__subclasses__() | 127 | controller_classes = PresentationController.__subclasses__() |
258 | 137 | for controller_class in controller_classes: | 128 | for controller_class in controller_classes: |
259 | 138 | controller = controller_class(self) | 129 | controller = controller_class(self) |
260 | 139 | 130 | ||
261 | === modified file 'tests/functional/openlp_core_common/test_common.py' | |||
262 | --- tests/functional/openlp_core_common/test_common.py 2017-04-24 05:17:55 +0000 | |||
263 | +++ tests/functional/openlp_core_common/test_common.py 2017-05-14 07:11:54 +0000 | |||
264 | @@ -23,10 +23,10 @@ | |||
265 | 23 | Functional tests to test the AppLocation class and related methods. | 23 | Functional tests to test the AppLocation class and related methods. |
266 | 24 | """ | 24 | """ |
267 | 25 | from unittest import TestCase | 25 | from unittest import TestCase |
269 | 26 | from unittest.mock import MagicMock, patch | 26 | from unittest.mock import MagicMock, call, patch |
270 | 27 | 27 | ||
273 | 28 | from openlp.core.common import check_directory_exists, de_hump, trace_error_handler, translate, is_win, is_macosx, \ | 28 | from openlp.core.common import check_directory_exists, clean_button_text, de_hump, extension_loader, is_macosx, \ |
274 | 29 | is_linux, clean_button_text | 29 | is_linux, is_win, trace_error_handler, translate |
275 | 30 | 30 | ||
276 | 31 | 31 | ||
277 | 32 | class TestCommonFunctions(TestCase): | 32 | class TestCommonFunctions(TestCase): |
278 | @@ -72,6 +72,68 @@ | |||
279 | 72 | mocked_exists.assert_called_with(directory_to_check) | 72 | mocked_exists.assert_called_with(directory_to_check) |
280 | 73 | self.assertRaises(ValueError, check_directory_exists, directory_to_check) | 73 | self.assertRaises(ValueError, check_directory_exists, directory_to_check) |
281 | 74 | 74 | ||
282 | 75 | def test_extension_loader_no_files_found(self): | ||
283 | 76 | """ | ||
284 | 77 | Test the `extension_loader` function when no files are found | ||
285 | 78 | """ | ||
286 | 79 | # GIVEN: A mocked `iglob` function which does not match any files | ||
287 | 80 | with patch('openlp.core.common.glob.iglob', return_value=[]), \ | ||
288 | 81 | patch('openlp.core.common.importlib.machinery.SourceFileLoader') as mocked_source_file_loader: | ||
289 | 82 | |||
290 | 83 | # WHEN: Calling `extension_loader` | ||
291 | 84 | extension_loader('glob', ['file2.py', 'file3.py']) | ||
292 | 85 | |||
293 | 86 | # THEN: `extension_loader` should not try to import any files | ||
294 | 87 | self.assertFalse(mocked_source_file_loader.called) | ||
295 | 88 | |||
296 | 89 | def test_extension_loader_files_found(self): | ||
297 | 90 | """ | ||
298 | 91 | Test the `extension_loader` function when it successfully finds and loads some files | ||
299 | 92 | """ | ||
300 | 93 | # GIVEN: A mocked `iglob` function which returns a list of files | ||
301 | 94 | with patch('openlp.core.common.glob.iglob', return_value=['import_dir/file1.py', 'import_dir/file2.py', | ||
302 | 95 | 'import_dir/file3.py', 'import_dir/file4.py']), \ | ||
303 | 96 | patch('openlp.core.common.importlib.machinery.SourceFileLoader') as mocked_source_file_loader: | ||
304 | 97 | |||
305 | 98 | # WHEN: Calling `extension_loader` with a list of files to exclude | ||
306 | 99 | extension_loader('glob', ['file2.py', 'file3.py']) | ||
307 | 100 | |||
308 | 101 | # THEN: `extension_loader` should only try to import the files that are matched by the blob, excluding the | ||
309 | 102 | # files listed in the `excluded_files` argument | ||
310 | 103 | mocked_source_file_loader.assert_has_calls([call('file1', 'import_dir/file1.py'), call().load_module(), | ||
311 | 104 | call('file4', 'import_dir/file4.py'), call().load_module()]) | ||
312 | 105 | |||
313 | 106 | def test_extension_loader_import_error(self): | ||
314 | 107 | """ | ||
315 | 108 | Test the `extension_loader` function when `SourceFileLoader` raises a `ImportError` | ||
316 | 109 | """ | ||
317 | 110 | # GIVEN: A mocked `SourceFileLoader` which raises an `ImportError` | ||
318 | 111 | with patch('openlp.core.common.glob.iglob', return_value=['import_dir/file1.py', 'import_dir/file2.py', | ||
319 | 112 | 'import_dir/file3.py', 'import_dir/file4.py']), \ | ||
320 | 113 | patch('openlp.core.common.importlib.machinery.SourceFileLoader', side_effect=ImportError()), \ | ||
321 | 114 | patch('openlp.core.common.log') as mocked_logger: | ||
322 | 115 | |||
323 | 116 | # WHEN: Calling `extension_loader` | ||
324 | 117 | extension_loader('glob') | ||
325 | 118 | |||
326 | 119 | # THEN: The `ImportError` should be caught and logged | ||
327 | 120 | self.assertTrue(mocked_logger.warning.called) | ||
328 | 121 | |||
329 | 122 | def test_extension_loader_os_error(self): | ||
330 | 123 | """ | ||
331 | 124 | Test the `extension_loader` function when `SourceFileLoader` raises a `ImportError` | ||
332 | 125 | """ | ||
333 | 126 | # GIVEN: A mocked `SourceFileLoader` which raises an `OSError` | ||
334 | 127 | with patch('openlp.core.common.glob.iglob', return_value=['import_dir/file1.py']), \ | ||
335 | 128 | patch('openlp.core.common.importlib.machinery.SourceFileLoader', side_effect=OSError()), \ | ||
336 | 129 | patch('openlp.core.common.log') as mocked_logger: | ||
337 | 130 | |||
338 | 131 | # WHEN: Calling `extension_loader` | ||
339 | 132 | extension_loader('glob') | ||
340 | 133 | |||
341 | 134 | # THEN: The `OSError` should be caught and logged | ||
342 | 135 | self.assertTrue(mocked_logger.warning.called) | ||
343 | 136 | |||
344 | 75 | def test_de_hump_conversion(self): | 137 | def test_de_hump_conversion(self): |
345 | 76 | """ | 138 | """ |
346 | 77 | Test the de_hump function with a class name | 139 | Test the de_hump function with a class name |
Looks good but has issues with trunk!