File: osken.py

package info (click to toggle)
python-os-ken 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,280 kB
  • sloc: python: 100,620; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (180 lines) | stat: -rw-r--r-- 5,459 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright (C) 2013,2014 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2013,2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
#
# 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.

# this is a pyang plugin to generate os_ken/lib/of_config/generated_classes.py
# usage example:
# PYTHONPATH=. ./bin/pyang --plugindir ~/git/os_ken/tools/pyang_plugins -f os_ken ~/git/os_ken/tools/of-config1.1.1.yang > ~/git/os_ken/lib/of_config/generated_classes.py


_COPYRIGHT_NOTICE = """
# Copyright (C) 2013,2014 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2013,2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
#
# 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 sys
import StringIO
import pyang
from pyang import plugin


def pyang_plugin_init():
    plugin.register_plugin(OSKenPlugin())


class OSKenPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        fmts['os_ken'] = self

    def emit(self, ctx, modules, fd):
        emit_os_ken(ctx, modules[0], fd)


def emit_os_ken(ctx, module, fd):
    ctx.i_os_ken_queue = []
    visit_children(ctx, module, fd, module.i_children)
    ctx.i_os_ken_queue.reverse()
    generate_header(ctx)
    done = set()
    for s in ctx.i_os_ken_queue:
        name = generate_type_name(s)
        if name in done:
            continue
        generate_class(s)
        done.add(name)


def visit_children(ctx, module, fd, children, prefix=''):
    for c in children:
        if not is_leaf(c):
            ctx.i_os_ken_queue.append(c)
        if hasattr(c, 'i_children'):
            visit_children(ctx, module, fd, c.i_children, prefix + '  ')


def is_leaf(s):
    return s.keyword in ['leaf', 'leaf-list']


def generate_header(ctx):
    print(_COPYRIGHT_NOTICE)
    print('# do not edit.')
    print('# this file was mechanically generated with:')
    print('#    pyang %s' % pyang.__version__)
    print('#    os_ken.tools.pyang_plugins.osken')
    for mod, ver in sorted(ctx.modules):
        print('#    %s@%s' % (mod, ver))
    print('')
    print('from os_ken.lib.of_config.base import _Base, _e, _ct')


def generate_class_def(s):
    try:
        return s.i_os_ken_class_def
    except AttributeError:
        pass
    s.i_os_ken_class_def = _generate_class_def(s)
    return s.i_os_ken_class_def


def _generate_class_def(s):
    if is_leaf(s):
        return generate_type_name(s)
    o = StringIO.StringIO()
    print >> o, 'class %s(_Base):' % (generate_type_name(s),)
    print >> o, '    _ELEMENTS = ['
    for c in s.i_children:
        if is_leaf(c):
            print >> o, '        _e(\'%s\', is_list=%s),  # %s' % \
                (c.arg, c.keyword == 'leaf-list', generate_type_name(c),)
        else:
            print >> o, '        _ct(\'%s\',' % (c.arg,)
            print >> o, '            %s,' % (generate_type_name(c),)
            print >> o, '            is_list=%s),' % (c.keyword == 'list',)
    print >> o, '    ]'
    return o.getvalue()


def generate_class(s):
    print('')
    print('')
    sys.stdout.write(generate_class_def(s))


def same_class_def(s1, s2):
    return generate_class_def(s1) == generate_class_def(s2)


# 'hoge-hoge' -> 'HogeHoge'
def pythonify(name):
    a = name.split('-')
    a = map(lambda x: x.capitalize(), a)  # XXX locale sensitive
    return ''.join(a)


def chop_suf(s, suf):
    if not s.endswith(suf):
        return s
    return s[:-len(suf)]


_classes = {}


def generate_type_name(s):
    try:
        return s.i_os_ken_class_name
    except AttributeError:
        pass
    s.i_os_ken_class_name = name = _generate_type_name(s)
    assert (name not in _classes) or same_class_def(_classes[name], s)
    _classes[name] = s
    return name


def _generate_type_name(s):
    # 1. use the hardcoded name for the top level capable-switch.
    if s.arg == 'capable-switch':
        return 'OFCapableSwitchType'
    # 2. pick the name from the first yang grouping.
    # this relies on the way OF-Config yang specification is written.
    t = s.search_one('uses')
    if t:
        return t.arg
    # 3. pick the name of yang type.
    t = s.search_one('type')
    if t:
        return t.arg
    # 4. infer from the parent's name.
    # if the parent is 'OFFooType' and our name is 'bar-baz',
    # use 'OFFooBarBazType'.
    # again, this relies on the way OF-Config yang specification is written.
    return (chop_suf(generate_type_name(s.parent), 'Type')
            + pythonify(s.arg)
            + 'Type')