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 | ''' |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -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 | |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 39 | |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 40 | leaf = "" |
| 41 | val = "" |
| 42 | li = [] |
| 43 | |
| 44 | if len(sys.argv) < 3: |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 45 | print('yang2props.py <input yang> <output properties>') |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 46 | sys.exit(2) |
| 47 | |
| 48 | with open(sys.argv[1], "r") as ins: |
| 49 | for line in ins: |
| 50 | # if we see a leaf save the name for later |
| 51 | if "leaf " in line: |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 52 | match = re.search(r'leaf (\S+)', line) |
| 53 | if match: |
| 54 | leaf = match.group(1) |
| 55 | |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 56 | # if we see enum convert the value to enum format and see if it changed |
| 57 | # if the value is different write a property entry |
| 58 | if "enum " in line: |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 59 | match = re.search(r'enum "(\S+)";', line) |
| 60 | if match: |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 61 | val = match.group(1) |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 62 | enum = to_enum(val) |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 63 | # see if converting to enum changed the string |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 64 | if val != enum: |
| 65 | property = "yang." + leaf + "." + enum + "=" + val |
| 66 | if property not in li: |
| 67 | li.append(property) |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 68 | # Open output file |
Ryan Young | b96311a | 2018-04-06 17:32:41 -0400 | [diff] [blame] | 69 | fo = open(sys.argv[2], "w") |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 70 | fo.write("# yang conversion properties \n") |
| 71 | fo.write("# used to convert Enum back to the original yang value \n") |
| 72 | fo.write("\n".join(li)) |
| 73 | fo.write("\n") |
| 74 | |
| 75 | # Close opend file |
| 76 | fo.close() |
Skip Wonnell | ab6c2c0 | 2017-08-14 17:47:10 -0500 | [diff] [blame] | 77 | |