]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi/source.py
qapi/source.py: add type hint annotations
[mirror_qemu.git] / scripts / qapi / source.py
1 #
2 # QAPI frontend source file info
3 #
4 # Copyright (c) 2019 Red Hat Inc.
5 #
6 # Authors:
7 # Markus Armbruster <armbru@redhat.com>
8 #
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING file in the top-level directory.
11
12 import copy
13 import sys
14 from typing import List, Optional, TypeVar
15
16
17 class QAPISchemaPragma:
18 def __init__(self) -> None:
19 # Are documentation comments required?
20 self.doc_required = False
21 # Whitelist of commands allowed to return a non-dictionary
22 self.returns_whitelist: List[str] = []
23 # Whitelist of entities allowed to violate case conventions
24 self.name_case_whitelist: List[str] = []
25
26
27 class QAPISourceInfo:
28 T = TypeVar('T', bound='QAPISourceInfo')
29
30 def __init__(self, fname: str, line: int,
31 parent: Optional['QAPISourceInfo']):
32 self.fname = fname
33 self.line = line
34 self.parent = parent
35 self.pragma: QAPISchemaPragma = (
36 parent.pragma if parent else QAPISchemaPragma()
37 )
38 self.defn_meta: Optional[str] = None
39 self.defn_name: Optional[str] = None
40
41 def set_defn(self, meta: str, name: str) -> None:
42 self.defn_meta = meta
43 self.defn_name = name
44
45 def next_line(self: T) -> T:
46 info = copy.copy(self)
47 info.line += 1
48 return info
49
50 def loc(self) -> str:
51 if self.fname is None:
52 return sys.argv[0]
53 ret = self.fname
54 if self.line is not None:
55 ret += ':%d' % self.line
56 return ret
57
58 def in_defn(self) -> str:
59 if self.defn_name:
60 return "%s: In %s '%s':\n" % (self.fname,
61 self.defn_meta, self.defn_name)
62 return ''
63
64 def include_path(self) -> str:
65 ret = ''
66 parent = self.parent
67 while parent:
68 ret = 'In file included from %s:\n' % parent.loc() + ret
69 parent = parent.parent
70 return ret
71
72 def __str__(self) -> str:
73 return self.include_path() + self.in_defn() + self.loc()