about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/azure/core/serialization.py
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/azure/core/serialization.py
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-4a52a71956a8d46fcb7294ac71734504bb09bcc2.tar.gz
two version of R2R are here HEAD master
Diffstat (limited to '.venv/lib/python3.12/site-packages/azure/core/serialization.py')
-rw-r--r--.venv/lib/python3.12/site-packages/azure/core/serialization.py125
1 files changed, 125 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/azure/core/serialization.py b/.venv/lib/python3.12/site-packages/azure/core/serialization.py
new file mode 100644
index 00000000..705ffbf0
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/azure/core/serialization.py
@@ -0,0 +1,125 @@
+# coding=utf-8
+# --------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See License.txt in the project root for
+# license information.
+# --------------------------------------------------------------------------
+import base64
+from json import JSONEncoder
+from typing import Union, cast, Any
+from datetime import datetime, date, time, timedelta
+from datetime import timezone
+
+
+__all__ = ["NULL", "AzureJSONEncoder"]
+TZ_UTC = timezone.utc
+
+
+class _Null:
+    """To create a Falsy object"""
+
+    def __bool__(self) -> bool:
+        return False
+
+
+NULL = _Null()
+"""
+A falsy sentinel object which is supposed to be used to specify attributes
+with no data. This gets serialized to `null` on the wire.
+"""
+
+
+def _timedelta_as_isostr(td: timedelta) -> str:
+    """Converts a datetime.timedelta object into an ISO 8601 formatted string, e.g. 'P4DT12H30M05S'
+
+    Function adapted from the Tin Can Python project: https://github.com/RusticiSoftware/TinCanPython
+
+    :param td: The timedelta object to convert
+    :type td: datetime.timedelta
+    :return: An ISO 8601 formatted string representing the timedelta object
+    :rtype: str
+    """
+
+    # Split seconds to larger units
+    seconds = td.total_seconds()
+    minutes, seconds = divmod(seconds, 60)
+    hours, minutes = divmod(minutes, 60)
+    days, hours = divmod(hours, 24)
+
+    days, hours, minutes = list(map(int, (days, hours, minutes)))
+    seconds = round(seconds, 6)
+
+    # Build date
+    date_str = ""
+    if days:
+        date_str = "%sD" % days
+
+    # Build time
+    time_str = "T"
+
+    # Hours
+    bigger_exists = date_str or hours
+    if bigger_exists:
+        time_str += "{:02}H".format(hours)
+
+    # Minutes
+    bigger_exists = bigger_exists or minutes
+    if bigger_exists:
+        time_str += "{:02}M".format(minutes)
+
+    # Seconds
+    try:
+        if seconds.is_integer():
+            seconds_string = "{:02}".format(int(seconds))
+        else:
+            # 9 chars long w/ leading 0, 6 digits after decimal
+            seconds_string = "%09.6f" % seconds
+            # Remove trailing zeros
+            seconds_string = seconds_string.rstrip("0")
+    except AttributeError:  # int.is_integer() raises
+        seconds_string = "{:02}".format(seconds)
+
+    time_str += "{}S".format(seconds_string)
+
+    return "P" + date_str + time_str
+
+
+def _datetime_as_isostr(dt: Union[datetime, date, time, timedelta]) -> str:
+    """Converts a datetime.(datetime|date|time|timedelta) object into an ISO 8601 formatted string.
+
+    :param dt: The datetime object to convert
+    :type dt: datetime.datetime or datetime.date or datetime.time or datetime.timedelta
+    :return: An ISO 8601 formatted string representing the datetime object
+    :rtype: str
+    """
+    # First try datetime.datetime
+    if hasattr(dt, "year") and hasattr(dt, "hour"):
+        dt = cast(datetime, dt)
+        # astimezone() fails for naive times in Python 2.7, so make make sure dt is aware (tzinfo is set)
+        if not dt.tzinfo:
+            iso_formatted = dt.replace(tzinfo=TZ_UTC).isoformat()
+        else:
+            iso_formatted = dt.astimezone(TZ_UTC).isoformat()
+        # Replace the trailing "+00:00" UTC offset with "Z" (RFC 3339: https://www.ietf.org/rfc/rfc3339.txt)
+        return iso_formatted.replace("+00:00", "Z")
+    # Next try datetime.date or datetime.time
+    try:
+        dt = cast(Union[date, time], dt)
+        return dt.isoformat()
+    # Last, try datetime.timedelta
+    except AttributeError:
+        dt = cast(timedelta, dt)
+        return _timedelta_as_isostr(dt)
+
+
+class AzureJSONEncoder(JSONEncoder):
+    """A JSON encoder that's capable of serializing datetime objects and bytes."""
+
+    def default(self, o: Any) -> Any:
+        if isinstance(o, (bytes, bytearray)):
+            return base64.b64encode(o).decode()
+        try:
+            return _datetime_as_isostr(o)
+        except AttributeError:
+            pass
+        return super(AzureJSONEncoder, self).default(o)