Example #1
0
 def test_set_dest_dir(self):
     from pygall.lib.imageprocessing import ImageProcessing
     ip = ImageProcessing()
     ip.set_dest_dir('/foo/bar')
     self.assertEqual(ip.dest_dir, '/foo/bar')
     self.assertEqual(ip.abs_orig_dest_dir,  '/foo/bar/orig')
     self.assertEqual(ip.abs_scaled_dest_dir, '/foo/bar/scaled')
Example #2
0
 def test_set_dest_dir_None(self):
     from pygall.lib.imageprocessing import ImageProcessing
     ip = ImageProcessing()
     ip.set_dest_dir(None)
     self.assertTrue(ip.dest_dir is None)
     self.assertTrue(ip.abs_orig_dest_dir is None)
     self.assertTrue(ip.abs_scaled_dest_dir is None)
Example #3
0
def main():
    global IP, OPTIONS
    args, OPTIONS = get_options(sys.argv)

    ini_file = args[0]
    app = get_app(ini_file, "PyGall")
    settings = app.registry.settings
    OPTIONS['photos_dir'] = settings['photos_dir']
    IP = ImageProcessing(settings['photos_dir'])

    # configure engine for fspot database
    FS_initialize_sql(create_engine("sqlite:///%s" % OPTIONS['fspot-db']))

    if OPTIONS['drop-db']:
        Base.metadata.drop_all()
        print "All tables has been dropped"
    Base.metadata.create_all(checkfirst=True)

    if OPTIONS['cleanup-files'] and os.path.exists(settings['photos_dir']):
        print "Photos dir %s has been cleaned up" % settings['photos_dir']
        shutil.rmtree(settings['photos_dir'])
    
    fs_ids = []
    msgs = []
    for row in FS_DBSession.query(FS_Tag)\
            .filter_by(name=OPTIONS['fspot-exporttag']).one().photos:
        # process the photo and appends fspot_id to the list of processed
        # fspot photos
        fs_ids.append(process(row, msgs))
        sys.stdout.write('.')
        sys.stdout.flush()

    # remove photos coming from fspot that are not associated with tag pygall
    # anymore
    for photo in DBSession.query(Photo).filter(and_(
        Photo.fspot_id!=None, not_(Photo.fspot_id.in_(fs_ids)))).all():
        IP.remove_image(photo.uri)
        DBSession.delete(photo)
        transaction.commit()
        msgs.append("Photo %s has been deleted from PyGall" % photo.uri)
        sys.stdout.write('.')
        sys.stdout.flush()

    print ''
    if len(msgs) > 0:
        for msg in msgs:
            print msg
    else:
        print "Nothing to do..."
Example #4
0
 def setUp(self):
     from tempfile import mkdtemp
     from pygall.lib.imageprocessing import ImageProcessing
     self.destdir = mkdtemp(prefix='test_ip_')
     self.ip = ImageProcessing(
             dest_dir=self.destdir,
             crop_dimension=700,
             crop_quality=80)
