예제 #1
0
    def handle(self, sender, event):
        """
        Intercept events where a new file has been added to the organize
        directory and place it in the correct path (starting with
        self.target_path)
        """
        # Only handle this event type
        assert isinstance(event, OrganizeFile), \
            "Organizer can only handle OrganizeFile events.Given '%s'" % event
        try:
            # We must select the target_path based on whether file was recorded
            # by airtime or not.
            # Do we need to "massage" the path using mmp.organized_path?
            target_path = self.recorded_path if event.metadata.is_recorded() \
                                             else self.target_path
            # nasty hack do this properly
            owner_id = mmp.owner_id(event.path)
            if owner_id != -1:
                target_path = os.path.join(target_path, unicode(owner_id))

            mdata = event.metadata.extract()
            new_path = mmp.organized_path(event.path, target_path, mdata)

            # See hack in mmp.magic_move
            def new_dir_watch(d):
                # TODO : rewrite as return lambda : dispatcher.send(...
                def cb():
                    dispatcher.send(signal="add_subwatch", sender=self,
                            directory=d)
                return cb

            mmp.magic_move(event.path, new_path,
                    after_dir_make=new_dir_watch(dirname(new_path)))

            # The reason we need to go around saving the owner in this ass
            # backwards way is bewcause we are unable to encode the owner id
            # into the file itself so that the StoreWatchListener listener can
            # detect it from the file
            owners.add_file_owner(new_path, owner_id )

            self.logger.info('Organized: "%s" into "%s"' %
                    (event.path, new_path))
        except BadSongFile as e:
            self.report_problem_file(event=event, exception=e)
        # probably general error in mmp.magic.move...
        except Exception as e:
            self.unexpected_exception( e )
            self.report_problem_file(event=event, exception=e)
예제 #2
0
 def test_has_owner(self):
     owners.reset_owners()
     o = 12345
     self.assertTrue(owners.add_file_owner(self.f, o))
     self.assertTrue(owners.has_owner(self.f))
예제 #3
0
 def test_remove_file_owner(self):
     owners.reset_owners()
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertTrue(owners.remove_file_owner(self.f))
     self.assertFalse(owners.remove_file_owner(self.f))
예제 #4
0
 def test_get_owner(self):
     owners.reset_owners()
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertEqual(owners.get_owner(self.f), 123, "file is owned")
     self.assertEqual(owners.get_owner("random_stuff.txt"), -1, "file is not owned")
예제 #5
0
 def test_add_file_owner(self):
     owners.reset_owners()
     self.assertFalse(owners.add_file_owner("testing", -1))
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertTrue(owners.add_file_owner(self.f, 456))
예제 #6
0
 def test_has_owner(self):
     owners.reset_owners()
     o = 12345
     self.assertTrue(owners.add_file_owner(self.f, o))
     self.assertTrue(owners.has_owner(self.f))
예제 #7
0
 def test_get_owner(self):
     owners.reset_owners()
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertEqual(owners.get_owner(self.f), 123, "file is owned")
     self.assertEqual(owners.get_owner("random_stuff.txt"), -1,
                      "file is not owned")
예제 #8
0
 def test_remove_file_owner(self):
     owners.reset_owners()
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertTrue(owners.remove_file_owner(self.f))
     self.assertFalse(owners.remove_file_owner(self.f))
예제 #9
0
 def test_add_file_owner(self):
     owners.reset_owners()
     self.assertFalse(owners.add_file_owner('testing', -1))
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertTrue(owners.add_file_owner(self.f, 123))
     self.assertTrue(owners.add_file_owner(self.f, 456))