# Path: roulier/codec.py
# class DecoderGetLabel(ABC):
#     def __init__(self, config_object):
#         """
#         items in parcels list should be a dict of this form
#         label = {
#             "id": 1,
#             "reference": "",
#             "tracking": {
#                 "number":"",
#                 "url": "",
#                 "partner": "",
#             },
#             "label": {
#                 "data": base64 label,
#                 "name": "",
#                 "type": "",
#             },
#         }
#         """
#         self.config = config_object
#         self.result = {
#             "parcels": [],
#             "annexes": [],
#         }
# 
#     @abstractmethod
#     def decode(self, response, payload):
#         """Transform a specific representation to python dict.
#         Args:
#             response : answer from the webservice
#             payload : data sent initially to the webservice
#         Need to increment the result attribute of the object, it does not need to return
#         anything
#         """
#         pass
# 
#     # helper to get the reference in case of mono_parcel
#     def _get_parcel_number(self, payload):
#         parcel_ref = ""
#         roulier_input = self.config.roulier_input or {}
#         parcels = roulier_input.get("parcels", [])
#         if len(parcels) == 1:
#             parcel_ref = parcels[0].get("reference", "")
#         return parcel_ref
# 
# Path: roulier/codec.py
# class DecoderGetPackingSlip(ABC):
#     def __init__(self, config_object):
#         """
#         packing_slip should be a dict of this form
#         {
#             "number": "string" or None,
#             "published_datetime": aware_datetime or None,
#             "number_of_parcels": int or None,
#             "client": {
#                 "number": "string" or None,
#                 "adress": "string" or None,
#                 "company": "string" or None,
#             }
#         }
#         """
#         self.config = config_object
#         self.result = {
#             "packing_slip": {},
#             "annexes": [],
#         }
# 
#     @abstractmethod
#     def decode(self, response, payload):
#         """Transform a specific representation to python dict.
#         Args:
#             response : answer from the webservice
#             payload : data sent initially to the webservice
#         Need to increment the result attribute of the object, it does not need to return
#         anything
#         """
#         pass


from datetime import datetime
from lxml import objectify
from ...codec import DecoderGetLabel
from ...codec import DecoderGetPackingSlip
import base64



class _UNSPECIFIED:
    pass


def _get_text(xml, tag, default=_UNSPECIFIED):
    """
    Returns the text content of a tag to avoid returning an lxml instance
    If no default is specified, it will raises the original exception of accessing
    to an inexistant tag
    """
    if not hasattr(xml, tag):
        if default is _UNSPECIFIED:
            # raise classic attr error
            return getattr(xml, tag)
        return default
    return getattr(xml, tag).text


def _get_cid(tag, tree):
    element = tree.find(tag)
    if element is None:
        return None
    href = element.getchildren()[0].attrib["href"]
    # href contains cid:236212...-38932@cfx.apache.org
    return href[len("cid:") :]  # remove prefix



# Path: roulier/codec.py
# class DecoderGetLabel(ABC):
#     def __init__(self, config_object):
#         """
#         items in parcels list should be a dict of this form
#         label = {
#             "id": 1,
#             "reference": "",
#             "tracking": {
#                 "number":"",
#                 "url": "",
#                 "partner": "",
#             },
#             "label": {
#                 "data": base64 label,
#                 "name": "",
#                 "type": "",
#             },
#         }
#         """
#         self.config = config_object
#         self.result = {
#             "parcels": [],
#             "annexes": [],
#         }
# 
#     @abstractmethod
#     def decode(self, response, payload):
#         """Transform a specific representation to python dict.
#         Args:
#             response : answer from the webservice
#             payload : data sent initially to the webservice
#         Need to increment the result attribute of the object, it does not need to return
#         anything
#         """
#         pass
# 
#     # helper to get the reference in case of mono_parcel
#     def _get_parcel_number(self, payload):
#         parcel_ref = ""
#         roulier_input = self.config.roulier_input or {}
#         parcels = roulier_input.get("parcels", [])
#         if len(parcels) == 1:
#             parcel_ref = parcels[0].get("reference", "")
#         return parcel_ref
# 
# Path: roulier/codec.py
# class DecoderGetPackingSlip(ABC):
#     def __init__(self, config_object):
#         """
#         packing_slip should be a dict of this form
#         {
#             "number": "string" or None,
#             "published_datetime": aware_datetime or None,
#             "number_of_parcels": int or None,
#             "client": {
#                 "number": "string" or None,
#                 "adress": "string" or None,
#                 "company": "string" or None,
#             }
#         }
#         """
#         self.config = config_object
#         self.result = {
#             "packing_slip": {},
#             "annexes": [],
#         }
# 
#     @abstractmethod
#     def decode(self, response, payload):
#         """Transform a specific representation to python dict.
#         Args:
#             response : answer from the webservice
#             payload : data sent initially to the webservice
#         Need to increment the result attribute of the object, it does not need to return
#         anything
#         """
#         pass


# Path: roulier/carriers/laposte_fr/decoder.py
from datetime import datetime
from lxml import objectify
from ...codec import DecoderGetLabel
from ...codec import DecoderGetPackingSlip
import base64




class _UNSPECIFIED:
    pass


def _get_text(xml, tag, default=_UNSPECIFIED):
    """
    Returns the text content of a tag to avoid returning an lxml instance
    If no default is specified, it will raises the original exception of accessing
    to an inexistant tag
    """
    if not hasattr(xml, tag):
        if default is _UNSPECIFIED:
            # raise classic attr error
            return getattr(xml, tag)
        return default
    return getattr(xml, tag).text


def _get_cid(tag, tree):
    element = tree.find(tag)
    if element is None:
        return None
    href = element.getchildren()[0].attrib["href"]
    # href contains cid:236212...-38932@cfx.apache.org
    return href[len("cid:") :]  # remove prefix



class LaposteFrDecoderGetLabel(DecoderGetLabel):