• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2021-2023 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# This file does only contain a selection of the most common options. For a
18# full list see the documentation:
19# http://www.sphinx-doc.org/en/master/config
20
21import re
22from typing import Dict, Any
23
24
25class Descriptor:
26    def __init__(self, input_file: str) -> None:
27        self.input_file = input_file
28        self.header = re.compile(
29            r"/\*---(?P<header>.+)---\*/", re.DOTALL)
30        self.includes = re.compile(r"includes:\s+\[(?P<includes>.+)]")
31        self.includes2 = re.compile(r"includes:(?P<includes>(\s+-[^-].+)+)")
32        self.flags = re.compile(r"flags:\s+\[(?P<flags>.+)]")
33        self.negative = re.compile(
34            r"negative:.*phase:\s+(?P<phase>\w+).*type:\s+(?P<type>\w+)",
35            re.DOTALL)
36        self.spec = re.compile(r"spec:\s+(?P<spec>.+)")
37        self.issues = re.compile(r"issues:\s+(?P<issues>.+)")
38
39        self.content = self.get_content()
40
41    def get_content(self) -> str:
42        with open(self.input_file, "r", encoding="utf-8") as file_pointer:
43            input_str = file_pointer.read()
44        return input_str
45
46    def get_header(self) -> str:
47        header_comment = self.header.search(self.content)
48        return header_comment.group(0) if header_comment else ""
49
50    def parse_descriptor(self) -> Dict[str, Any]:
51        header = self.get_header()
52        result: Dict[str, Any] = {}
53
54        if len(header) == 0:
55            return result
56
57        includes = []
58        match = self.includes.search(header)
59        includes += [incl.strip() for incl in match.group("includes").split(",")] if match else []
60
61        match = self.includes2.search(header)
62        includes += [incl.strip() for incl in match.group("includes").split("-")][1:] if match else []
63
64        result["includes"] = includes
65
66        match = self.flags.search(header)
67        if match:
68            result["flags"] = [flag.strip() for flag in match.group("flags").split(",")]
69
70        match = self.negative.search(header)
71        if match:
72            result["negative_phase"] = match.group("phase")
73            result["negative_type"] = match.group("type")
74
75        match = self.spec.search(header)
76        if match:
77            result["spec"] = match.group("spec")
78
79        match = self.issues.search(header)
80        if match:
81            result["issues"] = [issue.strip() for issue in match.group("issues").split(",")]
82
83        return result
84
85    def get_descriptor(self) -> Dict[str, Any]:
86        return self.parse_descriptor()
87