# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
P4Runtime global options.
"""
import enum
try:
from .p4_exception import UnknownOptionName, InvalidOptionValueType
except ImportError:
from p4_exception import UnknownOptionName, InvalidOptionValueType
@enum.unique
class Options(enum.Enum):
"""
P4 options.
"""
canonical_bytestrings = bool
class GlobalOptions:
"""
P4 global options.
"""
option_defaults = {
Options.canonical_bytestrings: True,
}
option_helpstrings = {
Options.canonical_bytestrings: """
Use byte-padded legacy format for binary strings sent to the P4Runtime server,
instead of the canonical representation. See P4Runtime specification for details.
"""
}
def __init__(self):
self._values = {}
self.reset()
self._option_names = [option.name for option in Options]
self._set_docstring()
def reset(self):
"""
Reset all options to their defaults.
:return: void
"""
for option in Options:
assert option in GlobalOptions.option_defaults
self._values[option] = GlobalOptions.option_defaults[option]
def _supported_options_as_str(self):
"""
Return a comma-separated string of supported options.
:return: string of supported options
"""
return ", ".join([f"{o.name} ({o.value.__name__})" for o in Options])
def _supported_options_as_str_verbose(self):
"""
Return a detailed comma-separated string of supported options.
:return: string of supported options
"""
opt_str = ""
for option in Options:
opt_str += f"Option name: {option.name}\n"
opt_str += f"Type: {option.value.__name__}\n"
opt_str += f"Default value: " \
f"{GlobalOptions.option_defaults[option]}\n"
opt_str += f"Description: " \
f"{GlobalOptions.option_helpstrings.get(option, 'N/A')}\n"
opt_str += "\n"
return opt_str[:-1]
def _set_docstring(self):
"""
Set the documentation for this object.
:return: void
"""
self.__doc__ = f"""
Manage global options for the P4Runtime shell.
Supported options are: {self._supported_options_as_str()}
To set the value of a global option, use GLOBAL_OPTIONS["