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: |
405 lines (+152/-68) 10 files modified
openlp/core/common/__init__.py (+45/-1) openlp/core/lib/pluginmanager.py (+3/-28) openlp/core/ui/media/mediacontroller.py (+6/-14) 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 (+6/-15) tests/functional/openlp_core_common/test_common.py (+86/-5) |
To merge this branch: | bzr merge lp:~phill-ridout/openlp/import-depreciations |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Phill | Needs Fixing | ||
Tim Bentley | Pending | ||
Review via email: mp+324025@code.launchpad.net |
This proposal supersedes a proposal from 2017-05-13.
This proposal has been superseded by a proposal from 2017-05-15.
Commit message
Description of the change
Fix the depreciated code, and refactor it.
lp:~phill-ridout/openlp/import-depreciations (revision 2737)
[SUCCESS] https:/
[SUCCESS] https:/
[SUCCESS] https:/
[SUCCESS] https:/
[FAILURE] https:/
Stopping after failure
To post a comment you must log in.
Revision history for this message
Tim Bentley (trb143) wrote : Posted in a previous version of this proposal | # |
review:
Needs Fixing
Revision history for this message
Phill (phill-ridout) wrote : | # |
I had merged with trunk, but forgot to update trunk!
Revision history for this message
Phill (phill-ridout) : | # |
review:
Needs Fixing
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-15 10:24:47 +0000 | |||
4 | @@ -24,7 +24,7 @@ | |||
5 | 24 | OpenLP work. | 24 | OpenLP work. |
6 | 25 | """ | 25 | """ |
7 | 26 | import hashlib | 26 | import hashlib |
9 | 27 | 27 | import importlib | |
10 | 28 | import logging | 28 | import logging |
11 | 29 | import os | 29 | import os |
12 | 30 | import re | 30 | import re |
13 | @@ -32,6 +32,7 @@ | |||
14 | 32 | import traceback | 32 | import traceback |
15 | 33 | from chardet.universaldetector import UniversalDetector | 33 | from chardet.universaldetector import UniversalDetector |
16 | 34 | from ipaddress import IPv4Address, IPv6Address, AddressValueError | 34 | from ipaddress import IPv4Address, IPv6Address, AddressValueError |
17 | 35 | from pathlib import Path | ||
18 | 35 | from shutil import which | 36 | from shutil import which |
19 | 36 | from subprocess import check_output, CalledProcessError, STDOUT | 37 | from subprocess import check_output, CalledProcessError, STDOUT |
20 | 37 | 38 | ||
21 | @@ -79,6 +80,49 @@ | |||
22 | 79 | log.exception('failed to check if directory exists or create directory') | 80 | log.exception('failed to check if directory exists or create directory') |
23 | 80 | 81 | ||
24 | 81 | 82 | ||
25 | 83 | def extension_loader(glob_pattern, excluded_files=[]): | ||
26 | 84 | """ | ||
27 | 85 | A utility function to find and load OpenLP extensions, such as plugins, presentation and media controllers and | ||
28 | 86 | importers. | ||
29 | 87 | |||
30 | 88 | :param glob_pattern: A glob pattern used to find the extension(s) to be imported. Should be relative to the | ||
31 | 89 | application directory. i.e. openlp/plugins/*/*plugin.py | ||
32 | 90 | :type glob_pattern: str | ||
33 | 91 | |||
34 | 92 | :param excluded_files: A list of file names to exclude that the glob pattern may find. | ||
35 | 93 | :type excluded_files: list of strings | ||
36 | 94 | |||
37 | 95 | :return: None | ||
38 | 96 | :rtype: None | ||
39 | 97 | """ | ||
40 | 98 | app_dir = Path(AppLocation.get_directory(AppLocation.AppDir)).parent | ||
41 | 99 | for extension_path in app_dir.glob(glob_pattern): | ||
42 | 100 | extension_path = extension_path.relative_to(app_dir) | ||
43 | 101 | if extension_path.name in excluded_files: | ||
44 | 102 | continue | ||
45 | 103 | module_name = path_to_module(extension_path) | ||
46 | 104 | try: | ||
47 | 105 | importlib.import_module(module_name) | ||
48 | 106 | except (ImportError, OSError): | ||
49 | 107 | # On some platforms importing vlc.py might cause OSError exceptions. (e.g. Mac OS X) | ||
50 | 108 | log.warning('Failed to import {module_name} on path {extension_path}' | ||
51 | 109 | .format(module_name=module_name, extension_path=str(extension_path))) | ||
52 | 110 | |||
53 | 111 | |||
54 | 112 | def path_to_module(path): | ||
55 | 113 | """ | ||
56 | 114 | Convert a path to a module name (i.e openlp.core.common) | ||
57 | 115 | |||
58 | 116 | :param path: The path to convert to a module name. | ||
59 | 117 | :type path: Path | ||
60 | 118 | |||
61 | 119 | :return: The module name. | ||
62 | 120 | :rtype: str | ||
63 | 121 | """ | ||
64 | 122 | module_path = path.with_suffix('') | ||
65 | 123 | return '.'.join(module_path.parts) | ||
66 | 124 | |||
67 | 125 | |||
68 | 82 | def get_frozen_path(frozen_option, non_frozen_option): | 126 | def get_frozen_path(frozen_option, non_frozen_option): |
69 | 83 | """ | 127 | """ |
70 | 84 | Return a path based on the system status. | 128 | Return a path based on the system status. |
71 | 85 | 129 | ||
72 | === modified file 'openlp/core/lib/pluginmanager.py' | |||
73 | --- openlp/core/lib/pluginmanager.py 2016-12-31 11:01:36 +0000 | |||
74 | +++ openlp/core/lib/pluginmanager.py 2017-05-15 10:24:47 +0000 | |||
75 | @@ -23,10 +23,9 @@ | |||
76 | 23 | Provide plugin management | 23 | Provide plugin management |
77 | 24 | """ | 24 | """ |
78 | 25 | import os | 25 | import os |
79 | 26 | import imp | ||
80 | 27 | 26 | ||
81 | 28 | from openlp.core.lib import Plugin, PluginStatus | 27 | from openlp.core.lib import Plugin, PluginStatus |
83 | 29 | from openlp.core.common import AppLocation, RegistryProperties, OpenLPMixin, RegistryMixin | 28 | from openlp.core.common import AppLocation, RegistryProperties, OpenLPMixin, RegistryMixin, extension_loader |
84 | 30 | 29 | ||
85 | 31 | 30 | ||
86 | 32 | class PluginManager(RegistryMixin, OpenLPMixin, RegistryProperties): | 31 | class PluginManager(RegistryMixin, OpenLPMixin, RegistryProperties): |
87 | @@ -70,32 +69,8 @@ | |||
88 | 70 | """ | 69 | """ |
89 | 71 | Scan a directory for objects inheriting from the ``Plugin`` class. | 70 | Scan a directory for objects inheriting from the ``Plugin`` class. |
90 | 72 | """ | 71 | """ |
117 | 73 | start_depth = len(os.path.abspath(self.base_path).split(os.sep)) | 72 | glob_pattern = os.path.join('openlp', 'plugins', '*', '*plugin.py') |
118 | 74 | present_plugin_dir = os.path.join(self.base_path, 'presentations') | 73 | extension_loader(glob_pattern) |
93 | 75 | self.log_debug('finding plugins in {path} at depth {depth:d}'.format(path=self.base_path, depth=start_depth)) | ||
94 | 76 | for root, dirs, files in os.walk(self.base_path): | ||
95 | 77 | for name in files: | ||
96 | 78 | if name.endswith('.py') and not name.startswith('__'): | ||
97 | 79 | path = os.path.abspath(os.path.join(root, name)) | ||
98 | 80 | this_depth = len(path.split(os.sep)) | ||
99 | 81 | if this_depth - start_depth > 2: | ||
100 | 82 | # skip anything lower down | ||
101 | 83 | break | ||
102 | 84 | module_name = name[:-3] | ||
103 | 85 | # import the modules | ||
104 | 86 | self.log_debug('Importing {name} from {root}. Depth {depth:d}'.format(name=module_name, | ||
105 | 87 | root=root, | ||
106 | 88 | depth=this_depth)) | ||
107 | 89 | try: | ||
108 | 90 | # Use the "imp" library to try to get around a problem with the PyUNO library which | ||
109 | 91 | # monkey-patches the __import__ function to do some magic. This causes issues with our tests. | ||
110 | 92 | # First, try to find the module we want to import, searching the directory in root | ||
111 | 93 | fp, path_name, description = imp.find_module(module_name, [root]) | ||
112 | 94 | # Then load the module (do the actual import) using the details from find_module() | ||
113 | 95 | imp.load_module(module_name, fp, path_name, description) | ||
114 | 96 | except ImportError as e: | ||
115 | 97 | self.log_exception('Failed to import module {name} on path {path}: ' | ||
116 | 98 | '{args}'.format(name=module_name, path=path, args=e.args[0])) | ||
119 | 99 | plugin_classes = Plugin.__subclasses__() | 74 | plugin_classes = Plugin.__subclasses__() |
120 | 100 | plugin_objects = [] | 75 | plugin_objects = [] |
121 | 101 | for p in plugin_classes: | 76 | for p in plugin_classes: |
122 | 102 | 77 | ||
123 | === modified file 'openlp/core/ui/media/mediacontroller.py' | |||
124 | --- openlp/core/ui/media/mediacontroller.py 2016-12-31 11:01:36 +0000 | |||
125 | +++ openlp/core/ui/media/mediacontroller.py 2017-05-15 10:24:47 +0000 | |||
126 | @@ -28,7 +28,8 @@ | |||
127 | 28 | import datetime | 28 | import datetime |
128 | 29 | from PyQt5 import QtCore, QtWidgets | 29 | from PyQt5 import QtCore, QtWidgets |
129 | 30 | 30 | ||
131 | 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, \ |
132 | 32 | extension_loader, translate | ||
133 | 32 | from openlp.core.lib import ItemCapabilities | 33 | from openlp.core.lib import ItemCapabilities |
134 | 33 | from openlp.core.lib.ui import critical_error_message_box | 34 | from openlp.core.lib.ui import critical_error_message_box |
135 | 34 | from openlp.core.common import AppLocation | 35 | from openlp.core.common import AppLocation |
136 | @@ -39,6 +40,7 @@ | |||
137 | 39 | parse_optical_path | 40 | parse_optical_path |
138 | 40 | from openlp.core.ui.lib.toolbar import OpenLPToolbar | 41 | from openlp.core.ui.lib.toolbar import OpenLPToolbar |
139 | 41 | 42 | ||
140 | 43 | |||
141 | 42 | log = logging.getLogger(__name__) | 44 | log = logging.getLogger(__name__) |
142 | 43 | 45 | ||
143 | 44 | TICK_TIME = 200 | 46 | TICK_TIME = 200 |
144 | @@ -172,19 +174,9 @@ | |||
145 | 172 | Check to see if we have any media Player's available. | 174 | Check to see if we have any media Player's available. |
146 | 173 | """ | 175 | """ |
147 | 174 | log.debug('_check_available_media_players') | 176 | log.debug('_check_available_media_players') |
161 | 175 | controller_dir = os.path.join(AppLocation.get_directory(AppLocation.AppDir), 'core', 'ui', 'media') | 177 | controller_dir = os.path.join('openlp', 'core', 'ui', 'media') |
162 | 176 | for filename in os.listdir(controller_dir): | 178 | glob_pattern = os.path.join(controller_dir, '*player.py') |
163 | 177 | if filename.endswith('player.py') and filename != 'mediaplayer.py': | 179 | extension_loader(glob_pattern, ['mediaplayer.py']) |
151 | 178 | path = os.path.join(controller_dir, filename) | ||
152 | 179 | if os.path.isfile(path): | ||
153 | 180 | module_name = 'openlp.core.ui.media.' + os.path.splitext(filename)[0] | ||
154 | 181 | log.debug('Importing controller %s', module_name) | ||
155 | 182 | try: | ||
156 | 183 | __import__(module_name, globals(), locals(), []) | ||
157 | 184 | # On some platforms importing vlc.py might cause | ||
158 | 185 | # also OSError exceptions. (e.g. Mac OS X) | ||
159 | 186 | except (ImportError, OSError): | ||
160 | 187 | log.warning('Failed to import %s on path %s', module_name, path) | ||
164 | 188 | player_classes = MediaPlayer.__subclasses__() | 180 | player_classes = MediaPlayer.__subclasses__() |
165 | 189 | for player_class in player_classes: | 181 | for player_class in player_classes: |
166 | 190 | self.register_players(player_class(self)) | 182 | self.register_players(player_class(self)) |
167 | 191 | 183 | ||
168 | === modified file 'openlp/plugins/presentations/lib/impresscontroller.py' | |||
169 | --- openlp/plugins/presentations/lib/impresscontroller.py 2016-12-31 11:01:36 +0000 | |||
170 | +++ openlp/plugins/presentations/lib/impresscontroller.py 2017-05-15 10:24:47 +0000 | |||
171 | @@ -58,7 +58,8 @@ | |||
172 | 58 | 58 | ||
173 | 59 | from openlp.core.lib import ScreenList | 59 | from openlp.core.lib import ScreenList |
174 | 60 | from openlp.core.common import get_uno_command, get_uno_instance | 60 | from openlp.core.common import get_uno_command, get_uno_instance |
176 | 61 | from .presentationcontroller import PresentationController, PresentationDocument, TextType | 61 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument, \ |
177 | 62 | TextType | ||
178 | 62 | 63 | ||
179 | 63 | 64 | ||
180 | 64 | log = logging.getLogger(__name__) | 65 | log = logging.getLogger(__name__) |
181 | 65 | 66 | ||
182 | === modified file 'openlp/plugins/presentations/lib/pdfcontroller.py' | |||
183 | --- openlp/plugins/presentations/lib/pdfcontroller.py 2016-12-31 11:01:36 +0000 | |||
184 | +++ openlp/plugins/presentations/lib/pdfcontroller.py 2017-05-15 10:24:47 +0000 | |||
185 | @@ -29,7 +29,7 @@ | |||
186 | 29 | from openlp.core.common import AppLocation, check_binary_exists | 29 | from openlp.core.common import AppLocation, check_binary_exists |
187 | 30 | from openlp.core.common import Settings, is_win | 30 | from openlp.core.common import Settings, is_win |
188 | 31 | from openlp.core.lib import ScreenList | 31 | from openlp.core.lib import ScreenList |
190 | 32 | from .presentationcontroller import PresentationController, PresentationDocument | 32 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument |
191 | 33 | 33 | ||
192 | 34 | if is_win(): | 34 | if is_win(): |
193 | 35 | from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW | 35 | from subprocess import STARTUPINFO, STARTF_USESHOWWINDOW |
194 | 36 | 36 | ||
195 | === modified file 'openlp/plugins/presentations/lib/powerpointcontroller.py' | |||
196 | --- openlp/plugins/presentations/lib/powerpointcontroller.py 2016-12-31 11:01:36 +0000 | |||
197 | +++ openlp/plugins/presentations/lib/powerpointcontroller.py 2017-05-15 10:24:47 +0000 | |||
198 | @@ -43,7 +43,7 @@ | |||
199 | 43 | from openlp.core.lib import ScreenList | 43 | from openlp.core.lib import ScreenList |
200 | 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 |
201 | 45 | from openlp.core.common import trace_error_handler, Registry | 45 | from openlp.core.common import trace_error_handler, Registry |
203 | 46 | from .presentationcontroller import PresentationController, PresentationDocument | 46 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument |
204 | 47 | 47 | ||
205 | 48 | log = logging.getLogger(__name__) | 48 | log = logging.getLogger(__name__) |
206 | 49 | 49 | ||
207 | 50 | 50 | ||
208 | === modified file 'openlp/plugins/presentations/lib/pptviewcontroller.py' | |||
209 | --- openlp/plugins/presentations/lib/pptviewcontroller.py 2016-12-31 11:01:36 +0000 | |||
210 | +++ openlp/plugins/presentations/lib/pptviewcontroller.py 2017-05-15 10:24:47 +0000 | |||
211 | @@ -35,7 +35,7 @@ | |||
212 | 35 | 35 | ||
213 | 36 | from openlp.core.common import AppLocation | 36 | from openlp.core.common import AppLocation |
214 | 37 | from openlp.core.lib import ScreenList | 37 | from openlp.core.lib import ScreenList |
216 | 38 | from .presentationcontroller import PresentationController, PresentationDocument | 38 | from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument |
217 | 39 | 39 | ||
218 | 40 | 40 | ||
219 | 41 | log = logging.getLogger(__name__) | 41 | log = logging.getLogger(__name__) |
220 | 42 | 42 | ||
221 | === modified file 'openlp/plugins/presentations/lib/presentationtab.py' | |||
222 | --- openlp/plugins/presentations/lib/presentationtab.py 2016-12-31 11:01:36 +0000 | |||
223 | +++ openlp/plugins/presentations/lib/presentationtab.py 2017-05-15 10:24:47 +0000 | |||
224 | @@ -25,7 +25,7 @@ | |||
225 | 25 | from openlp.core.common import Settings, UiStrings, translate | 25 | from openlp.core.common import Settings, UiStrings, translate |
226 | 26 | from openlp.core.lib import SettingsTab, build_icon | 26 | from openlp.core.lib import SettingsTab, build_icon |
227 | 27 | from openlp.core.lib.ui import critical_error_message_box | 27 | from openlp.core.lib.ui import critical_error_message_box |
229 | 28 | from .pdfcontroller import PdfController | 28 | from openlp.plugins.presentations.lib.pdfcontroller import PdfController |
230 | 29 | 29 | ||
231 | 30 | 30 | ||
232 | 31 | class PresentationTab(SettingsTab): | 31 | class PresentationTab(SettingsTab): |
233 | 32 | 32 | ||
234 | === modified file 'openlp/plugins/presentations/presentationplugin.py' | |||
235 | --- openlp/plugins/presentations/presentationplugin.py 2016-12-31 11:01:36 +0000 | |||
236 | +++ openlp/plugins/presentations/presentationplugin.py 2017-05-15 10:24:47 +0000 | |||
237 | @@ -20,19 +20,18 @@ | |||
238 | 20 | # Temple Place, Suite 330, Boston, MA 02111-1307 USA # | 20 | # Temple Place, Suite 330, Boston, MA 02111-1307 USA # |
239 | 21 | ############################################################################### | 21 | ############################################################################### |
240 | 22 | """ | 22 | """ |
243 | 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 |
244 | 24 | formats. | 24 | presentations from a variety of document formats. |
245 | 25 | """ | 25 | """ |
246 | 26 | import os | 26 | import os |
247 | 27 | import logging | 27 | import logging |
248 | 28 | 28 | ||
249 | 29 | from PyQt5 import QtCore | 29 | from PyQt5 import QtCore |
250 | 30 | 30 | ||
252 | 31 | from openlp.core.common import AppLocation, translate | 31 | from openlp.core.common import AppLocation, extension_loader, translate |
253 | 32 | from openlp.core.lib import Plugin, StringContent, build_icon | 32 | from openlp.core.lib import Plugin, StringContent, build_icon |
254 | 33 | from openlp.plugins.presentations.lib import PresentationController, PresentationMediaItem, PresentationTab | 33 | from openlp.plugins.presentations.lib import PresentationController, PresentationMediaItem, PresentationTab |
255 | 34 | 34 | ||
256 | 35 | |||
257 | 36 | log = logging.getLogger(__name__) | 35 | log = logging.getLogger(__name__) |
258 | 37 | 36 | ||
259 | 38 | 37 | ||
260 | @@ -122,17 +121,9 @@ | |||
261 | 122 | Check to see if we have any presentation software available. If not do not install the plugin. | 121 | Check to see if we have any presentation software available. If not do not install the plugin. |
262 | 123 | """ | 122 | """ |
263 | 124 | log.debug('check_pre_conditions') | 123 | log.debug('check_pre_conditions') |
275 | 125 | controller_dir = os.path.join(AppLocation.get_directory(AppLocation.PluginsDir), 'presentations', 'lib') | 124 | controller_dir = os.path.join('openlp', 'plugins', 'presentations', 'lib') |
276 | 126 | for filename in os.listdir(controller_dir): | 125 | glob_pattern = os.path.join(controller_dir, '*controller.py') |
277 | 127 | if filename.endswith('controller.py') and filename != 'presentationcontroller.py': | 126 | extension_loader(glob_pattern, ['presentationcontroller.py']) |
267 | 128 | path = os.path.join(controller_dir, filename) | ||
268 | 129 | if os.path.isfile(path): | ||
269 | 130 | module_name = 'openlp.plugins.presentations.lib.' + os.path.splitext(filename)[0] | ||
270 | 131 | log.debug('Importing controller {name}'.format(name=module_name)) | ||
271 | 132 | try: | ||
272 | 133 | __import__(module_name, globals(), locals(), []) | ||
273 | 134 | except ImportError: | ||
274 | 135 | log.warning('Failed to import {name} on path {path}'.format(name=module_name, path=path)) | ||
278 | 136 | controller_classes = PresentationController.__subclasses__() | 127 | controller_classes = PresentationController.__subclasses__() |
279 | 137 | for controller_class in controller_classes: | 128 | for controller_class in controller_classes: |
280 | 138 | controller = controller_class(self) | 129 | controller = controller_class(self) |
281 | 139 | 130 | ||
282 | === modified file 'tests/functional/openlp_core_common/test_common.py' | |||
283 | --- tests/functional/openlp_core_common/test_common.py 2017-04-24 05:17:55 +0000 | |||
284 | +++ tests/functional/openlp_core_common/test_common.py 2017-05-15 10:24:47 +0000 | |||
285 | @@ -22,11 +22,13 @@ | |||
286 | 22 | """ | 22 | """ |
287 | 23 | Functional tests to test the AppLocation class and related methods. | 23 | Functional tests to test the AppLocation class and related methods. |
288 | 24 | """ | 24 | """ |
289 | 25 | from pathlib import Path | ||
290 | 25 | from unittest import TestCase | 26 | from unittest import TestCase |
292 | 26 | from unittest.mock import MagicMock, patch | 27 | from unittest.mock import MagicMock, call, patch |
293 | 27 | 28 | ||
296 | 28 | from openlp.core.common import check_directory_exists, de_hump, trace_error_handler, translate, is_win, is_macosx, \ | 29 | from openlp.core import common |
297 | 29 | is_linux, clean_button_text | 30 | from openlp.core.common import check_directory_exists, clean_button_text, de_hump, extension_loader, is_macosx, \ |
298 | 31 | is_linux, is_win, path_to_module, trace_error_handler, translate | ||
299 | 30 | 32 | ||
300 | 31 | 33 | ||
301 | 32 | class TestCommonFunctions(TestCase): | 34 | class TestCommonFunctions(TestCase): |
302 | @@ -72,6 +74,72 @@ | |||
303 | 72 | mocked_exists.assert_called_with(directory_to_check) | 74 | mocked_exists.assert_called_with(directory_to_check) |
304 | 73 | self.assertRaises(ValueError, check_directory_exists, directory_to_check) | 75 | self.assertRaises(ValueError, check_directory_exists, directory_to_check) |
305 | 74 | 76 | ||
306 | 77 | def test_extension_loader_no_files_found(self): | ||
307 | 78 | """ | ||
308 | 79 | Test the `extension_loader` function when no files are found | ||
309 | 80 | """ | ||
310 | 81 | # GIVEN: A mocked `Path.glob` method which does not match any files | ||
311 | 82 | with patch('openlp.core.common.AppLocation.get_directory', return_value='/app/dir/openlp'), \ | ||
312 | 83 | patch.object(common.Path, 'glob', return_value=[]), \ | ||
313 | 84 | patch('openlp.core.common.importlib.import_module') as mocked_import_module: | ||
314 | 85 | |||
315 | 86 | # WHEN: Calling `extension_loader` | ||
316 | 87 | extension_loader('glob', ['file2.py', 'file3.py']) | ||
317 | 88 | |||
318 | 89 | # THEN: `extension_loader` should not try to import any files | ||
319 | 90 | self.assertFalse(mocked_import_module.called) | ||
320 | 91 | |||
321 | 92 | def test_extension_loader_files_found(self): | ||
322 | 93 | """ | ||
323 | 94 | Test the `extension_loader` function when it successfully finds and loads some files | ||
324 | 95 | """ | ||
325 | 96 | # GIVEN: A mocked `Path.glob` method which returns a list of files | ||
326 | 97 | with patch('openlp.core.common.AppLocation.get_directory', return_value='/app/dir/openlp'), \ | ||
327 | 98 | patch.object(common.Path, 'glob', return_value=[Path('/app/dir/openlp/import_dir/file1.py'), | ||
328 | 99 | Path('/app/dir/openlp/import_dir/file2.py'), | ||
329 | 100 | Path('/app/dir/openlp/import_dir/file3.py'), | ||
330 | 101 | Path('/app/dir/openlp/import_dir/file4.py')]), \ | ||
331 | 102 | patch('openlp.core.common.importlib.import_module') as mocked_import_module: | ||
332 | 103 | |||
333 | 104 | # WHEN: Calling `extension_loader` with a list of files to exclude | ||
334 | 105 | extension_loader('glob', ['file2.py', 'file3.py']) | ||
335 | 106 | |||
336 | 107 | # THEN: `extension_loader` should only try to import the files that are matched by the blob, excluding the | ||
337 | 108 | # files listed in the `excluded_files` argument | ||
338 | 109 | mocked_import_module.assert_has_calls([call('openlp.import_dir.file1'), call('openlp.import_dir.file4')]) | ||
339 | 110 | |||
340 | 111 | def test_extension_loader_import_error(self): | ||
341 | 112 | """ | ||
342 | 113 | Test the `extension_loader` function when `SourceFileLoader` raises a `ImportError` | ||
343 | 114 | """ | ||
344 | 115 | # GIVEN: A mocked `import_module` which raises an `ImportError` | ||
345 | 116 | with patch('openlp.core.common.AppLocation.get_directory', return_value='/app/dir/openlp'), \ | ||
346 | 117 | patch.object(common.Path, 'glob', return_value=[Path('/app/dir/openlp/import_dir/file1.py')]), \ | ||
347 | 118 | patch('openlp.core.common.importlib.import_module', side_effect=ImportError()), \ | ||
348 | 119 | patch('openlp.core.common.log') as mocked_logger: | ||
349 | 120 | |||
350 | 121 | # WHEN: Calling `extension_loader` | ||
351 | 122 | extension_loader('glob') | ||
352 | 123 | |||
353 | 124 | # THEN: The `ImportError` should be caught and logged | ||
354 | 125 | self.assertTrue(mocked_logger.warning.called) | ||
355 | 126 | |||
356 | 127 | def test_extension_loader_os_error(self): | ||
357 | 128 | """ | ||
358 | 129 | Test the `extension_loader` function when `import_module` raises a `ImportError` | ||
359 | 130 | """ | ||
360 | 131 | # GIVEN: A mocked `SourceFileLoader` which raises an `OSError` | ||
361 | 132 | with patch('openlp.core.common.AppLocation.get_directory', return_value='/app/dir/openlp'), \ | ||
362 | 133 | patch.object(common.Path, 'glob', return_value=[Path('/app/dir/openlp/import_dir/file1.py')]), \ | ||
363 | 134 | patch('openlp.core.common.importlib.import_module', side_effect=OSError()), \ | ||
364 | 135 | patch('openlp.core.common.log') as mocked_logger: | ||
365 | 136 | |||
366 | 137 | # WHEN: Calling `extension_loader` | ||
367 | 138 | extension_loader('glob') | ||
368 | 139 | |||
369 | 140 | # THEN: The `OSError` should be caught and logged | ||
370 | 141 | self.assertTrue(mocked_logger.warning.called) | ||
371 | 142 | |||
372 | 75 | def test_de_hump_conversion(self): | 143 | def test_de_hump_conversion(self): |
373 | 76 | """ | 144 | """ |
374 | 77 | Test the de_hump function with a class name | 145 | Test the de_hump function with a class name |
375 | @@ -83,7 +151,7 @@ | |||
376 | 83 | new_string = de_hump(string) | 151 | new_string = de_hump(string) |
377 | 84 | 152 | ||
378 | 85 | # THEN: the new string should be converted to python format | 153 | # THEN: the new string should be converted to python format |
380 | 86 | self.assertTrue(new_string == "my_class", 'The class name should have been converted') | 154 | self.assertEqual(new_string, "my_class", 'The class name should have been converted') |
381 | 87 | 155 | ||
382 | 88 | def test_de_hump_static(self): | 156 | def test_de_hump_static(self): |
383 | 89 | """ | 157 | """ |
384 | @@ -96,7 +164,20 @@ | |||
385 | 96 | new_string = de_hump(string) | 164 | new_string = de_hump(string) |
386 | 97 | 165 | ||
387 | 98 | # THEN: the new string should be converted to python format | 166 | # THEN: the new string should be converted to python format |
389 | 99 | self.assertTrue(new_string == "my_class", 'The class name should have been preserved') | 167 | self.assertEqual(new_string, "my_class", 'The class name should have been preserved') |
390 | 168 | |||
391 | 169 | def test_path_to_module(self): | ||
392 | 170 | """ | ||
393 | 171 | Test `path_to_module` when supplied with a `Path` object | ||
394 | 172 | """ | ||
395 | 173 | # GIVEN: A `Path` object | ||
396 | 174 | path = Path('openlp/core/ui/media/webkitplayer.py') | ||
397 | 175 | |||
398 | 176 | # WHEN: Calling path_to_module with the `Path` object | ||
399 | 177 | result = path_to_module(path) | ||
400 | 178 | |||
401 | 179 | # THEN: path_to_module should return the module name | ||
402 | 180 | self.assertEqual(result, 'openlp.core.ui.media.webkitplayer') | ||
403 | 100 | 181 | ||
404 | 101 | def test_trace_error_handler(self): | 182 | def test_trace_error_handler(self): |
405 | 102 | """ | 183 | """ |
Looks good but has issues with trunk!