-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement xml_mode() in a new irods.helpers module
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import functools | ||
import inspect | ||
import types | ||
import contextlib | ||
from ..test.helpers import make_session as make_test_session | ||
from irods.message import (ET, XML_Parser_Type) | ||
|
||
|
||
# See https://stackoverflow.com/questions/67028108/overriding-function-signature-in-help-when-using-functools-wraps | ||
def update_func(f, func_name='', update_kwargs: dict = None): | ||
"""Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)""" | ||
g = types.FunctionType( | ||
code=f.__code__, | ||
globals=f.__globals__.copy(), | ||
name=f.__name__, | ||
argdefs=f.__defaults__, | ||
closure=f.__closure__ | ||
) | ||
|
||
g = functools.update_wrapper(g, f) | ||
g.__signature__ = inspect.signature(g) | ||
g.__kwdefaults__ = (f.__kwdefaults__ or {}).copy() | ||
|
||
# Adjust your arguments | ||
for key, value in (update_kwargs or {}).items(): | ||
g.__kwdefaults__[key] = value | ||
g.__signature__.parameters[key]._default = value | ||
|
||
g.__name__ = func_name or g.__name__ | ||
return g | ||
|
||
|
||
make_session = update_func(make_test_session, | ||
update_kwargs = dict(test_server_version = False)) | ||
|
||
|
||
@contextlib.contextmanager | ||
def xml_mode(s): | ||
try: | ||
if isinstance(s,str): | ||
ET(getattr(XML_Parser_Type,s)) # e.g. xml_mode("QUASI_XML") | ||
elif isinstance(s,(int,XML_Parser_Type)): | ||
ET(s) # e.g. xml_mode(2) or xml_mode(XML_Parser_Type.QUASI_XML) | ||
else: | ||
msg = "xml_mode argument must be a string (e.g. 'QUASI_XML'), XML_Parser_Type enum, or int." | ||
raise ValueError(msg) | ||
yield | ||
finally: | ||
ET(None) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters