aboutsummaryrefslogtreecommitdiff
path: root/sgdfi/_decode.py
diff options
context:
space:
mode:
Diffstat (limited to 'sgdfi/_decode.py')
-rwxr-xr-xsgdfi/_decode.py76
1 files changed, 53 insertions, 23 deletions
diff --git a/sgdfi/_decode.py b/sgdfi/_decode.py
index a097a99..3a80b44 100755
--- a/sgdfi/_decode.py
+++ b/sgdfi/_decode.py
@@ -130,7 +130,12 @@ class Decoder:
`content`: the content as a stream, bytes or text object.
`type`: the MIME type.
`hint`: the hint, including the context (e.g.
- `intranet_place`). """
+ `intranet_place`).
+
+ Special hints:
+ - `ignore`: ignore the content.
+ - `raw`: return the raw content.
+ - `sic`: return the type decoded content. """
if hint == 'ignore':
return None
@@ -170,13 +175,17 @@ class Decoder:
if not hint:
raise ValueError(f"Missing HTML hint")
- try:
- func = getattr(self, f"_decode_html_{hint}")
- except AttributeError:
- msg = f"Unknown HTML hint: {repr(hint)}"
- raise ValueError(msg) from None
+ elif hint != 'sic':
+ try:
+ func = getattr(self, f"_decode_html_{hint}")
+ except AttributeError:
+ msg = f"Unknown HTML hint: {repr(hint)}"
+ raise ValueError(msg) from None
content = _BeautifulSoup(inp, 'lxml')
+ if hint == 'sic':
+ return content
+
result = func(content)
elif type == 'application/x-microsoft-ajax':
# The input might be a stream.
@@ -207,6 +216,9 @@ class Decoder:
resp.append(_AjaxField(code, name, attrib, text))
+ if hint == 'sic':
+ return resp
+
# The answer's payload usually is in the second entry.
# The code may allow to determinate what it corresponds to,
# but a lot of codes can be used depending on the actions
@@ -253,11 +265,12 @@ class Decoder:
# Try to find out for the hint.
- try:
- assert hint != None
- func = getattr(self, f"_decode_csv_{hint}")
- except (AssertionError, AttributeError):
- raise ValueError(f"Invalid CSV hint: {hint}") from None
+ if hint != 'sic':
+ try:
+ assert hint != None
+ func = getattr(self, f"_decode_csv_{hint}")
+ except (AssertionError, AttributeError):
+ raise ValueError(f"Invalid CSV hint: {hint}") from None
# Decode the file through the CSV reader, and send it to the
# function.
@@ -265,6 +278,9 @@ class Decoder:
reader = _csvreader(content, delimiter = ';')
resp = [row for row in reader]
+ if hint == 'sic':
+ return resp
+
return func(resp)
elif type == 'application/vnd.ms-excel': # XLS document.
# The input might be of different type, we want a text stream.
@@ -278,11 +294,12 @@ class Decoder:
# Get the hint.
- try:
- assert hint != None
- func = getattr(self, f"_decode_xls_{hint}")
- except (AssertionError, AttributeError):
- raise ValueError(f"Invalid XLS hint: {hint}") from None
+ if hint != 'sic':
+ try:
+ assert hint != None
+ func = getattr(self, f"_decode_xls_{hint}")
+ except (AssertionError, AttributeError):
+ raise ValueError(f"Invalid XLS hint: {hint}") from None
# Read the entries through pandas and call the function.
@@ -300,18 +317,27 @@ class Decoder:
break
resp = [e for e in entries(content)]
+ if hint == 'sic':
+ return resp
+
return func(resp)
elif type == 'text/xml':
# We ought to read the XML document through BeautifulSoup.
+ # If the first element is a '<string>', then we suppose the
+ # content is JSON.
content = _BeautifulSoup(inp, 'lxml')
+ if next(content.body.children).name == 'string':
+ content = _jsonloads(next(content.body.children).text)
+
+ if hint == 'sic':
+ return hint
# And here we manage this manually.
# TODO: there is a more elegant solution.
if hint == 'intranet_functions':
- data = _jsonloads(tree.getroot().text)
- return self.__decode_json_intranet_functions(data)
+ return self.__decode_json_intranet_functions(content)
else:
raise ValueError("Unknown XML hint: {repr(hint)}")
elif type == 'application/json':
@@ -329,11 +355,12 @@ class Decoder:
# Try to find out for the hint.
- try:
- assert hint != None
- func = getattr(self, f"_decode_json_{hint}")
- except (AssertionError, AttributeError):
- raise ValueError(f"Invalid JSON hint: {hint}") from None
+ if hint != 'sic':
+ try:
+ assert hint != None
+ func = getattr(self, f"_decode_json_{hint}")
+ except (AssertionError, AttributeError):
+ raise ValueError(f"Invalid JSON hint: {hint}") from None
# Then load using the standard json module.
# If it is a {'d': '…'} payload, it is a WebService answer and
@@ -348,6 +375,9 @@ class Decoder:
except:
pass
+ if hint == 'sic':
+ return data
+
return func(data)
else:
raise ValueError(f"unknown type: {repr(type)}")