<commit_before># -*- coding: utf-8 -*-
"""Transform input to geodis compatible xml."""
from jinja2 import Environment, PackageLoader
from roulier.codec import Encoder
from roulier.exception import InvalidApiInput
from .geodis_api_ws import GeodisApiWs

GEODIS_ACTIONS = ('demandeImpressionEtiquette',)


class GeodisEncoderWs(Encoder):
    """Transform input to geodis compatible xml."""

    def encode(self, api_input, action):
        """Transform input to geodis compatible xml."""
        if not (action in GEODIS_ACTIONS):
            raise InvalidApiInput(
                'action %s not in %s' % (action, ', '.join(GEODIS_ACTIONS)))

        api = GeodisApiWs()
        if not api.validate(api_input):
            raise InvalidApiInput(
                'Input error : %s' % api.errors(api_input))
        data = api.normalize(api_input)

        data['service']['labelFormat'] = self.lookup_label_format(
            data['service']['labelFormat'])
        data['service']['shippingDate'] = data['service']['shippingDate'].replace('/','') 

        is_test = data['service']['is_test']

        env = Environment(
            loader=PackageLoader('roulier', '/carriers/geodis/templates'),
            extensions=['jinja2.ext.with_', 'jinja2.ext.autoescape'],
            autoescape=True)

        template = env.get_template("geodis_%s.xml" % action)
        return {
            "body": template.render(
                service=data['service'],
                parcel=data['parcel'],
                sender_address=data['from_address'],
                receiver_address=data['to_address']),
            "headers": data['auth'],
            "is_test": is_test,
            "output_format": data['service']['labelFormat']
        }

    def api(self):
        api = GeodisApiWs()
        return api.api_values()

    def lookup_label_format(self, label_format="ZPL"):
        """Get a Geodis compatible format of label.
<commit_msg>return requested label format<commit_after># -*- coding: utf-8 -*-
"""Transform input to geodis compatible xml."""
from jinja2 import Environment, PackageLoader
from roulier.codec import Encoder
from roulier.exception import InvalidApiInput
from .geodis_api_ws import GeodisApiWs

GEODIS_ACTIONS = ('demandeImpressionEtiquette',)


class GeodisEncoderWs(Encoder):
    """Transform input to geodis compatible xml."""

    def encode(self, api_input, action):
        """Transform input to geodis compatible xml."""
        if not (action in GEODIS_ACTIONS):
            raise InvalidApiInput(
                'action %s not in %s' % (action, ', '.join(GEODIS_ACTIONS)))

        api = GeodisApiWs()
        if not api.validate(api_input):
            raise InvalidApiInput(
                'Input error : %s' % api.errors(api_input))
        data = api.normalize(api_input)

        data['service']['labelFormat'] = self.lookup_label_format(
            data['service']['labelFormat'])
        data['service']['shippingDate'] = data['service']['shippingDate'].replace('/','') 

        is_test = data['service']['is_test']

        env = Environment(
            loader=PackageLoader('roulier', '/carriers/geodis/templates'),
            extensions=['jinja2.ext.with_', 'jinja2.ext.autoescape'],
            autoescape=True)

        template = env.get_template("geodis_%s.xml" % action)
        return {
            "body": template.render(
                service=data['service'],
                parcel=data['parcel'],
                sender_address=data['from_address'],
                receiver_address=data['to_address']),
            "headers": data['auth'],
            "is_test": is_test,
            "output_format": api_input['service']['labelFormat']
        }

    def api(self):
        api = GeodisApiWs()
        return api.api_values()

    def lookup_label_format(self, label_format="ZPL"):
        """Get a Geodis compatible format of label.

{'fraction_non_alphanumeric': 0.13285457809694792, 'fraction_numerical': 0.0020518081559374197, 'mean_word_length': 2.658536585365854, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205561', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1097, 'n_words': 241}