Skip to content

Commit

Permalink
convert: added Res2PODds class
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Aug 27, 2019
1 parent 32392eb commit e419a79
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
84 changes: 83 additions & 1 deletion python/nistoar/nerdm/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,89 @@ def massage_refs(self, nerd):
refs[i].update(newref)

return nerd


class Res2PODds(object):
"""
a class for converting a NERDm Resource object to a POD Dataset object.
Currently, there are no configuration parameters supported.
"""

_flavor = {
"midas": "resource2midaspodds",
"pdr": "resource2midaspodds",
}

def __init__(self, jqlibdir, config=None, logger=None):
"""
create the converter
:param jqlibdir str: path to the directory containing the nerdm jq
modules
:param config dict: a dictionary with conversion configuration data
in it; currently, no paramters are supported.
:param logger Logger: a logger object that can be used to write warning
messages
"""
self.jqt = {
"midas": jq.Jq('nerdm::resource2midaspodds', jqlibdir,
["nerdm2pod:nerdm"])
}
if config is None:
config = {}
self.cfg = config
self._log = logger

def _jq4flavor(self, flavor):
if flavor in self.jqt:
return self.jqt[flavor]

if flavor in self._flavors:
flavor = self._flavors[flavor]
self.jqt[flavor] = jq.Jq('nerdm::'+flavor, jqlibdir, ["nerdm2pod:nerdm"])
return self.jqt[flavor]

def convert(self, nerdm, flavor="midas"):
"""
convert JSON-encoded data to a resource object
:param nerdm str: a string containing the JSON-formatted input NERDm
Resource record
:param flavor str: a name indicating which flavor of POD to conver to;
recognized names include "midas" and "pdr"
(default: "midas")
"""
jqt = self._jq4flavor(flavor)
out = jqt.transform(nerdm)
return out

def convert_data(self, nerdm, flavor="midas"):
"""
convert parsed POD record data to a resource object
:param nerdm dict: A dictionary containing a NERDm Resource record
:param flavor str: a name indicating which flavor of POD to conver to;
recognized names include "midas" and "pdr"
(default: "midas")
"""
return self.convert(json.dumps(nerdm))

def convert_file(self, nerdmfile, flavor="midas"):
"""
convert parsed POD record data to a resource object
:param nerdmfile str: the path to a file containing a JSON-encoded
NERDm Resource record
:param flavor str: a name indicating which flavor of POD to conver to;
recognized names include "midas" and "pdr"
(default: "midas")
"""
jqt = self._jq4flavor(flavor)
out = jqt.transform_file(nerdmfile)
return out




class ComponentCounter(object):
"""
Expand Down
48 changes: 48 additions & 0 deletions python/nistoar/nerdm/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
datadir = os.path.join(jqlibdir, "tests", "data")
janaffile = os.path.join(datadir, "janaf_pod.json")
simplefile = os.path.join(datadir, "simple-nerdm.json")
schemadir = os.path.join(os.path.dirname(jqlibdir), "model")
nerddir = os.path.join(schemadir,"examples")

class TestPODds2Res(unittest.TestCase):

Expand Down Expand Up @@ -192,6 +194,52 @@ def test_massage_authors(self):
}
]

class TestRes2PODds(unittest.TestCase):

def test_ctor(self):
cvtr = cvt.Res2PODds(jqlibdir)
self.assertIn("midas", cvtr.jqt)

def test_convert_file(self):
cvtr = cvt.Res2PODds(jqlibdir)
pod = cvtr.convert_file(os.path.join(nerddir,"janaf.json"))
self.assertEquals(pod["accessLevel"], "public")
self.assertEquals(pod["@type"], "dcat:Dataset")

self.assertEqual(len(pod['references']), 2)
self.assertEqual(len(pod['distribution']), 319)
self.assertEqual(pod['contactPoint']['fn'], 'Thomas Allison')
self.assertEqual(pod['contactPoint']['hasEmail'],
'mailto:thomas.allison@nist.gov')

def test_convert(self):
cvtr = cvt.Res2PODds(jqlibdir)
with open(os.path.join(nerddir,"janaf.json")) as fd:
data = json.load(fd)
pod = cvtr.convert(json.dumps(data))
self.assertEquals(pod["accessLevel"], "public")
self.assertEquals(pod["@type"], "dcat:Dataset")

self.assertEqual(len(pod['references']), 2)
self.assertEqual(len(pod['distribution']), 319)
self.assertEqual(pod['contactPoint']['fn'], 'Thomas Allison')
self.assertEqual(pod['contactPoint']['hasEmail'],
'mailto:thomas.allison@nist.gov')

def test_convert_data(self):
cvtr = cvt.Res2PODds(jqlibdir)
with open(os.path.join(nerddir,"janaf.json")) as fd:
data = json.load(fd)
pod = cvtr.convert_data(data)
self.assertEquals(pod["accessLevel"], "public")
self.assertEquals(pod["@type"], "dcat:Dataset")

self.assertEqual(len(pod['references']), 2)
self.assertEqual(len(pod['distribution']), 319)
self.assertEqual(pod['contactPoint']['fn'], 'Thomas Allison')
self.assertEqual(pod['contactPoint']['hasEmail'],
'mailto:thomas.allison@nist.gov')

class TestComponentCounter(unittest.TestCase):

def test_inventory_collection(self):
Expand Down

0 comments on commit e419a79

Please sign in to comment.