Added function to recognize the owner of a file if it's there.
Added corresponding tests.
This commit is contained in:
Rudi Grinberg 2012-08-24 17:10:43 -04:00
parent 3fb0fbd73f
commit 506f6afa7b
2 changed files with 32 additions and 0 deletions
python_apps/media-monitor2
media/monitor
tests

View File

@ -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()

View File

@ -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()