aboutsummaryrefslogtreecommitdiff
path: root/mmi2grpc/_description.py
blob: 0a3c5748ab5fbaf7e10b8bd6662593274130026b (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
import functools
import unittest
import textwrap

COMMENT_WIDTH = 80 - 8  # 80 cols - 8 indentation space


def assert_description(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        description = textwrap.fill(
            kwargs["description"], COMMENT_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.__name__}')

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


def format_function(id, description):
    wrapped = textwrap.fill(
        description, COMMENT_WIDTH, replace_whitespace=False)
    return (
        f'@assert_description\n'
        f'def {id}(self, **kwargs):\n'
        f'    """\n'
        f'{textwrap.indent(wrapped, "    ")}\n'
        f'    """\n'
        f'\n'
        f'    return "OK"\n'
    )


def format_proxy(profile, id, description):
    return (
        f'from ._description import assert_description\n'
        f'from ._proxy import ProfileProxy\n'
        f'\n'
        f'from blueberry.{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'{textwrap.indent(format_function(id, description), "    ")}'
    )