From ef7c30b55e7d1f6a65f297ab3c49be55f9079ff7 Mon Sep 17 00:00:00 2001
From: Rudi Grinberg <rudi.grinberg@sourcefabric.org>
Date: Wed, 29 Aug 2012 18:08:14 -0400
Subject: [PATCH] cc-4241: Partially resolved issue. Added tests.

---
 .../media-monitor2/media/monitor/metadata.py     | 10 ++++++++++
 .../media-monitor2/media/monitor/organizer.py    |  4 ++++
 python_apps/media-monitor2/media/monitor/pure.py |  8 +++++++-
 python_apps/media-monitor2/tests/test_pure.py    | 16 ++++++++++++++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/python_apps/media-monitor2/media/monitor/metadata.py b/python_apps/media-monitor2/media/monitor/metadata.py
index f718b7b2e..70828faeb 100644
--- a/python_apps/media-monitor2/media/monitor/metadata.py
+++ b/python_apps/media-monitor2/media/monitor/metadata.py
@@ -105,6 +105,16 @@ class Metadata(Loggable):
     # little bit messy. Some of the handling is in m.m.pure while the rest is
     # here. Also interface is not very consistent
 
+    @staticmethod
+    def fix_title(self, path):
+        # If we have no title in path we will format it
+        # TODO : this is very hacky so make sure to fix it
+        m = mutagen.File(path, easy=True)
+        if u'title' not in m:
+            new_title = unicode( mmp.no_extension_basename(path) )
+            m[u'title'] = new_title
+            m.save()
+
     @staticmethod
     def airtime_dict(d):
         """
diff --git a/python_apps/media-monitor2/media/monitor/organizer.py b/python_apps/media-monitor2/media/monitor/organizer.py
index bf6f672d9..e8b22d680 100644
--- a/python_apps/media-monitor2/media/monitor/organizer.py
+++ b/python_apps/media-monitor2/media/monitor/organizer.py
@@ -6,6 +6,7 @@ from media.monitor.handler    import ReportHandler
 from media.monitor.log        import Loggable
 from media.monitor.exceptions import BadSongFile
 from media.monitor.events     import OrganizeFile
+from media.monitor.metadata   import Metadata
 from pydispatch               import dispatcher
 from os.path                  import dirname
 
@@ -65,8 +66,11 @@ class Organizer(ReportHandler,Loggable):
                             directory=d)
                 return cb
 
+            Metadata.fix_title(event.path)
+
             mmp.magic_move(event.path, new_path,
                     after_dir_make=new_dir_watch(dirname(new_path)))
+
             owners.add_file_owner(new_path, mmp.owner_id(event.path) )
             self.logger.info('Organized: "%s" into "%s"' %
                     (event.path, new_path))
diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py
index 25a0add28..2a1d7e483 100644
--- a/python_apps/media-monitor2/media/monitor/pure.py
+++ b/python_apps/media-monitor2/media/monitor/pure.py
@@ -252,15 +252,21 @@ def normalized_metadata(md, original_path):
         if new_md['MDATA_KEY_BPM'] is None:
             del new_md['MDATA_KEY_BPM']
 
+
     if is_airtime_recorded(new_md):
         hour,minute,second,name = new_md['MDATA_KEY_TITLE'].split("-",3)
         new_md['MDATA_KEY_TITLE'] = u'%s-%s-%s:%s:%s' % \
             (name, new_md['MDATA_KEY_YEAR'], hour, minute, second)
     else:
         # Read title from filename if it does not exist
+        default_title = no_extension_basename(original_path)
+        if re.match(".+-%s-.+$" % unicode_unknown, default_title):
+            default_title = u''
         new_md = default_to(dictionary=new_md, keys=['MDATA_KEY_TITLE'],
-                            default=no_extension_basename(original_path))
+                            default=default_title)
 
+    new_md['MDATA_KEY_TITLE'] = re.sub(r'-?%s-?' % unicode_unknown, u'',
+            new_md['MDATA_KEY_TITLE'])
     return new_md
 
 def organized_path(old_path, root_path, orig_md):
diff --git a/python_apps/media-monitor2/tests/test_pure.py b/python_apps/media-monitor2/tests/test_pure.py
index b9f8d646c..5dfaaedc2 100644
--- a/python_apps/media-monitor2/tests/test_pure.py
+++ b/python_apps/media-monitor2/tests/test_pure.py
@@ -80,6 +80,22 @@ class TestMMP(unittest.TestCase):
         # TODO : add a better test than this...
         self.assertTrue( len(opath) > 0 )
 
+    def test_normalized_metadata3(self):
+        """
+        Test the case where the metadata is empty
+        """
+        orig = Metadata.airtime_dict({})
+        paths_unknown_title = [
+                ("/testin/unknown-unknown-unknown.mp3",""),
+                ("/testin/01-unknown-123kbps.mp3",""),
+                ("/testin/02-unknown-140kbps.mp3",""),
+                ("/testin/unknown-unknown-123kbps.mp3",""),
+                ("/testin/unknown-bibimbop-unknown.mp3","bibimbop"),
+        ]
+        for p,res in paths_unknown_title:
+            normalized = mmp.normalized_metadata(orig, p)
+            self.assertEqual( normalized['MDATA_KEY_TITLE'], res)
+
     def test_file_md5(self):
         p = os.path.realpath(__file__)
         m1 = mmp.file_md5(p)