Skip Wonnell | faae86d | 2018-02-20 14:42:34 -0600 | [diff] [blame] | 1 | ''' |
| 2 | /*- |
| 3 | * ============LICENSE_START======================================================= |
| 4 | * ONAP : APPC |
| 5 | * ================================================================================ |
| 6 | * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. |
| 7 | * ============================================================================= |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | * ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 21 | * ============LICENSE_END========================================================= |
| 22 | */ |
| 23 | ''' |
Dilip kumar Pampana | 108ff43 | 2018-01-08 15:08:21 -0500 | [diff] [blame] | 24 | #!/usr/bin/python |
| 25 | |
| 26 | import re |
| 27 | import sys |
| 28 | |
| 29 | |
| 30 | # Convert word from foo-bar to FooBar |
| 31 | # words begining with a digit will be converted to _digit |
| 32 | def to_enum(s): |
| 33 | if s[0].isdigit(): |
| 34 | s = "_" + s |
| 35 | else: |
| 36 | s = s[0].upper() + s[1:] |
| 37 | return re.sub(r'(?!^)-([a-zA-Z])', lambda m: m.group(1).upper(), s) |
| 38 | |
| 39 | leaf = "" |
| 40 | val = "" |
| 41 | li = [] |
| 42 | |
| 43 | if len(sys.argv) < 3: |
| 44 | print 'yang2props.py <input yang> <output properties>' |
| 45 | sys.exit(2) |
| 46 | |
| 47 | with open(sys.argv[1], "r") as ins: |
| 48 | for line in ins: |
| 49 | # if we see a leaf save the name for later |
| 50 | if "leaf " in line: |
| 51 | match = re.search(r'leaf (\S+)', line) |
| 52 | if match: |
| 53 | leaf = match.group(1) |
Skip Wonnell | faae86d | 2018-02-20 14:42:34 -0600 | [diff] [blame] | 54 | |
Dilip kumar Pampana | 108ff43 | 2018-01-08 15:08:21 -0500 | [diff] [blame] | 55 | # if we see enum convert the value to enum format and see if it changed |
| 56 | # if the value is different write a property entry |
| 57 | if "enum " in line: |
| 58 | match = re.search(r'enum "(\S+)";', line) |
| 59 | if match: |
| 60 | val = match.group(1) |
| 61 | enum = to_enum(val) |
| 62 | |
| 63 | # see if converting to enum changed the string |
| 64 | if val != enum: |
| 65 | property = "yang."+leaf+"."+enum+"="+val |
| 66 | if property not in li: |
| 67 | li.append( property) |
| 68 | |
| 69 | |
| 70 | # Open output file |
| 71 | fo = open(sys.argv[2], "wb") |
| 72 | fo.write("# yang conversion properties \n") |
| 73 | fo.write("# used to convert Enum back to the original yang value \n") |
| 74 | fo.write("\n".join(li)) |
| 75 | fo.write("\n") |
| 76 | |
| 77 | # Close opend file |
| 78 | fo.close() |
| 79 | |
Skip Wonnell | faae86d | 2018-02-20 14:42:34 -0600 | [diff] [blame] | 80 | |