Skip to content

Commit

Permalink
preservation: IngestClient: added find_named() method
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Aug 21, 2019
1 parent 2cf79e4 commit 9496ab8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
26 changes: 24 additions & 2 deletions python/nistoar/pdr/ingest/rmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Module providing client-side support for the RMM ingest service.
"""
import os, sys, shutil, logging, requests
from collections import Mapping, Sequence
from collections import Mapping, Sequence, OrderedDict

from ..exceptions import (StateException, ConfigurationException, PDRException,
NERDError)
Expand Down Expand Up @@ -363,7 +363,29 @@ def submit(self, name=None):

return { "succeeded": [], "failed": [], "skipped": staged }


def find_named(self, name):
"""
return the paths to cached records with the given name. The result is
returned as a dictionary that maps states under which the record is found
("succeeded", "failed", "staged", and "in_progress") to a file path for
the cached record. Because a record with a given name can be ingested
multiple times, versions may exist under multiple states (such as
"succeeded" from the first ingest, and "staged" for the subsequent ingest).
"""
bases = OrderedDict([
("succeeded", self._successdir),
("in_progress", self._inprogdir),
("staged", self._stagedir),
("failed", self._faildir)
])

out = OrderedDict()
for state in bases:
path = os.path.join(bases[state], name+".json")
if os.path.exists(path):
out[state] = path

return out


class IngestServiceException(PDRException):
Expand Down
35 changes: 34 additions & 1 deletion python/tests/nistoar/pdr/ingest/test_rmm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os, pdb, sys, json, requests, logging, time, re
import os, pdb, sys, json, requests, logging, time, re, shutil
import unittest as test
from copy import deepcopy
from collections import Mapping

from nistoar.testing import *
from nistoar.pdr.ingest import rmm
Expand Down Expand Up @@ -318,6 +319,38 @@ def test_submit_all_failed(self):
self.assertTrue(os.path.exists(os.path.join(self.faildir,"bro.json")))
self.assertTrue(os.path.exists(os.path.join(self.faildir,"bru.json")))

def test_find_named(self):
sfile = os.path.join(self.stagedir, "bru.json")
rec = getrec()
self.cl.stage(rec, 'bru')
self.assertTrue(os.path.exists(sfile))
self.assertEqual(self.cl.staged_names(), ["bru"])

found = self.cl.find_named("bru")
self.assertTrue(isinstance(found, Mapping))
self.assertEqual(len(found), 1)
self.assertIn('staged', found)
self.assertEqual(found['staged'], sfile)

shutil.copy(sfile, os.path.join(self.successdir, "bru.json"))
found = self.cl.find_named("bru")
self.assertEqual(len(found), 2)
self.assertIn('staged', found)
self.assertIn('succeeded', found)
self.assertEqual(found['staged'], sfile)
self.assertEqual(found['succeeded'],
os.path.join(self.successdir, "bru.json"))

shutil.copy(sfile, os.path.join(self.faildir, "bru.json"))
found = self.cl.find_named("bru")
self.assertEqual(len(found), 3)
self.assertIn('staged', found)
self.assertIn('failed', found)
self.assertIn('succeeded', found)
self.assertEqual(found['staged'], sfile)
self.assertEqual(found['failed'], os.path.join(self.faildir, "bru.json"))



if __name__ == '__main__':
test.main()

0 comments on commit 9496ab8

Please sign in to comment.