# SPDX-FileCopyrightText: Copyright © 2026 BBC
#
# SPDX-License-Identifier: BSD-3-Clause
from src.validationLogging.validationCodes import ValidationCode
from src.validationLogging.validationResult import ValidationResult, \
ERROR
from src.validationLogging.validationLogger import ValidationLogger
from xml.etree.ElementTree import Element
from src.xmlUtils import make_qname
from .xmlCheck import XmlCheck
from .ttmlUtils import ns_ttml
from src.registries.ttmRoleRegistry import role_registry_entries, \
role_user_defined_value_prefix
[docs]
class ttmlRoleTypeCheck(XmlCheck):
"""
Checks values of ttm:role attribute
"""
def __init__(self) -> None:
super().__init__()
[docs]
def run(
self,
input: Element,
context: dict,
validation_results: ValidationLogger) -> bool:
tt_ns: str = \
context.get('root_ns', ns_ttml)
ttm_ns: str = tt_ns + '#metadata'
role_attr_tag: str = make_qname(ttm_ns, 'role')
valid = True
role_els: list[Element[str]] = []
# The findall doesn't find the attribute on the input element
# so check it explicitly
if input.get(role_attr_tag):
role_els.append(input)
role_els.extend(input.findall(
'.//*[@{}]'.format(role_attr_tag)))
good_roles_count = 0
for role_el in role_els:
role_val = role_el.get(role_attr_tag)
if role_val \
and not role_val.startswith(
role_user_defined_value_prefix) \
and role_val not in role_registry_entries:
valid = False
validation_results.append(ValidationResult(
status=ERROR,
location='{} element'
.format(role_el.tag),
message='"{}" is not a permitted value for ttm:role'
.format(role_val),
code=ValidationCode.ttml_metadata_role
))
elif role_val:
good_roles_count += 1
if valid and good_roles_count > 0:
validation_results.good(
location='{} element and descendants'
.format(input.tag),
message='{} well-formed ttm:role attribute{} found'
.format(
good_roles_count,
's' if good_roles_count != 1 else ''),
code=ValidationCode.ttml_metadata_role
)
elif valid or good_roles_count > 0:
validation_results.info(
location='{} element and descendants'
.format(input.tag),
message='{} well-formed ttm:role attribute{} found'
.format(
good_roles_count,
's' if good_roles_count != 1 else ''),
code=ValidationCode.ttml_metadata_role
)
return valid