From 506f6afa7b7ec80fa955ebd16fd917e5c4d8c19d Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 24 Aug 2012 17:10:43 -0400 Subject: [PATCH] cc-3936: Added function to recognize the owner of a file if it's there. Added corresponding tests. --- .../media-monitor2/media/monitor/pure.py | 20 +++++++++++++++++++ python_apps/media-monitor2/tests/test_pure.py | 12 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index fa4aa7480..bf6975068 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -418,6 +418,26 @@ def sub_path(directory,f): common = os.path.commonprefix([ normalized, normpath(f) ]) return common == normalized +def owner_id(original_path): + """ + Given 'original_path' return the file name of the of 'identifier' file. + return the id that is contained in it. If no file is found or nothing is + read then -1 is returned. File is deleted after the number has been read + """ + fname = "%s.identifier" % original_path + owner_id = -1 + try: + f = open(fname) + for line in f: + owner_id = int(line) + break + f.close() + except Exception: pass + else: + try: os.unlink(fname) + except Exception: raise + return owner_id + if __name__ == '__main__': import doctest doctest.testmod() diff --git a/python_apps/media-monitor2/tests/test_pure.py b/python_apps/media-monitor2/tests/test_pure.py index 59f88dabe..b4e870023 100644 --- a/python_apps/media-monitor2/tests/test_pure.py +++ b/python_apps/media-monitor2/tests/test_pure.py @@ -82,4 +82,16 @@ class TestMMP(unittest.TestCase): self.assertEqual( mmp.parse_int("123asf"), "123" ) self.assertEqual( mmp.parse_int("asdf"), None ) + def test_owner_id(self): + start_path = "testing.mp3" + id_path = "testing.mp3.identifier" + o_id = 123 + f = open(id_path, 'w') + f.write("123") + f.close() + possible_id = mmp.owner_id(start_path) + self.assertFalse( os.path.exists(id_path) ) + self.assertEqual( possible_id, o_id ) + self.assertEqual( -1, mmp.owner_id("something.random") ) + if __name__ == '__main__': unittest.main()