mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-25 20:22:36 +01:00
[util] add 'std' object to global eval namespace (#6330)
allows accessing standard library modules (and other external modules) in a more straightforward manner than '__import__(...)' * std.os.getcwd() * std["os"].getcwd()
This commit is contained in:
parent
9b4c94fda8
commit
4667833195
@ -532,6 +532,24 @@ class HTTPBasicAuth():
|
|||||||
return request
|
return request
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleProxy():
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
|
def __getitem__(self, key, modules=sys.modules):
|
||||||
|
try:
|
||||||
|
return modules[key]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
__import__(key)
|
||||||
|
except ImportError:
|
||||||
|
modules[key] = NONE
|
||||||
|
return NONE
|
||||||
|
return modules[key]
|
||||||
|
|
||||||
|
__getattr__ = __getitem__
|
||||||
|
|
||||||
|
|
||||||
class LazyPrompt():
|
class LazyPrompt():
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
@ -540,6 +558,7 @@ class LazyPrompt():
|
|||||||
|
|
||||||
|
|
||||||
class NullContext():
|
class NullContext():
|
||||||
|
__slots__ = ()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return None
|
return None
|
||||||
@ -646,6 +665,7 @@ GLOBALS = {
|
|||||||
"restart" : raises(exception.RestartExtraction),
|
"restart" : raises(exception.RestartExtraction),
|
||||||
"hash_sha1": sha1,
|
"hash_sha1": sha1,
|
||||||
"hash_md5" : md5,
|
"hash_md5" : md5,
|
||||||
|
"std" : ModuleProxy(),
|
||||||
"re" : re,
|
"re" : re,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -830,6 +830,34 @@ def hash(value):
|
|||||||
i += 1
|
i += 1
|
||||||
self.assertEqual(i, 0)
|
self.assertEqual(i, 0)
|
||||||
|
|
||||||
|
def test_module_proxy(self):
|
||||||
|
proxy = util.ModuleProxy()
|
||||||
|
|
||||||
|
self.assertIs(proxy.os, os)
|
||||||
|
self.assertIs(proxy.os.path, os.path)
|
||||||
|
self.assertIs(proxy["os"], os)
|
||||||
|
self.assertIs(proxy["os.path"], os.path)
|
||||||
|
self.assertIs(proxy["os"].path, os.path)
|
||||||
|
|
||||||
|
self.assertIs(proxy.abcdefghi, util.NONE)
|
||||||
|
self.assertIs(proxy["abcdefghi"], util.NONE)
|
||||||
|
self.assertIs(proxy["abc.def.ghi"], util.NONE)
|
||||||
|
self.assertIs(proxy["os.path2"], util.NONE)
|
||||||
|
|
||||||
|
def test_null_context(self):
|
||||||
|
with util.NullContext():
|
||||||
|
pass
|
||||||
|
|
||||||
|
with util.NullContext() as ctx:
|
||||||
|
self.assertIs(ctx, None)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with util.NullContext() as ctx:
|
||||||
|
exc_orig = ValueError()
|
||||||
|
raise exc_orig
|
||||||
|
except ValueError as exc:
|
||||||
|
self.assertIs(exc, exc_orig)
|
||||||
|
|
||||||
|
|
||||||
class TestExtractor():
|
class TestExtractor():
|
||||||
category = "test_category"
|
category = "test_category"
|
||||||
|
Loading…
Reference in New Issue
Block a user