gitlab: Extract default build/test jobs templates
[qemu/ar7.git] / scripts / qapi / source.py
blob04193cc964371aaba56cde9bb289b0b538c63e6a
2 # QAPI frontend source file info
4 # Copyright (c) 2019 Red Hat Inc.
6 # Authors:
7 # Markus Armbruster <armbru@redhat.com>
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING file in the top-level directory.
12 import copy
13 from typing import List, Optional, TypeVar
16 class QAPISchemaPragma:
17 # Replace with @dataclass in Python 3.7+
18 # pylint: disable=too-few-public-methods
20 def __init__(self) -> None:
21 # Are documentation comments required?
22 self.doc_required = False
23 # Commands whose names may use '_'
24 self.command_name_exceptions: List[str] = []
25 # Commands allowed to return a non-dictionary
26 self.command_returns_exceptions: List[str] = []
27 # Types whose member names may violate case conventions
28 self.member_name_exceptions: List[str] = []
31 class QAPISourceInfo:
32 T = TypeVar('T', bound='QAPISourceInfo')
34 def __init__(self, fname: str, parent: Optional['QAPISourceInfo']):
35 self.fname = fname
36 self.line = 1
37 self.parent = parent
38 self.pragma: QAPISchemaPragma = (
39 parent.pragma if parent else QAPISchemaPragma()
41 self.defn_meta: Optional[str] = None
42 self.defn_name: Optional[str] = None
44 def set_defn(self, meta: str, name: str) -> None:
45 self.defn_meta = meta
46 self.defn_name = name
48 def next_line(self: T) -> T:
49 info = copy.copy(self)
50 info.line += 1
51 return info
53 def loc(self) -> str:
54 return f"{self.fname}:{self.line}"
56 def in_defn(self) -> str:
57 if self.defn_name:
58 return "%s: In %s '%s':\n" % (self.fname,
59 self.defn_meta, self.defn_name)
60 return ''
62 def include_path(self) -> str:
63 ret = ''
64 parent = self.parent
65 while parent:
66 ret = 'In file included from %s:\n' % parent.loc() + ret
67 parent = parent.parent
68 return ret
70 def __str__(self) -> str:
71 return self.include_path() + self.in_defn() + self.loc()