blob: 04193cc964371aaba56cde9bb289b0b538c63e6a [file] [log] [blame]
Markus Armbrustere6c42b92019-10-18 09:43:44 +02001#
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
12import copy
John Snowf5d43612020-10-09 12:15:46 -040013from typing import List, Optional, TypeVar
Markus Armbrustere6c42b92019-10-18 09:43:44 +020014
15
Markus Armbrusterbaa310f2020-03-04 16:59:29 +010016class QAPISchemaPragma:
John Snow96670e82020-10-09 12:15:47 -040017 # Replace with @dataclass in Python 3.7+
18 # pylint: disable=too-few-public-methods
19
John Snowf5d43612020-10-09 12:15:46 -040020 def __init__(self) -> None:
Markus Armbrustere6c42b92019-10-18 09:43:44 +020021 # Are documentation comments required?
22 self.doc_required = False
Markus Armbruster05ebf842021-03-23 10:40:21 +010023 # Commands whose names may use '_'
24 self.command_name_exceptions: List[str] = []
Markus Armbrusterb86df372021-03-23 10:40:16 +010025 # 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] = []
Markus Armbrustere6c42b92019-10-18 09:43:44 +020029
30
Markus Armbrusterbaa310f2020-03-04 16:59:29 +010031class QAPISourceInfo:
John Snowf5d43612020-10-09 12:15:46 -040032 T = TypeVar('T', bound='QAPISourceInfo')
33
John Snowb2b31fd2021-05-19 14:39:39 -040034 def __init__(self, fname: str, parent: Optional['QAPISourceInfo']):
Markus Armbrustere6c42b92019-10-18 09:43:44 +020035 self.fname = fname
John Snowb2b31fd2021-05-19 14:39:39 -040036 self.line = 1
Markus Armbrustere6c42b92019-10-18 09:43:44 +020037 self.parent = parent
John Snowf5d43612020-10-09 12:15:46 -040038 self.pragma: QAPISchemaPragma = (
39 parent.pragma if parent else QAPISchemaPragma()
40 )
41 self.defn_meta: Optional[str] = None
42 self.defn_name: Optional[str] = None
Markus Armbrustere6c42b92019-10-18 09:43:44 +020043
John Snowf5d43612020-10-09 12:15:46 -040044 def set_defn(self, meta: str, name: str) -> None:
Markus Armbrustere6c42b92019-10-18 09:43:44 +020045 self.defn_meta = meta
46 self.defn_name = name
47
John Snowf5d43612020-10-09 12:15:46 -040048 def next_line(self: T) -> T:
Markus Armbrustere6c42b92019-10-18 09:43:44 +020049 info = copy.copy(self)
50 info.line += 1
51 return info
52
John Snowf5d43612020-10-09 12:15:46 -040053 def loc(self) -> str:
John Snowb2b31fd2021-05-19 14:39:39 -040054 return f"{self.fname}:{self.line}"
Markus Armbrustere6c42b92019-10-18 09:43:44 +020055
John Snowf5d43612020-10-09 12:15:46 -040056 def in_defn(self) -> str:
Markus Armbrustere6c42b92019-10-18 09:43:44 +020057 if self.defn_name:
58 return "%s: In %s '%s':\n" % (self.fname,
59 self.defn_meta, self.defn_name)
60 return ''
61
John Snowf5d43612020-10-09 12:15:46 -040062 def include_path(self) -> str:
Markus Armbrustere6c42b92019-10-18 09:43:44 +020063 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
69
John Snowf5d43612020-10-09 12:15:46 -040070 def __str__(self) -> str:
Markus Armbrustere6c42b92019-10-18 09:43:44 +020071 return self.include_path() + self.in_defn() + self.loc()