This commit is contained in:
Iliyan Angelov
2025-12-01 06:50:10 +02:00
parent 91f51bc6fe
commit 62c1fe5951
4682 changed files with 544807 additions and 31208 deletions

View File

@@ -0,0 +1,33 @@
# Copyright (C) 2020 Red Hat, Inc.
#
# 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.
import abc
from collections.abc import Iterable
from typing import Any
class FormatterBase(metaclass=abc.ABCMeta):
"""Base class for example plugin used in the tutorial."""
def __init__(self, max_width: int = 60) -> None:
self.max_width = max_width
@abc.abstractmethod
def format(self, data: dict[str, Any]) -> Iterable[str]:
"""Format the data and return unicode text.
:param data: A dictionary with string keys and simple types as values.
:returns: Iterable producing the formatted text.
"""

View File

@@ -0,0 +1,44 @@
# Copyright (C) 2020 Red Hat, Inc.
#
# 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.
import argparse
from stevedore import driver
from .base import FormatterBase
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'format', nargs='?', default='simple', help='the output format'
)
parser.add_argument(
'--width', default=60, type=int, help='maximum output width for text'
)
parsed_args = parser.parse_args()
data = {'a': 'A', 'b': 'B', 'long': 'word ' * 80}
mgr: driver.DriverManager[FormatterBase] = driver.DriverManager(
namespace='stevedore.example.formatter',
name=parsed_args.format,
invoke_on_load=True,
invoke_args=(parsed_args.width,),
)
# okay because invoke_on_load is true
assert isinstance(mgr.driver, FormatterBase)
for chunk in mgr.driver.format(data):
print(chunk, end='')

View File

@@ -0,0 +1,53 @@
# Copyright (C) 2020 Red Hat, Inc.
#
# 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.
import argparse
from collections.abc import Iterable
from typing import Any
from stevedore import extension
from .base import FormatterBase
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--width', default=60, type=int, help='maximum output width for text'
)
parsed_args = parser.parse_args()
data = {'a': 'A', 'b': 'B', 'long': 'word ' * 80}
mgr: extension.ExtensionManager[FormatterBase]
mgr = extension.ExtensionManager(
namespace='stevedore.example.formatter',
invoke_on_load=True,
invoke_args=(parsed_args.width,),
)
def format_data(
ext: extension.Extension[FormatterBase], data: dict[str, Any], /
) -> tuple[str, Iterable[str]]:
assert ext.obj is not None
return (ext.name, ext.obj.format(data))
results = mgr.map(format_data, data)
for name, result in results:
print(f'Formatter: {name}')
for chunk in result:
print(chunk, end='')
print('')

View File

@@ -0,0 +1,40 @@
[build-system]
requires = ["setuptools>=61.0.0"]
build-backend = "setuptools.build_meta"
[project]
name = "stevedore-examples"
description = "Demonstration package for stevedore"
authors = [
{name = "Doug Hellmann", email = "doug@doughellmann.com"},
]
readme = {file = "README.rst", content-type = "text/x-rst"}
license = {text = "Apache-2.0"}
version = "0.1"
requires-python = ">=3.10"
classifiers = [
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
[project.urls]
Homepage = "https://docs.openstack.org/stevedore"
Repository = "https://opendev.org/openstack/stevedore"
[project.entry-points."stevedore.example.formatter"]
simple = "stevedore.example.simple:Simple"
plain = "stevedore.example.simple:Simple"
[tool.setuptools]
packages = [
"stevedore.examples"
]

View File

@@ -0,0 +1,18 @@
# Copyright (C) 2020 Red Hat, Inc.
#
# 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.
from setuptools import setup
setup()

View File

@@ -0,0 +1,32 @@
# Copyright (C) 2020 Red Hat, Inc.
#
# 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.
from collections.abc import Iterable
from typing import Any
from stevedore.example import base
class Simple(base.FormatterBase):
"""A very basic formatter."""
def format(self, data: dict[str, Any]) -> Iterable[str]:
"""Format the data and return unicode text.
:param data: A dictionary with string keys and simple types as values.
:returns: Iterable producing the formatted text.
"""
for name, value in sorted(data.items()):
line = f'{name} = {value}\n'
yield line