~kf5jwc/sms-printer

ref: 664f82c6b8dc4533e87cdda5bd27caaf3e7ad36a sms-printer/sms_printer/sms/__init__.py -rw-r--r-- 545 bytes View raw
664f82c6 — Kyle Jones Update to use Python 3.6 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']