# Path: beyond/io/ccsds/commons.py
# class CcsdsError(ParseError):
#     pass
# 
# def detect2load(string):
#     """Detect the type and format of the CCSDS file
# 
#     types may be : "OPM", "OMM", "OEM", "TDM"
#     format may be: "kvn" or "xml"
#     """
# 
#     format = "kvn" if string.lstrip().startswith("CCSDS_") else "xml"
# 
#     m = re.search(r"CCSDS_([A-Z]{3})_VERS", string, re.M)
# 
#     if m and m.group(1) in ["OPM", "OMM", "OEM", "TDM"]:
#         type = m.group(1).lower()
#     elif m:
#         raise CcsdsError(f"Unknown CCSDS type : {m}")
#     else:
#         raise CcsdsError("Unknown CCSDS type")
# 
#     return type, format
# 
# def detect2dump(data, **kwargs):
#     """Detect the type of the input data and provide the type of ccsds
#     file to generate
# 
#     Args:
#         data (Ephem or List[Ephem] or StateVector or MeasureSet)
#     Return:
#         str: type of data (e.g. "oem", "opm", "omm", etc.)
#     Raise:
#         TypeError: for unknown type detected
#     """
# 
#     if isinstance(data, Ephem) or (
#         isinstance(data, Iterable) and all(isinstance(x, Ephem) for x in data)
#     ):
#         type = "oem"
#     elif isinstance(data, StateVector):
#         if (
#             isinstance(data, Orbit)
#             and isinstance(data.propagator, AnalyticalPropagator)
#             and data.frame == TEME
#             and data.form is TLE
#         ):
#             type = "omm"
#         else:
#             type = "opm"
#     elif isinstance(data, Iterable) and all(isinstance(x, Measure) for x in data):
#         type = "tdm"
#     else:
#         raise TypeError("Unknown object type")
# 
#     return type


from ...config import config
from . import opm
from . import oem
from . import omm
from . import tdm
from .commons import CcsdsError, detect2load, detect2dump
    the optional osculating keplerian elements will be added to the OPM.

    It is possible to set the configuration dict to change the default value
    of 'fmt'.

    .. code-block:: python

        from beyond.config import config
        config["io"] = {"ccsds_default_format": "xml"}
    """
    fp.write(dumps(data, **kwargs))


def dumps(data, **kwargs):
    """Create a string CCSDS representation of the object

    Same arguments and behaviour as :py:func:`dump`
    """

    type = detect2dump(data)

    if type == "oem":
        content = oem.dumps(data, **kwargs)
    elif type == "opm":
        content = opm.dumps(data, **kwargs)
    elif type == "omm":
        content = omm.dumps(data, **kwargs)
    elif type == "tdm":
        content = tdm.dumps(data, **kwargs)
    else:  # pragma: no cover

# Path: beyond/io/ccsds/commons.py
# class CcsdsError(ParseError):
#     pass
# 
# def detect2load(string):
#     """Detect the type and format of the CCSDS file
# 
#     types may be : "OPM", "OMM", "OEM", "TDM"
#     format may be: "kvn" or "xml"
#     """
# 
#     format = "kvn" if string.lstrip().startswith("CCSDS_") else "xml"
# 
#     m = re.search(r"CCSDS_([A-Z]{3})_VERS", string, re.M)
# 
#     if m and m.group(1) in ["OPM", "OMM", "OEM", "TDM"]:
#         type = m.group(1).lower()
#     elif m:
#         raise CcsdsError(f"Unknown CCSDS type : {m}")
#     else:
#         raise CcsdsError("Unknown CCSDS type")
# 
#     return type, format
# 
# def detect2dump(data, **kwargs):
#     """Detect the type of the input data and provide the type of ccsds
#     file to generate
# 
#     Args:
#         data (Ephem or List[Ephem] or StateVector or MeasureSet)
#     Return:
#         str: type of data (e.g. "oem", "opm", "omm", etc.)
#     Raise:
#         TypeError: for unknown type detected
#     """
# 
#     if isinstance(data, Ephem) or (
#         isinstance(data, Iterable) and all(isinstance(x, Ephem) for x in data)
#     ):
#         type = "oem"
#     elif isinstance(data, StateVector):
#         if (
#             isinstance(data, Orbit)
#             and isinstance(data.propagator, AnalyticalPropagator)
#             and data.frame == TEME
#             and data.form is TLE
#         ):
#             type = "omm"
#         else:
#             type = "opm"
#     elif isinstance(data, Iterable) and all(isinstance(x, Measure) for x in data):
#         type = "tdm"
#     else:
#         raise TypeError("Unknown object type")
# 
#     return type


# Path: beyond/io/ccsds/ccsds.py
from ...config import config
from . import opm
from . import oem
from . import omm
from . import tdm
from .commons import CcsdsError, detect2load, detect2dump

    the optional osculating keplerian elements will be added to the OPM.

    It is possible to set the configuration dict to change the default value
    of 'fmt'.

    .. code-block:: python

        from beyond.config import config
        config["io"] = {"ccsds_default_format": "xml"}
    """
    fp.write(dumps(data, **kwargs))


def dumps(data, **kwargs):
    """Create a string CCSDS representation of the object

    Same arguments and behaviour as :py:func:`dump`
    """

    type = detect2dump(data)

    if type == "oem":
        content = oem.dumps(data, **kwargs)
    elif type == "opm":
        content = opm.dumps(data, **kwargs)
    elif type == "omm":
        content = omm.dumps(data, **kwargs)
    elif type == "tdm":
        content = tdm.dumps(data, **kwargs)
    else:  # pragma: no cover

        raise CcsdsError(f"Unknown object type for CCSDS : {type}")