Skip to content

Commit

Permalink
added resolve() function that resolves ID into fully expanded nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed May 10, 2018
1 parent 3129e60 commit a2f93ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions codemeta/codemeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ def clean(data):
data['identifier'] = data['identifier'].lower()
return data

def resolve(data, idmap=None):
"""Resolve nodes that refer to an ID mentioned earlier"""
if idmap is None: idmap = {}
for k,v in data.items():
if isinstance(v, (dict, OrderedDict)):
if '@id' in v:
if len(v) > 1:
#this is not a reference, register in idmap (possibly overwriting earlier definition!)
idmap[v['@id']] = v
elif len(v) == 1:
#this is a reference
if v['@id'] in idmap:
data[k] = idmap[v['@id']]
else:
print("NOTICE: Unable to resolve @id " + v['@id'] ,file=sys.stderr)
resolve(v, idmap)
elif isinstance(v, (tuple, list)):
data[k] = [ resolve(x,idmap) if isinstance(x, (dict,OrderedDict)) else x for x in v ]
return data

def getregistry(identifier, registry):
for tool in registry['@graph']:
if tool['identifier'].lower() == identifier.lower():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def read(fname):

setup(
name = "CodeMetaPy",
version = "0.1.2",
version = "0.1.3",
author = "Maarten van Gompel",
author_email = "proycon@anaproy.nl",
description = ("Generate CodeMeta metadata for Python packages"),
Expand Down

0 comments on commit a2f93ff

Please sign in to comment.