aboutsummaryrefslogtreecommitdiff
path: root/mmi2grpc/_helpers.py
blob: 4e34d5986162efaa9f45b63badb6b5e226a431d0 (plain)
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
# Copyright 2022 Google LLC
#
# 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
#
#     https://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.

"""Helper functions.

Facilitates the implementation of a new profile proxy or a PTS MMI.
"""

import functools
import textwrap
import unittest

DOCSTRING_WIDTH = 80 - 8  # 80 cols - 8 indentation spaces


def assert_description(f):
    """Decorator which verifies the description of a PTS MMI implementation.

    Asserts that the docstring of a function implementing a PTS MMI is the same
    as the corresponding official MMI description.

    Args:
        f: function implementing a PTS MMI.

    Raises:
        AssertionError: the docstring of the function does not match the MMI
            description.
    """
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        description = textwrap.fill(
            kwargs['description'], DOCSTRING_WIDTH, replace_whitespace=False)
        docstring = textwrap.dedent(f.__doc__ or '')

        if docstring.strip() != description.strip():
            print(f'Expected description of {f.__name__}:')
            print(description)

            # Generate AssertionError.
            test = unittest.TestCase()
            test.maxDiff = None
            test.assertMultiLineEqual(
                docstring.strip(),
                description.strip(),
                f'description does not match with function docstring of'
                f'{f.__name__}')

        return f(*args, **kwargs)
    return wrapper


def format_function(mmi_name, mmi_description):
    """Returns the base format of a function implementing a PTS MMI."""
    wrapped_description = textwrap.fill(
        mmi_description, DOCSTRING_WIDTH, replace_whitespace=False)
    return (
        f'@assert_description\n'
        f'def {mmi_name}(self, **kwargs):\n'
        f'    """\n'
        f'{textwrap.indent(wrapped_description, "    ")}\n'
        f'    """\n'
        f'\n'
        f'    return "OK"\n')


def format_proxy(profile, mmi_name, mmi_description):
    """Returns the base format of a profile proxy including a given MMI."""
    wrapped_function = textwrap.indent(
        format_function(mmi_name, mmi_description), '    ')
    return (
        f'from mmi2grpc._helpers import assert_description\n'
        f'from mmi2grpc._proxy import ProfileProxy\n'
        f'\n'
        f'from pandora.{profile.lower()}_grpc import {profile}\n'
        f'\n'
        f'\n'
        f'class {profile}Proxy(ProfileProxy):\n'
        f'\n'
        f'    def __init__(self, channel):\n'
        f'        super().__init__()\n'
        f'        self.{profile.lower()} = {profile}(channel)\n'
        f'\n'
        f'{wrapped_function}')