Example #1
0
def main(args=None):
    # Parse command-line arguments
    parser = OptionParser()
    parser.add_option("--database", dest="database", default=None, help=_("Path to F-Spot database"))
    parser.add_option(
        "--log-level",
        dest="log_level",
        default=logging.INFO,
        type="int",
        help=_("Logging level for messages (1:debug 2:info, 3:warning, 4:errors, 5:critical)"),
    )
    # Find
    lookupgrp = OptionGroup(parser, _("Queries"))
    lookupgrp.add_option("--find-path", dest="find_path", default=None, help=_("Find by path"))
    lookupgrp.add_option("--find-tag", dest="find_tag", default=None, help=_("Find by tag"))
    lookupgrp.add_option(
        "--find-missing", dest="find_missing", default=False, action="store_true", help=_("Find photos missing on disk")
    )
    lookupgrp.add_option(
        "--find-corrupted",
        dest="find_corrupted",
        default=False,
        action="store_true",
        help=_("Find corrupted Jpeg photos"),
    )

    # Actions
    actionsgrp = OptionGroup(parser, _("Actions"))
    actionsgrp.add_option("--list", dest="list", default=False, action="store_true", help=_("List photos matching set"))
    actionsgrp.add_option("--rating", dest="rating", default=None, type="int", help=_("Change rating"))
    actionsgrp.add_option(
        "--safe-rating",
        dest="safe_rating",
        default=False,
        action="store_true",
        help=_("Change rating only if superior to current"),
    )
    actionsgrp.add_option("--tag", dest="tag", default=None, help=_("Apply specified tag"))
    actionsgrp.add_option("--untag", dest="untag", default=None, help=_("Remove specified tag"))
    parser.add_option_group(lookupgrp)
    parser.add_option_group(actionsgrp)
    (options, args) = parser.parse_args(args)

    logging.basicConfig(level=options.log_level)

    # Start using the controller
    fm = FSpotController(dbpath=options.database)
    logger.info(_("F-Spot version  : %s") % fm.fspot_version)
    logger.debug(_("F-Spot database : %s") % fm.db_version)

    # Chain find queries
    if options.find_path:
        path = unicode(options.find_path, "utf8")
        fm.photoset = fm.find_by_path(path)
    if options.find_tag:
        tagname = unicode(options.find_tag, "utf8")
        fm.photoset = fm.find_by_tag(tagname)
    if options.find_missing:
        fm.photoset = fm.find_missing_on_disk()
    if options.find_corrupted:
        fm.photoset = fm.find_corrupted()

    if options.rating:
        fm.change_rating(options.rating, options.safe_rating)
    if options.tag:
        fm.apply_tag(options.tag)
    if options.untag:
        fm.remove_tag(options.untag)

    # List photoset in stdout
    if options.list:
        # Force UTF-8 encoding of stdout
        # sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
        logger.debug(_("Default locale: %s") % locale.getdefaultlocale()[1])
        logger.debug(_("Terminal encoding (stdout): %s") % sys.stdout.encoding)
        for p in fm.photoset:
            print p.path

    if not any([options.list, options.rating, options.tag, options.untag]):
        logger.warning(_("No action was specified."))
    return 0
Example #2
0
class TestController(DataTestCase, unittest.TestCase):
    fixture = dbfixture
    datasets = [PhotoData, MetaData]

    def setUp(self):
        super(TestController, self).setUp()
        self.fm = FSpotController(dbpath=DB_PATH, 
                                  engine=engine,
                                  backup=False)

    def test_normalize(self):
        pass

    def test_find_by_path(self):
        self.fm.normalize = False
        p = self.fm.find_by_path("tests").all()
        self.assertFalse(len(p) > 0)
        p = self.fm.find_by_path("*tests*").all()
        self.assertTrue(len(p) > 0)
        p = self.fm.find_by_path(BASE_PATH+'*').all()
        self.assertTrue(len(p) > 0)
        p = self.fm.find_by_path('*tests/bee*').all()
        self.assertFalse(len(p) > 0)
        p = self.fm.find_by_path('*file*').all()
        self.assertEqual(len(p), 4)
        p = self.fm.find_by_path('*file?.jpg').all()
        self.assertEqual(len(p), 2)
        p = self.fm.find_by_path('*spa ce*').all()
        self.assertEqual(len(p), 1)
        p = self.fm.find_by_path(u'*Ω*'.encode('utf-8')).all()
        self.assertEqual(len(p), 1)
        # Now normalize the paths
        self.fm.normalize = True
        p = self.fm.find_by_path('*tests/bee*').all()
        self.assertTrue(len(p) > 0)
        p = self.fm.find_by_path('*spa ce*').all()
        self.assertEqual(len(p), 2)
        p = self.fm.find_by_path(u'*Ω*'.encode('utf-8')).all()
        self.assertEqual(len(p), 2)

    def test_change_rating(self):
        p = session.query(Photo).filter_by(filename='bee.jpg').one()
        assert p
        self.assertEqual(0, p.rating)
        self.fm.change_rating(1)
        self.assertEqual(1, p.rating)
        # Safe
        self.fm.change_rating(0, safe=True)
        self.assertEqual(1, p.rating)
        # Not safe
        self.fm.change_rating(0, safe=False)
        self.assertEqual(0, p.rating)

    def test_find_missing_on_disk(self):
        n = self.fm.photoset.count()
        p = self.fm.find_missing_on_disk().all()
        self.assertEqual(len(p), n - 1)