~kf5jwc/sms-printer

ref: 6a2e6e2b7591ba972574af4054db55dbf969bba0 sms-printer/sms_printer/sms/__init__.py -rw-r--r-- 548 bytes
6a2e6e2b — Kyle Jones Code linters are great. 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from jsonschema import validate
from jsonschema.exceptions import ValidationError
from flask import json

from .schemas import SCHEMAS
from .schemas.types import Parser_Base


class NoMatchingSchema(Exception):
    pass


def parse_sms(sms_input: str) -> Parser_Base:
    for schema, sms_parser in SCHEMAS.items():
        try:
            validate(sms_input, json.loads(schema))
            return sms_parser(sms_input)
        except ValidationError:
            continue

    raise NoMatchingSchema


__all__ = ["parse_sms", "NoMatchingSchema"]