From 1d3696ba8ce7cf4732e02e59736ee2f8d99461a4 Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Sun, 30 Aug 2020 19:17:50 +0100 Subject: [PATCH] add some code for handling type --- yang/generate | 85 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/yang/generate b/yang/generate index d3360a73d..0437b3551 100755 --- a/yang/generate +++ b/yang/generate @@ -4,6 +4,7 @@ import re import os import sys import json +import decimal import shutil import pprint import urllib.request @@ -24,28 +25,70 @@ def write(string): sys.stdout.flush() +class Boolean(int): + def __new__(cls, value): + return int.__new__(cls, value not in ('false', False, 0)) + + def __init__(self, boolean): + self.string = boolean + + def __str__(self): + return self.string + + +class Decimal64(decimal.Decimal): + def __init__(cls, value, frac=0): + raise RuntimeError() + # look at https://github.com/CZ-NIC/yangson/blob/master/yangson/datatype.py#L682 + return super().__init__(decimal.Decimal(value)) + + class yang: - types = ( - 'binary', - 'bits', - 'boolean', - 'decimal64', - 'empty', - 'enumeration', - 'identityref', - 'instance-identifier', - 'int8', - 'int16', - 'int32', - 'int64', - 'leafref', - 'string', - 'uint8', - 'uint16', - 'uint32', - 'uint64', - 'union', - ) + restriction = { + 'binary': ['length'], + 'bits': ['bit'], + 'boolean': [], + 'decimal64': ['range'], + 'empty': [], + 'enumeration': ['enum'], + 'identityref': [], + 'instance-identifier': ['require-instance'], + 'int8': [], + 'int16': [], + 'int32': [], + 'int64': [], + 'leafref': ['path', 'require-instance'], + 'string': ['pattern', 'length'], + 'uint8': [], + 'uint16': [], + 'uint32': [], + 'uint64': [], + 'union': [], + } + + klass = { + 'binary': ['length'], + 'bits': ['bit'], + 'boolean': Boolean, + 'decimal64': Decimal64, + 'empty': None, + 'enumeration': None, + 'identityref': str, + 'instance-identifier': str, + 'int8': int, + 'int16': int, + 'int32': int, + 'int64': int, + 'leafref': str, + 'string': str, + 'uint8': int, + 'uint16': int, + 'uint32': int, + 'uint64': int, + 'union': None, + } + + types = list(restriction.keys()) words = ( 'extension',