Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pycoreconf to add multiple SID definition files for a given model #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pycoreconf/pycoreconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class CORECONFModel(ModelSID):
to convert to and from CORECONF/CBOR representation."""

def __init__(self,
sid_file,
*sid_files,
model_description_file=None):

self.model_description_file = model_description_file
self.yang_ietf_modules_paths = ["."]
ModelSID.__init__(self, sid_file)
ModelSID.__init__(self, *sid_files)

def add_modules_path(self, path):
"""
Expand Down
78 changes: 45 additions & 33 deletions pycoreconf/sid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,77 @@ class ModelSID:
Class to define methods for reading a YANG model SID file and hold values.
"""

def __init__(self, sid_file):
self.sid_file = sid_file
def __init__(self, *sid_files):
self.sid_files = sid_files
self.sids, self.types, self.name = self.getSIDsAndTypes() #req. ltn22/pyang
self.ids = {v: k for k, v in self.sids.items()} # {sid:id}

def getSIDsAndTypes(self):
"""
Read SID file and return { identifier : sid } + { identifier : type } dictionaries.
"""
# Read the contents of the sid/json file
f = open(self.sid_file, "r")
obj = json.load(f)
f.close()

# Get items & map identifier : sid and leafIdentifier : typename
sids = {} # init
types = {} # init
items = obj["items"] # list
for item in items:
sids[item["identifier"]] = item["sid"]
if "type" in item.keys():
types[item["identifier"]] = item["type"]
names = []

for sid_file in self.sid_files:

# Read the contents of the sid/json files
f = open(sid_file, "r")
obj = json.load(f)
f.close()

# Get items & map identifier : sid and leafIdentifier : typename

items = obj["items"] # list
for item in items:
sids[item["identifier"]] = item["sid"]
if "type" in item.keys():
types[item["identifier"]] = item["type"]

# tmp while single module support:
name = obj["module-name"]
# tmp while single module support:
names.append(obj["module-name"])

# NOTE IF there are multiple SID files, the names will be concatenated
# Concatenate all the names separated by a comma
name = ', '.join(names)
print("SIDS ", len(sids), len(types))
return sids, types, name


def getIdentifiers(self):
"""
Read SID file and return { sid : identifier } dictionary.
"""
# Read the contents of the sid/json file
f = open(self.sid_file, "r")
obj = json.load(f)
f.close()

# Get items & map identifier : sid
ids = {} # init
items = obj["items"] # list
for item in items:
ids[item["sid"]] = item["identifier"]
for sid_file in self.sid_files:
# Read the contents of the sid/json file
f = open(sid_file, "r")
obj = json.load(f)
f.close()

# Get items & map identifier : sid
ids = {} # init
items = obj["items"] # list
for item in items:
ids[item["sid"]] = item["identifier"]

return ids

def getSIDs(self):
"""
Read SID file and return { identifier : sid } dictionary.
"""
# Read the contents of the sid/json file
f = open(self.sid_file, "r")
obj = json.load(f)
f.close()

# Get items & map identifier : sid
sids = {} # init
items = obj["items"] # list
for item in items:
sids[item["identifier"]] = item["sid"]
for sid_file in self.sid_files:
# Read the contents of the sid/json file
f = open(self.sid_file, "r")
obj = json.load(f)
f.close()

# Get items & map identifier : sid
items = obj["items"] # list
for item in items:
sids[item["identifier"]] = item["sid"]

return sids