Skip to content

Commit

Permalink
Remove some usage of globals()
Browse files Browse the repository at this point in the history
  • Loading branch information
QuLogic authored and Nick-Hall committed Dec 23, 2024
1 parent 9571318 commit 5d8ea82
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
49 changes: 29 additions & 20 deletions gramps/gen/plug/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,37 @@ def version_str_to_tup(sversion, positions):
return tup[:positions]


class newplugin:
class FakeRegistrar:
"""
Fake newplugin.
Fakes ``register`` and ``newplugin``.
"""

def __init__(self):
globals()["register_results"].append({})
self.results = []

def __setattr__(self, attr, value):
globals()["register_results"][-1][attr] = value
@property
def newplugin(self):
class _newplugin:
"""
Fake newplugin.
"""

def __init__(inner_self):
self.results.append({})

def register(ptype, **kwargs):
"""
Fake registration. Side-effect sets register_results to kwargs.
"""
retval = {"ptype": ptype}
retval.update(kwargs)
# Get the results back to calling function
if "register_results" in globals():
globals()["register_results"].append(retval)
else:
globals()["register_results"] = [retval]
def __setattr__(inner_self, attr, value):
self.results[-1][attr] = value

return _newplugin

def register(self, ptype, **kwargs):
"""
Fake registration. Side-effect sets results to kwargs.
"""
retval = {"ptype": ptype}
retval.update(kwargs)
# Get the results back to calling function
self.results.append(retval)


class Zipfile:
Expand Down Expand Up @@ -380,12 +388,13 @@ def load_addon_file(path, callback=None):
if callback:
callback((_("Examining '%s'...") % gpr_file) + "\n")
contents = file_obj.extractfile(gpr_file).read()
registrar = FakeRegistrar()
# Put a fake register and _ function in environment:
env = make_environment(
register=register, newplugin=newplugin, _=lambda text: text
register=registrar.register,
newplugin=registrar.newplugin,
_=lambda text: text,
)
# clear out the result variable:
globals()["register_results"] = []
# evaluate the contents:
try:
exec(contents, env)
Expand All @@ -395,7 +404,7 @@ def load_addon_file(path, callback=None):
callback(" " + msg + "\n" + str(exp))
continue
# There can be multiple addons per gpr file:
for results in globals()["register_results"]:
for results in registrar.results:
gramps_target_version = results.get("gramps_target_version", None)
id = results.get("id", None)
if gramps_target_version:
Expand Down
14 changes: 3 additions & 11 deletions gramps/plugins/test/db_undo_and_signals_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,17 +449,9 @@ def test_one(self):
self.assertEqual(cm_padd_key, None, msg="Callback Manager disconnect cb check")


params = [("SQLite", "sqlite")]

for name, param in params:
cls_name = "TestMyTestClass_%s" % (name,)
globals()[cls_name] = type(
cls_name,
(DbTestClassBase, unittest.TestCase),
{
"param": param,
},
)
class TestSQLite(DbTestClassBase, unittest.TestCase):
param = "sqlite"


if __name__ == "__main__":
unittest.main()

0 comments on commit 5d8ea82

Please sign in to comment.