• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2024 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
18import os
19import re
20import logging
21from typing import Optional, Tuple, Dict, Any
22from abc import ABC, abstractmethod
23
24log = logging.getLogger('vmb')
25
26
27class LangBase(ABC):
28    """Base class for lang plugns."""
29
30    name = ''
31    short_name = ''
32    _re_import = re.compile(
33        r'^\s*(import\s+)?(?P<what>[\w{}\s]+)\s+from\s+'
34        r'([\'"])?(?P<lib>[./\w]+)([\'"])?\s*(;\s*)?$')
35
36    def __init__(self):
37        self.src = set()
38        self.ext = ''
39
40    @property
41    def re_import(self):
42        """Regexp for import module."""
43        return self._re_import
44
45    @property
46    @abstractmethod
47    def re_state(self) -> re.Pattern:
48        raise NotImplementedError
49
50    @property
51    @abstractmethod
52    def re_param(self) -> re.Pattern:
53        raise NotImplementedError
54
55    @property
56    @abstractmethod
57    def re_func(self) -> re.Pattern:
58        raise NotImplementedError
59
60    def get_import_line(self, lib: str, what: str) -> str:
61        libfile = os.path.split(lib)[1]
62        libname = os.path.splitext(libfile)[0]
63        return f'import {what} from "./{libname}";\n'
64
65    def get_method_call(self, name: str, typ: str) -> str:
66        if typ and typ != 'void':
67            return f'Consumer.consume(bench.{name}());'
68        return f'bench.{name}();'
69
70    def get_custom_fields(
71            # pylint: disable-next=unused-argument
72            self, values: Dict[str, Any], custom_values: Dict[str, Any]
73    ) -> Dict[str, str]:
74        """Help in adding custom fields in extra-plugins."""
75        return {}
76
77    def parse_state(self, line: str) -> str:
78        m = re.search(self.re_state, line)
79        if m:
80            return m.group("class")
81        return ''
82
83    def parse_param(self, line: str) -> Optional[Tuple[str, str]]:
84        m = re.search(self.re_param, line)
85        ret = None
86        if m:
87            # actually type is not needed
88            return m.group("param"), m.group("type")
89        return ret
90
91    def parse_func(self, line: str) -> Optional[Tuple[str, str]]:
92        m = re.search(self.re_func, line)  # type: ignore
93        ret = None
94        if m:
95            return m.group("func"), m.group("type")
96        return ret
97
98    def parse_import(self, line: str) -> Optional[Tuple[str, str]]:
99        m = re.search(self.re_import, line)  # type: ignore
100        ret = None
101        if m:
102            lib = m.group('lib')
103            what = m.group('what')
104            return lib, self.get_import_line(lib, what)
105        return ret
106