This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema.py
966 lines (762 loc) · 28.3 KB
/
schema.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
# Copyright (c) 2018-2019 Robin Jarry
# SPDX-License-Identifier: MIT
from _libyang import ffi
from _libyang import lib
from .util import c2str
from .util import str2c
#------------------------------------------------------------------------------
def schema_in_format(fmt_string):
if fmt_string == 'yang':
return lib.LYS_IN_YANG
if fmt_string == 'yin':
return lib.LYS_IN_YIN
raise ValueError('unknown schema input format: %r' % fmt_string)
#------------------------------------------------------------------------------
def schema_out_format(fmt_string):
if fmt_string == 'yang':
return lib.LYS_OUT_YANG
if fmt_string == 'yin':
return lib.LYS_OUT_YIN
if fmt_string == 'tree':
return lib.LYS_OUT_TREE
if fmt_string == 'info':
return lib.LYS_OUT_INFO
if fmt_string == 'json':
return lib.LYS_OUT_JSON
raise ValueError('unknown schema output format: %r' % fmt_string)
#------------------------------------------------------------------------------
class Module(object):
def __init__(self, context, module_p):
self.context = context
self._module = module_p
def name(self):
return c2str(self._module.name)
def prefix(self):
return c2str(self._module.prefix)
def description(self):
return c2str(self._module.dsc)
def filepath(self):
return c2str(self._module.filepath)
def implemented(self):
return bool(lib.lypy_module_implemented(self._module))
def feature_enable(self, name):
ret = lib.lys_features_enable(self._module, str2c(name))
if ret != 0:
raise self.context.error('no such feature: %r' % name)
def feature_enable_all(self):
self.feature_enable('*')
def feature_disable(self, name):
ret = lib.lys_features_disable(self._module, str2c(name))
if ret != 0:
raise self.context.error('no such feature: %r' % name)
def feature_disable_all(self):
self.feature_disable('*')
def feature_state(self, name):
ret = lib.lys_features_state(self._module, str2c(name))
if ret < 0:
raise self.context.error('no such feature: %r' % name)
return bool(ret)
def features(self):
for i in range(self._module.features_size):
yield Feature(self.context, self._module.features[i])
def get_feature(self, name):
for f in self.features():
if f.name() == name:
return f
raise self.context.error('no such feature: %r' % name)
def revisions(self):
for i in range(self._module.rev_size):
yield Revision(self.context, self._module.rev[i])
def __iter__(self):
return self.children()
def children(self, types=None):
return iter_children(self.context, self._module, types=types)
def __str__(self):
return self.name()
def print_mem(self, fmt='tree', path=None):
fmt = schema_out_format(fmt)
buf = ffi.new('char **')
ret = lib.lys_print_mem(buf, self._module, fmt, str2c(path), 0, 0)
if ret != 0:
raise self.context.error('cannot print module')
try:
return c2str(buf[0])
finally:
lib.free(buf[0])
def print_file(self, fileobj, fmt='tree', path=None):
fmt = schema_out_format(fmt)
ret = lib.lys_print_fd(
fileobj.fileno(), self._module, fmt, str2c(path), 0, 0)
if ret != 0:
raise self.context.error('cannot print module')
def parse_data_dict(self, dic, parent=None,
rpc_input=False, rpc_output=False):
"""
Convert a python dictionary to a DNode object following the schema of
this module. The returned value is always a top-level data node (i.e.:
without parent).
:arg dict dic:
The python dictionary to convert.
:arg DNode parent:
Optional parent to update. If not specified a new top-level DNode
will be created.
:arg bool rpc_input:
If True, dic will be parsed by looking in the rpc input nodes.
:arg bool rpc_output:
If True, dic will be parsed by looking in the rpc output nodes.
"""
from .data import dict_to_dnode # circular import
return dict_to_dnode(dic, self, parent=parent,
rpc_input=rpc_input, rpc_output=rpc_output)
#------------------------------------------------------------------------------
class Revision(object):
def __init__(self, context, rev_p):
self.context = context
self._rev = rev_p
def date(self):
return c2str(self._rev.date)
def description(self):
return c2str(self._rev.dsc)
def reference(self):
return c2str(self._rev.ref)
def extensions(self):
for i in range(self._rev.ext_size):
yield Extension(self.context, self._rev.ext[i])
def get_extension(self, name, prefix=None, arg_value=None):
ext = lib.lypy_find_ext(
self._rev.ext, self._rev.ext_size,
str2c(name), str2c(prefix), str2c(arg_value))
if ext:
return Extension(self.context, ext)
return None
def __repr__(self):
cls = self.__class__
return '<%s.%s: %s>' % (cls.__module__, cls.__name__, str(self))
def __str__(self):
return self.date()
#------------------------------------------------------------------------------
class Extension(object):
def __init__(self, context, ext_p):
self.context = context
self._ext = ext_p
self._def = getattr(ext_p, 'def')
def name(self):
return c2str(self._def.name)
def argument(self):
return c2str(self._ext.arg_value)
def module(self):
module_p = lib.lys_main_module(self._def.module)
if not module_p:
raise self.context.error('cannot get module')
return Module(self.context, module_p)
def __repr__(self):
cls = self.__class__
return '<%s.%s: %s>' % (cls.__module__, cls.__name__, str(self))
def __str__(self):
return self.name()
#------------------------------------------------------------------------------
class Type(object):
DER = lib.LY_TYPE_DER
BINARY = lib.LY_TYPE_BINARY
BITS = lib.LY_TYPE_BITS
BOOL = lib.LY_TYPE_BOOL
DEC64 = lib.LY_TYPE_DEC64
EMPTY = lib.LY_TYPE_EMPTY
ENUM = lib.LY_TYPE_ENUM
IDENT = lib.LY_TYPE_IDENT
INST = lib.LY_TYPE_INST
LEAFREF = lib.LY_TYPE_LEAFREF
STRING = lib.LY_TYPE_STRING
UNION = lib.LY_TYPE_UNION
INT8 = lib.LY_TYPE_INT8
UINT8 = lib.LY_TYPE_UINT8
INT16 = lib.LY_TYPE_INT16
UINT16 = lib.LY_TYPE_UINT16
INT32 = lib.LY_TYPE_INT32
UINT32 = lib.LY_TYPE_UINT32
INT64 = lib.LY_TYPE_INT64
UINT64 = lib.LY_TYPE_UINT64
BASENAMES = {
DER: 'derived',
BINARY: 'binary',
BITS: 'bits',
BOOL: 'boolean',
DEC64: 'decimal64',
EMPTY: 'empty',
ENUM: 'enumeration',
IDENT: 'identityref',
INST: 'instance-id',
LEAFREF: 'leafref',
STRING: 'string',
UNION: 'union',
INT8: 'int8',
UINT8: 'uint8',
INT16: 'int16',
UINT16: 'uint16',
INT32: 'int32',
UINT32: 'uint32',
INT64: 'int64',
UINT64: 'uint64',
}
def __init__(self, context, type_p):
self.context = context
self._type = type_p
def get_bases(self):
if self._type.base == lib.LY_TYPE_DER:
for b in self.derived_type().get_bases():
yield b
elif self._type.base == lib.LY_TYPE_LEAFREF:
for b in self.leafref_type().get_bases():
yield b
elif self._type.base == lib.LY_TYPE_UNION:
for t in self.union_types():
for b in t.get_bases():
yield b
else: # builtin type
yield self
def name(self):
if self._type.der:
return c2str(self._type.der.name)
return self.basename()
def description(self):
if self._type.der:
return c2str(self._type.der.dsc)
return None
def base(self):
return self._type.base
def bases(self):
for b in self.get_bases():
yield b.base()
def basename(self):
return self.BASENAMES.get(self._type.base, 'unknown')
def basenames(self):
for b in self.get_bases():
yield b.basename()
def derived_type(self):
if not self._type.der:
return None
return Type(self.context, ffi.addressof(self._type.der.type))
def leafref_type(self):
if self._type.base != self.LEAFREF:
return None
lref = self._type.info.lref
return Type(self.context, ffi.addressof(lref.target.type))
def union_types(self):
if self._type.base != self.UNION:
return
t = self._type
while t.info.uni.count == 0:
t = ffi.addressof(t.der.type)
for i in range(t.info.uni.count):
yield Type(self.context, t.info.uni.types[i])
def enums(self):
if self._type.base != self.ENUM:
return
t = self._type
while t.info.enums.count == 0:
t = ffi.addressof(t.der.type)
for i in range(t.info.enums.count):
e = t.info.enums.enm[i]
yield c2str(e.name), c2str(e.dsc)
def all_enums(self):
for b in self.get_bases():
for e in b.enums():
yield e
def bits(self):
if self._type.base != self.BITS:
return
t = self._type
while t.info.bits.count == 0:
t = ffi.addressof(t.der.type)
for i in range(t.info.bits.count):
b = t.info.bits.bit[i]
yield c2str(b.name), c2str(b.dsc)
def all_bits(self):
for b in self.get_bases():
for bb in b.bits():
yield bb
NUM_TYPES = frozenset(
(INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64))
def range(self):
if self._type.base in self.NUM_TYPES and self._type.info.num.range:
return c2str(self._type.info.num.range.expr)
elif self._type.base == self.DEC64 and self._type.info.dec64.range:
return c2str(self._type.info.dec64.range.expr)
elif self._type.der:
return self.derived_type().range()
return None
def all_ranges(self):
if self._type.base == lib.LY_TYPE_UNION:
for t in self.union_types():
for r in t.all_ranges():
yield r
else:
rng = self.range()
if rng is not None:
yield rng
def length(self):
if self._type.base == self.STRING and self._type.info.str.length:
return c2str(self._type.info.str.length.expr)
elif self._type.base == self.BINARY and self._type.info.binary.length:
return c2str(self._type.info.binary.length.expr)
elif self._type.der:
return self.derived_type().length()
return None
def all_lengths(self):
if self._type.base == lib.LY_TYPE_UNION:
for t in self.union_types():
for l in t.all_lengths():
yield l
else:
length = self.length()
if length is not None:
yield length
def patterns(self):
if self._type.base != self.STRING:
return
for i in range(self._type.info.str.pat_count):
p = self._type.info.str.patterns[i]
if not p:
continue
# in case of pattern restriction, the first byte has a special
# meaning: 0x06 (ACK) for regular match and 0x15 (NACK) for
# invert-match
invert_match = p.expr[0] == 0x15
# yield tuples like:
# ('[a-zA-Z_][a-zA-Z0-9\-_.]*', False)
# ('[xX][mM][lL].*', True)
yield c2str(p.expr + 1), invert_match
if self._type.der:
for p in self.derived_type().patterns():
yield p
def all_patterns(self):
if self._type.base == lib.LY_TYPE_UNION:
for t in self.union_types():
for p in t.all_patterns():
yield p
else:
for p in self.patterns():
yield p
def module(self):
module_p = lib.lys_main_module(self._type.der.module)
if not module_p:
raise self.context.error('cannot get module')
return Module(self.context, module_p)
def extensions(self):
for i in range(self._type.ext_size):
yield Extension(self.context, self._type.ext[i])
if self._type.parent:
for i in range(self._type.parent.ext_size):
yield Extension(self.context, self._type.parent.ext[i])
def get_extension(self, name, prefix=None, arg_value=None):
ext = lib.lypy_find_ext(
self._type.ext, self._type.ext_size,
str2c(name), str2c(prefix), str2c(arg_value))
if not ext and self._type.parent:
ext = lib.lypy_find_ext(
self._type.parent.ext, self._type.parent.ext_size,
str2c(name), str2c(prefix), str2c(arg_value))
if ext:
return Extension(self.context, ext)
return None
def __repr__(self):
cls = self.__class__
return '<%s.%s: %s>' % (cls.__module__, cls.__name__, str(self))
def __str__(self):
return self.name()
#------------------------------------------------------------------------------
class Feature(object):
def __init__(self, context, feature_p):
self.context = context
self._feature = feature_p
def name(self):
return c2str(self._feature.name)
def description(self):
return c2str(self._feature.dsc)
def reference(self):
return c2str(self._feature.ref)
def state(self):
return bool(self._feature.flags & lib.LYS_FENABLED)
def deprecated(self):
return bool(self._feature.flags & lib.LYS_STATUS_DEPRC)
def obsolete(self):
return bool(self._feature.flags & lib.LYS_STATUS_OBSLT)
def if_features(self):
for i in range(self._feature.iffeature_size):
yield IfFeatureExpr(self.context, self._feature.iffeature[i])
def module(self):
module_p = lib.lys_main_module(self._feature.module)
if not module_p:
raise self.context.error('cannot get module')
return Module(self.context, module_p)
def __str__(self):
return self.name()
#------------------------------------------------------------------------------
class IfFeatureExpr(object):
def __init__(self, context, iffeature_p):
self.context = context
self._iffeature = iffeature_p
def _get_operator(self, position):
# the ->exp field is a 2bit array of operator values stored under
# a uint8_t C array.
mask = 0x3 # 2bits mask
shift = 2 * (position % 4)
item = self._iffeature.expr[position // 4]
result = item & (mask << shift)
return result >> shift
def _operands(self):
op_index = 0
ft_index = 0
expected = 1
while expected > 0:
operator = self._get_operator(op_index)
op_index += 1
if operator == lib.LYS_IFF_F:
yield IfFeature(self.context, self._iffeature.features[ft_index])
ft_index += 1
expected -= 1
elif operator == lib.LYS_IFF_NOT:
yield IfNotFeature
elif operator == lib.LYS_IFF_AND:
yield IfAndFeatures
expected += 1
elif operator == lib.LYS_IFF_OR:
yield IfOrFeatures
expected += 1
def tree(self):
def _tree(operands):
op = next(operands)
if op is IfNotFeature:
return op(self.context, _tree(operands))
elif op in (IfAndFeatures, IfOrFeatures):
return op(self.context, _tree(operands), _tree(operands))
else:
return op
return _tree(self._operands())
def dump(self):
return self.tree().dump()
def __str__(self):
return str(self.tree()).strip('()')
#------------------------------------------------------------------------------
class IfFeatureExprTree(object):
def dump(self, indent=0):
raise NotImplementedError()
def __str__(self):
raise NotImplementedError()
#------------------------------------------------------------------------------
class IfFeature(IfFeatureExprTree):
def __init__(self, context, feature_p):
self.context = context
self._feature = feature_p
def feature(self):
return Feature(self.context, self._feature)
def dump(self, indent=0):
feat = self.feature()
return '%s%s [%s]\n' % (' ' * indent, feat.name(), feat.description())
def __str__(self):
return self.feature().name()
#------------------------------------------------------------------------------
class IfNotFeature(IfFeatureExprTree):
def __init__(self, context, child):
self.context = context
self.child = child
def dump(self, indent=0):
return ' ' * indent + 'NOT\n' + self.child.dump(indent + 1)
def __str__(self):
return 'NOT %s' % self.child
#------------------------------------------------------------------------------
class IfAndFeatures(IfFeatureExprTree):
def __init__(self, context, a, b):
self.context = context
self.a = a
self.b = b
def dump(self, indent=0):
s = ' ' * indent + 'AND\n'
s += self.a.dump(indent + 1)
s += self.b.dump(indent + 1)
return s
def __str__(self):
return '%s AND %s' % (self.a, self.b)
#------------------------------------------------------------------------------
class IfOrFeatures(IfFeatureExprTree):
def __init__(self, context, a, b):
self.context = context
self.a = a
self.b = b
def dump(self, indent=0):
s = ' ' * indent + 'OR\n'
s += self.a.dump(indent + 1)
s += self.b.dump(indent + 1)
return s
def __str__(self):
return '(%s OR %s)' % (self.a, self.b)
#------------------------------------------------------------------------------
class SNode(object):
CONTAINER = lib.LYS_CONTAINER
LEAF = lib.LYS_LEAF
LEAFLIST = lib.LYS_LEAFLIST
LIST = lib.LYS_LIST
RPC = lib.LYS_RPC
INPUT = lib.LYS_INPUT
OUTPUT = lib.LYS_OUTPUT
KEYWORDS = {
CONTAINER: 'container',
LEAF: 'leaf',
LEAFLIST: 'leaf-list',
LIST: 'list',
RPC: 'rpc',
INPUT: 'input',
OUTPUT: 'output',
}
def __init__(self, context, node_p):
self.context = context
self._node = node_p
def nodetype(self):
return self._node.nodetype
def keyword(self):
return self.KEYWORDS.get(self._node.nodetype, '???')
def name(self):
return c2str(self._node.name)
def fullname(self):
try:
s = lib.lypy_node_fullname(self._node)
return c2str(s)
finally:
lib.free(s)
def description(self):
return c2str(self._node.dsc)
def config_set(self):
return bool(self._node.flags & lib.LYS_CONFIG_SET)
def config_false(self):
return bool(self._node.flags & lib.LYS_CONFIG_R)
def mandatory(self):
return bool(self._node.flags & lib.LYS_MAND_TRUE)
def deprecated(self):
return bool(self._node.flags & lib.LYS_STATUS_DEPRC)
def obsolete(self):
return bool(self._node.flags & lib.LYS_STATUS_OBSLT)
def status(self):
if self._node.flags & lib.LYS_STATUS_DEPRC:
return 'deprecated'
elif self._node.flags & lib.LYS_STATUS_OBSLT:
return 'obsolete'
return 'current'
def module(self):
module_p = lib.lys_node_module(self._node)
if not module_p:
raise self.context.error('cannot get module')
return Module(self.context, module_p)
def schema_path(self):
try:
s = lib.lys_path(self._node, 0)
return c2str(s)
finally:
lib.free(s)
def data_path(self):
try:
s = lib.lypy_data_path_pattern(self._node)
return c2str(s)
finally:
lib.free(s)
def extensions(self):
for i in range(self._node.ext_size):
yield Extension(self.context, self._node.ext[i])
def get_extension(self, name, prefix=None, arg_value=None):
ext = lib.lypy_find_ext(
self._node.ext, self._node.ext_size,
str2c(name), str2c(prefix), str2c(arg_value))
if ext:
return Extension(self.context, ext)
return None
def if_features(self):
for i in range(self._node.iffeature_size):
yield IfFeatureExpr(self.context, self._node.iffeature[i])
def parent(self):
parent_p = lib.lys_parent(self._node)
while parent_p and parent_p.nodetype not in SNode.NODETYPE_CLASS:
parent_p = lib.lys_parent(parent_p)
if parent_p:
return SNode.new(self.context, parent_p)
return None
def __repr__(self):
cls = self.__class__
return '<%s.%s: %s>' % (cls.__module__, cls.__name__, str(self))
def __str__(self):
return self.name()
def parse_data_dict(self, dic, parent=None,
rpc_input=False, rpc_output=False):
"""
Convert a python dictionary to a DNode object following the schema of
this module. The returned value is always a top-level data node (i.e.:
without parent).
:arg dict dic:
The python dictionary to convert.
:arg DNode parent:
Optional parent to update. If not specified a new top-level DNode
will be created.
:arg bool rpc_input:
If True, dic will be parsed by looking in the rpc input nodes.
:arg bool rpc_output:
If True, dic will be parsed by looking in the rpc output nodes.
"""
from .data import dict_to_dnode # circular import
return dict_to_dnode(dic, self, parent=parent,
rpc_input=rpc_input, rpc_output=rpc_output)
NODETYPE_CLASS = {}
@classmethod
def register(cls, nodetype):
def _decorator(nodeclass):
cls.NODETYPE_CLASS[nodetype] = nodeclass
return nodeclass
return _decorator
@classmethod
def new(cls, context, node_p):
nodecls = cls.NODETYPE_CLASS.get(node_p.nodetype, SNode)
return nodecls(context, node_p)
#------------------------------------------------------------------------------
@SNode.register(SNode.LEAF)
class SLeaf(SNode):
def __init__(self, context, node_p):
SNode.__init__(self, context, node_p)
self._leaf = ffi.cast('struct lys_node_leaf *', node_p)
def default(self):
return c2str(self._leaf.dflt)
def units(self):
return c2str(self._leaf.units)
def type(self):
return Type(self.context, ffi.addressof(self._leaf.type))
def is_key(self):
if lib.lys_is_key(self._leaf, ffi.NULL):
return True
return False
def must_conditions(self):
for i in range(self._leaf.must_size):
yield c2str(self._leaf.must[i].expr)
def __str__(self):
return '%s %s' % (self.name(), self.type().name())
#------------------------------------------------------------------------------
@SNode.register(SNode.LEAFLIST)
class SLeafList(SNode):
def __init__(self, context, node_p):
SNode.__init__(self, context, node_p)
self._leaflist = ffi.cast('struct lys_node_leaflist *', node_p)
def ordered(self):
return bool(self._node.flags & lib.LYS_USERORDERED)
def units(self):
return c2str(self._leaflist.units)
def type(self):
return Type(self.context, ffi.addressof(self._leaflist.type))
def defaults(self):
for i in range(self._leaflist.dflt_size):
yield c2str(self._leaflist.dflt[i])
def must_conditions(self):
for i in range(self._leaflist.must_size):
yield c2str(self._leaflist.must[i].expr)
def __str__(self):
return '%s %s' % (self.name(), self.type().name())
#------------------------------------------------------------------------------
@SNode.register(SNode.CONTAINER)
class SContainer(SNode):
def __init__(self, context, node_p):
SNode.__init__(self, context, node_p)
self._container = ffi.cast('struct lys_node_container *', node_p)
def presence(self):
return c2str(self._container.presence)
def must_conditions(self):
for i in range(self._container.must_size):
yield c2str(self._container.must[i].expr)
def __iter__(self):
return self.children()
def children(self, types=None):
return iter_children(self.context, self._node, types=types)
#------------------------------------------------------------------------------
@SNode.register(SNode.LIST)
class SList(SNode):
def __init__(self, context, node_p):
SNode.__init__(self, context, node_p)
self._list = ffi.cast('struct lys_node_list *', node_p)
def ordered(self):
return bool(self._node.flags & lib.LYS_USERORDERED)
def __iter__(self):
return self.children()
def children(self, skip_keys=False, types=None):
return iter_children(
self.context, self._node, skip_keys=skip_keys, types=types)
def keys(self):
for i in range(self._list.keys_size):
node = ffi.cast('struct lys_node *', self._list.keys[i])
yield SLeaf(self.context, node)
def must_conditions(self):
for i in range(self._list.must_size):
yield c2str(self._list.must[i].expr)
def __str__(self):
return '%s [%s]' % (
self.name(), ', '.join(k.name() for k in self.keys()))
#------------------------------------------------------------------------------
@SNode.register(SNode.INPUT)
@SNode.register(SNode.OUTPUT)
class SRpcInOut(SNode):
def __iter__(self):
return self.children()
def must_conditions(self):
return ()
def children(self, types=None):
return iter_children(self.context, self._node, types=types)
#------------------------------------------------------------------------------
@SNode.register(SNode.RPC)
class SRpc(SNode):
def must_conditions(self):
return ()
def input(self):
try:
return next(iter_children(
self.context, self._node, types=(self.INPUT,),
options=lib.LYS_GETNEXT_WITHINOUT))
except StopIteration:
return None
def output(self):
try:
return next(iter_children(
self.context, self._node, types=(self.OUTPUT,),
options=lib.LYS_GETNEXT_WITHINOUT))
except StopIteration:
return None
def __iter__(self):
return self.children()
def children(self, types=None):
return iter_children(self.context, self._node, types=types)
#------------------------------------------------------------------------------
def iter_children(context, parent, skip_keys=False, types=None, options=0):
if types is None:
types = (lib.LYS_CONTAINER, lib.LYS_LIST, lib.LYS_RPC,
lib.LYS_LEAF, lib.LYS_LEAFLIST)
def _skip(node):
if node.nodetype not in types:
return True
if not skip_keys:
return False
if node.nodetype != lib.LYS_LEAF:
return False
leaf = ffi.cast('struct lys_node_leaf *', node)
if lib.lys_is_key(leaf, ffi.NULL):
return True
return False
if ffi.typeof(parent) == ffi.typeof('struct lys_module *'):
module = parent
parent = ffi.NULL
else:
module = ffi.NULL
child = lib.lys_getnext(ffi.NULL, parent, module, options)
while child:
if not _skip(child):
yield SNode.new(context, child)
child = lib.lys_getnext(child, parent, module, options)
#------------------------------------------------------------------------------
# compat
Container = SContainer
Leaf = SLeaf
LeafList = SLeafList
List = SList
Node = SNode
Rpc = SRpc
RpcInOut = SRpcInOut