File: enum.py

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,784 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (129 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from common import *

_enum_to_str_list = [
    # xproto.xml
    ('xcb', 'VisualClass'),
    ('xcb', 'EventMask'),
    ('xcb', 'BackingStore'),
    ('xcb', 'ImageOrder'),
    ('xcb', 'ModMask'),
    ('xcb', 'ButtonMask'),
    ('xcb', 'KeyButMask'),
    ('xcb', 'Motion'),
    ('xcb', 'NotifyDetail'),
    ('xcb', 'NotifyMode'),
    ('xcb', 'Visibility'),
    ('xcb', 'Place'),
    ('xcb', 'ColormapState'),
    ('xcb', 'Mapping'),
    ('xcb', 'WindowClass'),
    ('xcb', 'Gravity'),
    ('xcb', 'MapState'),
    ('xcb', 'ConfigWindow'),
    ('xcb', 'GrabStatus'),
    ('xcb', 'FontDraw'),
    ('xcb', 'StackMode'),
    ('xcb', 'Blanking'),
    ('xcb', 'Exposures'),
    ('xcb', 'MappingStatus'),

    # TODO

    # screensaver.xml
    ('xcb', 'ScreenSaver', 'Kind'),
    ('xcb', 'ScreenSaver', 'State'),
]

def get_enum_prefix(name):
    pkg_name, name = split_pkg_and_type(name)
    # check result
    enum_prefix = ''.join(name)
    assert enum_prefix[0].isupper()

    _ns = get_namespace()
    if _ns.is_ext and pkg_name != '' and pkg_name != get_go_pkg_name(_ns.ext_name):
        # add pkg ident
        enum_prefix = pkg_name + '.' + enum_prefix

    return enum_prefix


_cname_special_cases = {
    'VISUALID': 'VisualID',        
}

_item_name_special_list = [
        # xproto.xml
        'YX',
        'XY',
        'DE',
        'RGB',
        'LSB',
        'MSB',
        'WM',

        # render.xml
        'BGR',
        'HSL',
]

def get_enum_item_name(name):
    if name in _cname_special_cases:
        return _cname_special_cases[name]

    name_parts = split_name(name)
    result = []
    for x in name_parts:
        if x in _item_name_special_list:
            result.append(x)
        else:
            result.append(x.title())
    return ''.join(result)


def define_go_enum_to_string_func(prefix, self):
    if len(self.values) < 2:
        return
    if not self.name in _enum_to_str_list:
        return

    l('\nfunc %sEnumToStr(v int) string {', prefix)

    if len(self.bits) > 0:
        l('strv := []string{}')
        l('switch {')
        for (item_name, val) in self.values:
            val = int(val)
            if val == 0:
                continue
            l('case v & %s%s > 0:', prefix, item_name)
            l('    strv = append(strv, "%s")', item_name)
        l('}') # end switch
        l('return "[" + strings.Join(strv, "|") + "]"')

    else:
        items = dict()
        # key is val, value is list of item_name
        for (item_name, val) in self.values:
            val = int(val)
            if val in items:
                items[val].append(item_name)
                # items[val] = items[val].append(item_name)
            else:
                items[val] = [ item_name ]

        l('switch v {')
        for k in sorted(items.keys()):
            l('case %d:', k)
            l('    return "%s"', ' or '.join(items[k]))

        # case default
        l('default:')
        l('return "<Unknown>"')
        l('}') # end switch

    l('}') # end func