1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import dataclasses
import enum
import os.path
from typing import Any, Dict, Iterable, List, Mapping, Optional, Tuple
import xml.etree.ElementTree
# Short aliases for typing
Element = xml.etree.ElementTree.Element
@dataclasses.dataclass(frozen=True)
class Description:
"""Holds the data from a Wayland protocol <description> subtree."""
# The value of the name summary of the <description>.
summary: str
# The text content of the <description>.
description: str
@staticmethod
def parse_xml(e: Element) -> Optional['Description']:
return Description(summary=e.get('summary'),
description=e.text.strip()
if e.text else '') if e is not None else None
class MessageArgType(enum.Enum):
"""The valid types of an <arg>."""
INT = 'int'
UINT = 'uint'
FIXED = 'fixed'
STRING = 'string'
FD = 'fd' # File descriptor
ARRAY = 'array' # untyped Array
NEW_ID = 'new_id' # Special type for constructing
OBJECT = 'object' # An interface
@dataclasses.dataclass(frozen=True)
class MessageArg:
"""Holds the data from a Wayland protocol <arg> subtree."""
# The containing Message, for context.
message: 'Message' = dataclasses.field(repr=False, hash=False)
# The value of the name attribute of the <arg>.
name: str
# The value of the type attribute of the <arg>.
type: MessageArgType
# The value of the optional summary attribute of the <arg>.
summary: Optional[str]
# The value of the optional interface attribute of the <arg>.
interface: Optional[str]
# The value of the optional nullable attribute of the <arg>.
nullable: Optional[bool]
# The value of the optional enum attribute of the <arg>.
enum: Optional[str]
# The optional <description> child element of the <arg>.
description: Optional[Description]
@staticmethod
def parse_xml(message: 'Message', e: Element) -> 'MessageArg':
return MessageArg(message=message,
name=e.get('name'),
type=e.get('type'),
summary=e.get('summary'),
interface=e.get('interface'),
nullable=e.get('allow-null') == 'true'
if e.get('allow-null') else None,
enum=e.get('enum'),
description=Description.parse_xml(
e.find('description')))
class RequestType(enum.Enum):
"""The valid types of a <request> message."""
DESTRUCTOR = 'destructor'
@dataclasses.dataclass(frozen=True)
class Message:
"""Holds the data from a Wayland protocol <request> OR <event> subtree."""
# The containing Interface, for context.
interface: 'Interface' = dataclasses.field(repr=False, hash=False)
# If true, this message is an <event> in the containing interface.
# Otherwise it is a <request>.
is_event: bool
# The value of the name attribute of the <request> or <event>.
name: str
# The value of the optional type attribute of the <request> Always empty
# for <events>.
request_type: Optional[RequestType]
# The value of the optional since attribute of the <request> or <event>.
since: Optional[int]
# The optional <description> child element of the <request> or <event>.
description: Optional[Description]
# The child <arg> elements of the <request> or <event>
args: Tuple[MessageArg, ...] = dataclasses.field(init=False)
@staticmethod
def parse_xml(interface: 'Interface', is_event: bool,
e: Element) -> 'Message':
message = Message(
interface=interface,
is_event=is_event,
name=e.get('name'),
request_type=e.get('type'),
since=int(e.get('since')) if e.get('since') else None,
description=Description.parse_xml(e.find('description')))
# Note: This is needed to finish up since the instance is frozen.
object.__setattr__(
message, 'args',
tuple(MessageArg.parse_xml(message, c) for c in e.findall('arg')))
return message
@dataclasses.dataclass(frozen=True)
class EnumEntry:
"""Holds the data from a Wayland protocol <entry> subtree."""
# The containing Enum, for context.
enum: 'Enum' = dataclasses.field(repr=False, hash=False)
# The value of the name attribute of the <entry>.
name: str
# The value of the value attribute of the <entry>.
value: int
# The value of the optional summary attribute of the <entry>.
summary: Optional[str]
# The value of the optional since attribute of the <entry>.
since: Optional[int]
# The optional <description> child element of the <request> or <event>.
description: Optional[Description]
@staticmethod
def parse_xml(enum: 'Enum', e: Element) -> 'EnumEntry':
return EnumEntry(
enum=enum,
name=e.get('name'),
value=int(e.get('value'), 0),
summary=e.get('summary'),
since=int(e.get('since'), 0) if e.get('since') else None,
description=Description.parse_xml(e.find('description')))
@dataclasses.dataclass(frozen=True)
class Enum:
"""Holds the data from a Wayland protocol <enum> subtree."""
# The containing Interface, for context.
interface: 'Interface' = dataclasses.field(repr=False, hash=False)
# The value of the name attribute of the <enum>.
name: str
# The value of the optional since attribute of the <enum>.
since: Optional[int]
# The value of the optional bitfield attribute of the <enum>.
bitfield: Optional[bool]
# The optional <description> child element of the <request> or <event>.
description: Optional[Description]
# The child <entry> elements for the <enum>.
entries: Tuple[EnumEntry, ...] = dataclasses.field(init=False)
@staticmethod
def parse_xml(interface: 'Interface', e: Element) -> 'Enum':
enum = Enum(interface=interface,
name=e.get('name'),
since=int(e.get('since'), 0) if e.get('since') else None,
bitfield=e.get('bitfield') == 'true'
if e.get('bitfield') else None,
description=Description.parse_xml(e.find('description')))
# Note: This is needed to finish up since the instance is frozen.
object.__setattr__(
enum, 'entries',
tuple(EnumEntry.parse_xml(enum, c) for c in e.findall('entry')))
return enum
@dataclasses.dataclass(frozen=True)
class Interface:
"""Holds the data from a Wayland protocol <interface> subtree."""
# The containing Protocol, for context.
protocol: 'Protocol' = dataclasses.field(repr=False, hash=False)
# The value of the name attribute of the <interface>.
name: str
# The value of the version attribute of the <interface>.
version: int
# The optional <description> child element of the <interface>.
description: Optional[Description]
# The child <request> elements for the <interface>.
requests: Tuple[Message, ...] = dataclasses.field(init=False)
# The child <event> elements for the <interface>.
events: Tuple[Message, ...] = dataclasses.field(init=False)
# The child <enum> elements for the <interface>.
enums: Tuple[Enum, ...] = dataclasses.field(init=False)
@staticmethod
def parse_xml(protocol: 'Protocol', e: Element) -> 'Interface':
interface = Interface(protocol=protocol,
name=e.get('name'),
version=int(e.get('version'), 0),
description=Description.parse_xml(
e.find('description')))
# Note: This is needed to finish up since the instance is frozen.
object.__setattr__(
interface, 'requests',
tuple(
Message.parse_xml(interface, False, c)
for c in e.findall('request')))
object.__setattr__(
interface, 'events',
tuple(
Message.parse_xml(interface, True, c)
for c in e.findall('event')))
object.__setattr__(
interface, 'enums',
tuple(Enum.parse_xml(interface, c) for c in e.findall('enum')))
return interface
@dataclasses.dataclass(frozen=True)
class Copyright:
"""Holds the data from a Wayland protocol <copyright> subtree."""
# The text content of the <copyright>.
text: str
@staticmethod
def parse_xml(e: Element) -> Optional['Copyright']:
return Copyright(text=e.text.strip()) if e is not None else None
@dataclasses.dataclass(frozen=True)
class Protocol:
"""Holds the data from a Wayland protocol <protocol> subtree."""
# The universe of known protocols this is part of.
protocols: 'Protocols' = dataclasses.field(repr=False, hash=False)
# The containing base filename (no path, no extension), for context.
filename: str
# The value of the name attribute of the <protocol>.
name: str
# The optional <copyright> child element of the <protocol>.
copyright: Optional[Copyright]
# The optional <description> child element of the <protocol>.
description: Optional[Description]
# The child <interface> elements for the <protocol>.
interfaces: Tuple[Interface, ...] = dataclasses.field(init=False)
@staticmethod
def parse_xml(protocols: 'Protocols', filename: str,
e: Element) -> 'Protocol':
protocol = Protocol(protocols,
filename=filename,
name=e.get('name'),
copyright=Copyright.parse_xml(e.find('copyright')),
description=Description.parse_xml(
e.find('description')))
# Note: This is needed to finish up since the instance is frozen.
object.__setattr__(
protocol, 'interfaces',
tuple(
Interface.parse_xml(protocol, i)
for i in e.findall('interface')))
return protocol
@dataclasses.dataclass(frozen=True)
class Protocols:
"""Holds the data from multiple Wayland protocol files."""
# The parsed protocol dataclasses
protocols: Tuple[Protocol, ...] = dataclasses.field(init=False)
@staticmethod
def parse_xml_files(filenames: Iterable[str]) -> 'Protocols':
protocols = Protocols()
# Note: This is needed to finish up since the instance is frozen.
object.__setattr__(
protocols, 'protocols',
tuple(
Protocol.parse_xml(
protocols,
os.path.splitext(os.path.basename(filename))[0],
xml.etree.ElementTree.parse(filename).getroot())
for filename in filenames))
return protocols
|