~kf5jwc/sms-printer

ref: 4a1d852f9a38f157710d4950364292dee33c144f sms-printer/sms_printer/sms/__init__.py -rw-r--r-- 545 bytes
4a1d852f — Kyle Jones Refactor for environment configuration 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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']