from company.choices import fr as choices
from mighty.errors import BackendError
import datetime, logging
logger = logging.getLogger(__name__)

CHOICES_APE = dict(choices.APE)
CHOICES_LEGALFORM = dict(choices.LEGALFORM)
CHOICES_SLICE = dict(choices.SLICE_EFFECTIVE)

class SearchBackend:
    message = None
    since_format = None
    iso_format = '%Y-%m-%dT%H:%M:%S.%f%z'

    def in_error(self, message):
        self.message = message

    def backend_error(self, msg):
        raise BackendError(msg)

    def companies(self, companies, response_code):
        if str(response_code)[0] == '4': self.in_error(companies[0]['message'])
        elif str(response_code)[0] == '5': self.in_error('error server')
        return companies

    def get_ape_str(self, code):
        try:
            return CHOICES_APE[code]
        except Exception:
            pass
        return code

    def get_legalform_str(self, code):
        try:
            code = int(code)
            return CHOICES_LEGALFORM[code]
        except Exception:
            pass
        return code

    def get_slice_str(self, code):
        try:
            return CHOICES_SLICE[code]
        except Exception:
            pass
        return code

    def lastupdate(self, date):
        return datetime.datetime.strptime(date, self.iso_format).strftime("%Y-%m-%d")

    def since(self, date):
        logger.warning(date)
        return datetime.datetime.strptime(date, self.since_format).strftime("%Y-%m-%d")

    def get_companies(self, companies, response_code):
        raise NotImplementedError("Subclasses should implement get_companies()")

    def get_company_by_siren(self, siren):
        raise NotImplementedError("Subclasses should implement get_company_by_siren()")

    def get_company_by_fulltext(self, fulltext):
        raise NotImplementedError("Subclasses should implement get_company_by_fulltext()")

    def get_active_companies(self, fulltext):
        raise NotImplementedError("Subclasses should implement get_active_companies()")
[((4, 9, 4, 36), 'logging.getLogger', 'logging.getLogger', ({(4, 27, 4, 35): '__name__'}, {}), '(__name__)', False, 'import datetime, logging\n'), ((19, 14, 19, 31), 'mighty.errors.BackendError', 'BackendError', ({(19, 27, 19, 30): 'msg'}, {}), '(msg)', False, 'from mighty.errors import BackendError\n'), ((49, 15, 49, 64), 'datetime.datetime.strptime', 'datetime.datetime.strptime', ({(49, 42, 49, 46): 'date', (49, 48, 49, 63): 'self.iso_format'}, {}), '(date, self.iso_format)', False, 'import datetime, logging\n'), ((53, 15, 53, 66), 'datetime.datetime.strptime', 'datetime.datetime.strptime', ({(53, 42, 53, 46): 'date', (53, 48, 53, 65): 'self.since_format'}, {}), '(date, self.since_format)', False, 'import datetime, logging\n')]