File: finders.py

package info (click to toggle)
python-openapi-spec-validator 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 772 kB
  • sloc: python: 2,050; makefile: 54
file content (26 lines) | stat: -rw-r--r-- 843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from re import compile
from typing import List

from jsonschema_path.typing import Schema

from openapi_spec_validator.versions.datatypes import SpecVersion
from openapi_spec_validator.versions.exceptions import OpenAPIVersionNotFound


class SpecVersionFinder:
    pattern = compile(r"(?P<major>\d+)\.(?P<minor>\d+)(\..*)?")

    def __init__(self, versions: List[SpecVersion]) -> None:
        self.versions = versions

    def find(self, spec: Schema) -> SpecVersion:
        for v in self.versions:
            if v.keyword in spec:
                version_str = spec[v.keyword]
                m = self.pattern.match(version_str)
                if m:
                    version = SpecVersion(**m.groupdict(), keyword=v.keyword)
                    if v == version:
                        return v

        raise OpenAPIVersionNotFound