From 9496ab8ecf5678e2c167bccc68862df796ded3db Mon Sep 17 00:00:00 2001 From: Ray Plante Date: Wed, 21 Aug 2019 13:03:57 -0400 Subject: [PATCH] preservation: IngestClient: added find_named() method --- python/nistoar/pdr/ingest/rmm.py | 26 +++++++++++++-- python/tests/nistoar/pdr/ingest/test_rmm.py | 35 ++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/python/nistoar/pdr/ingest/rmm.py b/python/nistoar/pdr/ingest/rmm.py index fed887afe..f8ad234a9 100644 --- a/python/nistoar/pdr/ingest/rmm.py +++ b/python/nistoar/pdr/ingest/rmm.py @@ -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) @@ -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): diff --git a/python/tests/nistoar/pdr/ingest/test_rmm.py b/python/tests/nistoar/pdr/ingest/test_rmm.py index 20138d665..156515cd3 100644 --- a/python/tests/nistoar/pdr/ingest/test_rmm.py +++ b/python/tests/nistoar/pdr/ingest/test_rmm.py @@ -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 @@ -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()