| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
from importlib import import_module, reload |
| 29 |
from itertools import chain |
| 30 |
import re |
| 31 |
import sys |
| 32 |
|
| 33 |
from apprise import ( |
| 34 |
AttachmentManager, |
| 35 |
ConfigurationManager, |
| 36 |
NotificationManager, |
| 37 |
) |
| 38 |
|
| 39 |
# Grant access to our Notification Manager Singleton |
| 40 |
N_MGR = NotificationManager() |
| 41 |
|
| 42 |
# Grant access to our Attachment Manager Singleton |
| 43 |
A_MGR = AttachmentManager() |
| 44 |
|
| 45 |
# Grant access to our Configuration Manager Singleton |
| 46 |
C_MGR = ConfigurationManager() |
| 47 |
|
| 48 |
# For filtering our result when scanning a module |
| 49 |
# Identify any items below we should match on that we can freely |
| 50 |
# directly copy around between our modules. This should only |
| 51 |
# catch class/function/variables we want to allow explicity |
| 52 |
# copy/paste access with |
| 53 |
module_filter_re = re.compile( |
| 54 |
r"^(?P<name>(Notify|Config|Attach)[A-Za-z0-9]+)$" |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def reload_plugin(name): |
| 59 |
"""Reload built-in plugin module, e.g. `NotifyGnome`. |
| 60 |
|
| 61 |
Reloading plugin modules is needed when testing module-level code of |
| 62 |
notification plugins. |
| 63 |
|
| 64 |
See also |
| 65 |
https://stackoverflow.com/questions/31363311. |
| 66 |
""" |
| 67 |
|
| 68 |
A_MGR.unload_modules() |
| 69 |
|
| 70 |
reload(sys.modules["apprise.apprise_attachment"]) |
| 71 |
reload(sys.modules["apprise.attachment.base"]) |
| 72 |
new_apprise_attachment_mod = import_module("apprise.apprise_attachment") |
| 73 |
new_apprise_attach_base_mod = import_module("apprise.attachment.base") |
| 74 |
reload(sys.modules["apprise.manager_attachment"]) |
| 75 |
|
| 76 |
module_pyname = f"{N_MGR.module_name_prefix}.{name}" |
| 77 |
if module_pyname in sys.modules: |
| 78 |
reload(sys.modules[module_pyname]) |
| 79 |
new_notify_mod = import_module(module_pyname) |
| 80 |
|
| 81 |
A_MGR.unload_modules() |
| 82 |
|
| 83 |
reload(sys.modules["apprise.apprise_config"]) |
| 84 |
reload(sys.modules["apprise.config.base"]) |
| 85 |
new_apprise_configuration_mod = import_module("apprise.apprise_config") |
| 86 |
new_apprise_config_base_mod = import_module("apprise.config.base") |
| 87 |
reload(sys.modules["apprise.manager_config"]) |
| 88 |
|
| 89 |
C_MGR.unload_modules() |
| 90 |
|
| 91 |
module_pyname = f"{N_MGR.module_name_prefix}.{name}" |
| 92 |
if module_pyname in sys.modules: |
| 93 |
reload(sys.modules[module_pyname]) |
| 94 |
new_notify_mod = import_module(module_pyname) |
| 95 |
|
| 96 |
# Detect our class object |
| 97 |
class_matches = {} |
| 98 |
for class_name in [ |
| 99 |
obj for obj in dir(new_notify_mod) if module_filter_re.match(obj) |
| 100 |
]: |
| 101 |
# Store our entry |
| 102 |
class_matches[class_name] = getattr(new_notify_mod, class_name) |
| 103 |
|
| 104 |
# the user running the tests did not correctly use reload_plugin() or |
| 105 |
# they did, but libraries around them have shifted. We need to error out |
| 106 |
# so the test can be fixed |
| 107 |
if not class_matches: |
| 108 |
raise AttributeError(f"Module {name} has no URLBase defined in it!") |
| 109 |
|
| 110 |
reload(sys.modules["apprise.manager_plugins"]) |
| 111 |
reload(sys.modules["apprise.apprise"]) |
| 112 |
reload(sys.modules["apprise.utils"]) |
| 113 |
reload(sys.modules["apprise.locale"]) |
| 114 |
reload(sys.modules["apprise"]) |
| 115 |
|
| 116 |
# Acquire all of the test files we have |
| 117 |
tests = [k for k in sys.modules if re.match(r"^test_.+$", k)] |
| 118 |
|
| 119 |
# Iterate over all of our test modules |
| 120 |
for module_name in tests: |
| 121 |
# Filter the test files by only those using the class_name we found |
| 122 |
# within our module |
| 123 |
possible_matches = [ |
| 124 |
m |
| 125 |
for m in dir(sys.modules[module_name]) |
| 126 |
if re.match( |
| 127 |
"^(?P<name>{})$".format("|".join(class_matches.keys())), m |
| 128 |
) |
| 129 |
] |
| 130 |
if not possible_matches: |
| 131 |
continue |
| 132 |
|
| 133 |
# if we get here, we have test_ files that utilize the Class we just |
| 134 |
# reloaded |
| 135 |
|
| 136 |
# Fix reference to new plugin class in given module. |
| 137 |
# Needed for updating the module-level import reference like |
| 138 |
# `from apprise.plugins.NotifyABCDE import NotifyABCDE`. |
| 139 |
# |
| 140 |
# We reload NotifyABCDE and place it back in its spot |
| 141 |
test_mod = import_module(module_name) |
| 142 |
for class_name, class_plugin in class_matches.items(): |
| 143 |
if hasattr(test_mod, class_name): |
| 144 |
setattr(test_mod, class_name, class_plugin) |
| 145 |
|
| 146 |
# |
| 147 |
# Detect our Apprise Modules (include helpers) |
| 148 |
# |
| 149 |
apprise_modules = sorted( |
| 150 |
[k for k in sys.modules if re.match(r"^(apprise|helpers)(\.|.+)$", k)], |
| 151 |
reverse=True, |
| 152 |
) |
| 153 |
|
| 154 |
# |
| 155 |
# This section below reloads our attachment classes |
| 156 |
# |
| 157 |
|
| 158 |
for entry in A_MGR: |
| 159 |
reload(sys.modules[entry["path"]]) |
| 160 |
for module_pyname in chain(apprise_modules, tests): |
| 161 |
detect = re.compile( |
| 162 |
r"^(?P<name>(AppriseAttachment|AttachBase|" |
| 163 |
+ entry["path"].split(".")[-1] |
| 164 |
+ r"))$" |
| 165 |
) |
| 166 |
|
| 167 |
possible_matches = [ |
| 168 |
m for m in dir(sys.modules[module_pyname]) if detect.match(m) |
| 169 |
] |
| 170 |
if not possible_matches: |
| 171 |
continue |
| 172 |
|
| 173 |
apprise_mod = import_module(module_pyname) |
| 174 |
# Fix reference to new plugin class in given module. |
| 175 |
# Needed for updating the module-level import reference |
| 176 |
# like `from apprise.<etc> import AttachABCDE`. |
| 177 |
# |
| 178 |
# We reload NotifyABCDE and place it back in its spot |
| 179 |
# new_attach = import_module(entry['path']) |
| 180 |
for name in possible_matches: |
| 181 |
if name == "AppriseAttachment": |
| 182 |
setattr( |
| 183 |
apprise_mod, |
| 184 |
name, |
| 185 |
getattr(new_apprise_attachment_mod, name), |
| 186 |
) |
| 187 |
|
| 188 |
elif name == "AttachBase": |
| 189 |
setattr( |
| 190 |
apprise_mod, |
| 191 |
name, |
| 192 |
getattr(new_apprise_attach_base_mod, name), |
| 193 |
) |
| 194 |
|
| 195 |
else: |
| 196 |
module_pyname = f"{A_MGR.module_name_prefix}.{name}" |
| 197 |
new_attach_mod = import_module(module_pyname) |
| 198 |
|
| 199 |
# Detect our class object |
| 200 |
class_matches = {} |
| 201 |
for class_name in [ |
| 202 |
obj |
| 203 |
for obj in dir(new_attach_mod) |
| 204 |
if module_filter_re.match(obj) |
| 205 |
]: |
| 206 |
# Store our entry |
| 207 |
class_matches[class_name] = getattr( |
| 208 |
new_attach_mod, class_name |
| 209 |
) |
| 210 |
|
| 211 |
for class_name, class_plugin in class_matches.items(): |
| 212 |
if hasattr(apprise_mod, class_name): |
| 213 |
setattr(apprise_mod, class_name, class_plugin) |
| 214 |
|
| 215 |
# |
| 216 |
# This section below reloads our configuration classes |
| 217 |
# |
| 218 |
|
| 219 |
for entry in C_MGR: |
| 220 |
reload(sys.modules[entry["path"]]) |
| 221 |
for module_pyname in chain(apprise_modules, tests): |
| 222 |
detect = re.compile( |
| 223 |
r"^(?P<name>(AppriseConfig|ConfigBase|" |
| 224 |
+ entry["path"].split(".")[-1] |
| 225 |
+ r"))$" |
| 226 |
) |
| 227 |
|
| 228 |
possible_matches = [ |
| 229 |
m for m in dir(sys.modules[module_pyname]) if detect.match(m) |
| 230 |
] |
| 231 |
if not possible_matches: |
| 232 |
continue |
| 233 |
|
| 234 |
apprise_mod = import_module(module_pyname) |
| 235 |
# Fix reference to new plugin class in given module. |
| 236 |
# Needed for updating the module-level import reference |
| 237 |
# like `from apprise.<etc> import ConfigABCDE`. |
| 238 |
# |
| 239 |
# We reload NotifyABCDE and place it back in its spot |
| 240 |
# new_attach = import_module(entry['path']) |
| 241 |
for name in possible_matches: |
| 242 |
if name == "AppriseConfig": |
| 243 |
setattr( |
| 244 |
apprise_mod, |
| 245 |
name, |
| 246 |
getattr(new_apprise_configuration_mod, name), |
| 247 |
) |
| 248 |
|
| 249 |
elif name == "ConfigBase": |
| 250 |
setattr( |
| 251 |
apprise_mod, |
| 252 |
name, |
| 253 |
getattr(new_apprise_config_base_mod, name), |
| 254 |
) |
| 255 |
|
| 256 |
else: |
| 257 |
module_pyname = f"{A_MGR.module_name_prefix}.{name}" |
| 258 |
new_config_mod = import_module(module_pyname) |
| 259 |
|
| 260 |
# Detect our class object |
| 261 |
class_matches = {} |
| 262 |
for class_name in [ |
| 263 |
obj |
| 264 |
for obj in dir(new_config_mod) |
| 265 |
if module_filter_re.match(obj) |
| 266 |
]: |
| 267 |
# Store our entry |
| 268 |
class_matches[class_name] = getattr( |
| 269 |
new_config_mod, class_name |
| 270 |
) |
| 271 |
|
| 272 |
for class_name, class_plugin in class_matches.items(): |
| 273 |
if hasattr(apprise_mod, class_name): |
| 274 |
setattr(apprise_mod, class_name, class_plugin) |