Example #5
0
class IPTests(TestCase):

    def setUp(self):
        from tempfile import mkdtemp
        from pygall.lib.imageprocessing import ImageProcessing
        self.destdir = mkdtemp(prefix='test_ip_')
        self.ip = ImageProcessing(
                dest_dir=self.destdir,
                crop_dimension=700,
                crop_quality=80)

    def tearDown(self):
        import shutil
        shutil.rmtree(self.destdir)

    def test_copy_orig_already_exists(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'orig', uri)
        os.makedirs(os.path.dirname(dest), 0755)
        open(dest, 'w').close() # create a dest file
        self.assertFalse(self.ip.copy_orig('python.jpg', uri))

    def test_copy_orig(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'orig', uri)
        self.ip.copy_orig('python.jpg', uri)
        self.assertTrue(os.path.exists(dest))
        with open(dest, 'rb') as f:
            self.assertEqual(
                    hashlib.md5(f.read()).hexdigest(),
                    '4c6f6da9406bce4ae220d265aa70f9d9')

    def test_copy_orig_fileobj(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'orig', uri)
        with open('python.jpg', 'rb') as src:
            loc = src.tell()
            self.ip.copy_orig(src, uri)
            self.assertTrue(os.path.exists(dest))
            with open(dest, 'rb') as f:
                self.assertEqual(
                        hashlib.md5(f.read()).hexdigest(),
                        '4c6f6da9406bce4ae220d265aa70f9d9')
            self.assertEqual(src.tell(), loc)

    def test_copy_scaled_already_exists(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'scaled', uri)
        os.makedirs(os.path.dirname(dest), 0755)
        open(dest, 'w').close() # create a dest file
        self.assertFalse(self.ip.copy_scaled('python.jpg', uri))

    def test_copy_scaled(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'scaled', uri)
        self.ip.copy_scaled('python.jpg', uri)
        self.assertTrue(os.path.exists(dest))
        with open(dest, 'rb') as f:
            self.assertEqual(
                    hashlib.md5(f.read()).hexdigest(),
                    'af18318b71016ea25e2c79c63810482a')

    def test_copy_scaled_with_rotation(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'scaled', uri)
        from mock import patch
        with patch('pygall.lib.imageprocessing.get_exif') as mock_get_exif:
            mock_get_exif.return_value = {'Orientation': 6}
            self.ip.copy_scaled('python.jpg', uri)
            self.assertTrue(os.path.exists(dest))
            with open(dest, 'rb') as f:
                self.assertEqual(
                        hashlib.md5(f.read()).hexdigest(),
                        'aa72c5f0442e45f413102b5dc022141b')
            self.assertEqual(mock_get_exif.called, 1)
            os.unlink(dest)

            # test that if an exception occurs, there is no rotation
            mock_get_exif.side_effect = Exception()
            self.ip.copy_scaled('python.jpg', uri)
            with open(dest, 'rb') as f:
                self.assertEqual(
                        hashlib.md5(f.read()).hexdigest(),
                        'af18318b71016ea25e2c79c63810482a')

    def test_copy_scaled_fileobj(self):
        import os
        import hashlib
        uri = 'test/python.jpg'
        dest = os.path.join(self.destdir, 'scaled', uri)
        with open('python.jpg', 'rb') as src:
            loc = src.tell()
            self.ip.copy_scaled(src, uri)
            self.assertTrue(os.path.exists(dest))
            with open(dest, 'rb') as f:
                self.assertEqual(
                        hashlib.md5(f.read()).hexdigest(),
                        'af18318b71016ea25e2c79c63810482a')
            self.assertEqual(src.tell(), loc)

    def test_remove_image(self):
        import os
        import hashlib
        uri1 = 'path/to/first/python.jpg'
        uri2 = 'path/to/second/python.jpg'
        origdir = os.path.join(self.destdir, 'orig')
        scaleddir = os.path.join(self.destdir, 'scaled')
        pathlens = []

        # create files
        for uri in (uri1, uri2,):
            dest_orig = os.path.join(origdir, uri)
            dest_scaled = os.path.join(scaleddir, uri)
            os.makedirs(os.path.dirname(dest_orig), 0755)
            os.makedirs(os.path.dirname(dest_scaled), 0755)
            open(dest_orig, 'w').close() # create a dest_orig file
            open(dest_scaled, 'w').close() # create a dest_scaled file
            self.assertTrue(os.path.exists(dest_orig))
            self.assertTrue(os.path.exists(dest_scaled))

        # test remove_image
        for uri in (uri1, uri2,):
            dest_orig = os.path.join(origdir, uri)
            dest_scaled = os.path.join(scaleddir, uri)
            self.ip.remove_image(uri)
            self.assertFalse(os.path.exists(dest_orig))
            self.assertFalse(os.path.exists(dest_scaled))
            for p in (dest_orig, dest_scaled,):
                base = os.path.dirname(p)
                i = 0
                while not os.path.exists(base):
                    i += 1;
                    base = os.path.dirname(base)
                pathlens.append(i)
        self.assertEqual(pathlens, [1, 1, 3, 3])
        self.assertTrue(os.path.exists(origdir))
        self.assertEqual(os.listdir(origdir), [])
        self.assertTrue(os.path.exists(scaleddir))
        self.assertEqual(os.listdir(scaleddir), [])

        # test removal does not crash if dest images don't exist
        self.ip.remove_image(uri)
        self.assertFalse(os.path.exists(dest_orig))
        self.assertFalse(os.path.exists(dest_scaled))

    def test_process_image(self):
        from contextlib import nested
        from datetime import datetime
        from pygall.lib.imageprocessing import ImageProcessing
        from mock import patch
        src = 'python.jpg'
        with nested(
                patch('pygall.lib.imageprocessing.get_info'),
                patch.object(ImageProcessing, 'copy_orig'),
                patch.object(ImageProcessing, 'copy_scaled'),
                patch.object(ImageProcessing, 'remove_image'),
                ) as (mock_get_info, mock_copy_orig, mock_copy_scaled,
                        mock_remove_image):
            mock_get_info.return_value = {
                    'md5sum': 'the_md5',
                    'date': datetime(2002, 02, 22),
                    'ext': 'jpeg',
                    }
            info = self.ip.process_image(src, md5sum='dummy_md5sum')
            uri = info['uri']
            self.assertEqual(uri, '2002/02/22/the_md5.jpeg')
            mock_copy_orig.assert_called_once_with(src, uri)
            mock_copy_scaled.assert_called_once_with(src, uri)
            # test if an exception is thrown
            mock_copy_scaled.side_effect = Exception()
            self.assertRaises(Exception, self.ip.process_image, src)
            mock_remove_image.assert_called_once_with(uri)