2009-06-18 19:42:27 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/11/13
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
2009-06-18 19:42:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
import os.path as op
|
|
|
|
import plistlib
|
2010-08-15 12:42:55 +00:00
|
|
|
import logging
|
|
|
|
import re
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-12-31 11:10:44 +00:00
|
|
|
from appscript import app, k, CommandError, ApplicationNotFoundError
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-11 10:59:53 +00:00
|
|
|
from hscommon import io
|
2011-01-11 12:36:05 +00:00
|
|
|
from hscommon.util import get_file_ext, remove_invalid_xml
|
2011-01-11 10:59:53 +00:00
|
|
|
from hscommon.path import Path
|
2010-07-13 06:08:18 +00:00
|
|
|
from hscommon.cocoa import as_fetch
|
|
|
|
from hscommon.cocoa.objcmin import NSUserDefaults, NSURL
|
2011-01-18 16:33:33 +00:00
|
|
|
from hscommon.trans import tr
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-12-30 10:37:57 +00:00
|
|
|
from core import fs
|
|
|
|
from core import app_cocoa, directories
|
2010-02-04 12:13:08 +00:00
|
|
|
from . import data, _block_osx
|
2009-10-18 09:26:04 +00:00
|
|
|
from .scanner import ScannerPE
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-12-31 11:10:44 +00:00
|
|
|
IPHOTO_PATH = Path('iPhoto Library')
|
|
|
|
|
2009-10-24 12:21:39 +00:00
|
|
|
class Photo(fs.File):
|
|
|
|
INITIAL_INFO = fs.File.INITIAL_INFO.copy()
|
2009-09-28 15:31:54 +00:00
|
|
|
INITIAL_INFO.update({
|
|
|
|
'dimensions': (0,0),
|
|
|
|
})
|
2010-12-31 11:10:44 +00:00
|
|
|
HANDLED_EXTS = {'png', 'jpg', 'jpeg', 'gif', 'psd', 'bmp', 'tiff', 'tif', 'nef', 'cr2'}
|
2009-10-24 12:21:39 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def can_handle(cls, path):
|
|
|
|
return fs.File.can_handle(path) and get_file_ext(path[-1]) in cls.HANDLED_EXTS
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-09-28 15:31:54 +00:00
|
|
|
def _read_info(self, field):
|
2009-10-24 12:21:39 +00:00
|
|
|
fs.File._read_info(self, field)
|
2009-09-28 15:31:54 +00:00
|
|
|
if field == 'dimensions':
|
2010-08-11 14:39:06 +00:00
|
|
|
self.dimensions = _block_osx.get_image_size(str(self.path))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def get_blocks(self, block_count_per_side):
|
|
|
|
try:
|
2010-08-11 14:39:06 +00:00
|
|
|
blocks = _block_osx.getblocks(str(self.path), block_count_per_side)
|
2009-10-24 12:21:39 +00:00
|
|
|
except Exception as e:
|
2010-08-11 14:39:06 +00:00
|
|
|
raise IOError('The reading of "%s" failed with "%s"' % (str(self.path), str(e)))
|
2009-06-01 09:55:11 +00:00
|
|
|
if not blocks:
|
2010-08-11 14:39:06 +00:00
|
|
|
raise IOError('The picture %s could not be read' % str(self.path))
|
2010-02-04 12:13:08 +00:00
|
|
|
return blocks
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IPhoto(Photo):
|
|
|
|
@property
|
|
|
|
def display_path(self):
|
2009-10-24 12:21:39 +00:00
|
|
|
return Path(('iPhoto Library', self.name))
|
|
|
|
|
|
|
|
def get_iphoto_database_path():
|
|
|
|
ud = NSUserDefaults.standardUserDefaults()
|
|
|
|
prefs = ud.persistentDomainForName_('com.apple.iApps')
|
2009-12-16 15:51:26 +00:00
|
|
|
if prefs is None:
|
|
|
|
raise directories.InvalidPathError()
|
2009-10-24 12:21:39 +00:00
|
|
|
if 'iPhotoRecentDatabases' not in prefs:
|
|
|
|
raise directories.InvalidPathError()
|
|
|
|
plisturl = NSURL.URLWithString_(prefs['iPhotoRecentDatabases'][0])
|
|
|
|
return Path(plisturl.path())
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-10-24 12:21:39 +00:00
|
|
|
def get_iphoto_pictures(plistpath):
|
|
|
|
if not io.exists(plistpath):
|
2010-01-14 15:33:27 +00:00
|
|
|
return []
|
2010-08-15 12:42:55 +00:00
|
|
|
s = io.open(plistpath, 'rt', encoding='utf-8').read()
|
|
|
|
# There was a case where a guy had 0x10 chars in his plist, causing expat errors on loading
|
|
|
|
s = remove_invalid_xml(s, replace_with='')
|
|
|
|
# It seems that iPhoto sometimes doesn't properly escape & chars. The regexp below is to find
|
|
|
|
# any & char that is not a &-based entity (&, ", etc.). based on TextMate's XML
|
|
|
|
# bundle's regexp
|
|
|
|
s, count = re.subn(r'&(?![a-zA-Z0-9_-]+|#[0-9]+|#x[0-9a-fA-F]+;)', '', s)
|
|
|
|
if count:
|
|
|
|
logging.warning("%d invalid XML entities replacement made", count)
|
|
|
|
plist = plistlib.readPlistFromBytes(s.encode('utf-8'))
|
2009-10-24 12:21:39 +00:00
|
|
|
result = []
|
|
|
|
for photo_data in plist['Master Image List'].values():
|
2009-06-01 09:55:11 +00:00
|
|
|
if photo_data['MediaType'] != 'Image':
|
2009-10-24 12:21:39 +00:00
|
|
|
continue
|
2009-06-01 09:55:11 +00:00
|
|
|
photo_path = Path(photo_data['ImagePath'])
|
2009-10-24 12:21:39 +00:00
|
|
|
photo = IPhoto(photo_path)
|
|
|
|
result.append(photo)
|
|
|
|
return result
|
|
|
|
|
|
|
|
class Directories(directories.Directories):
|
|
|
|
def __init__(self):
|
|
|
|
directories.Directories.__init__(self, fileclasses=[Photo])
|
2009-12-16 15:51:26 +00:00
|
|
|
try:
|
|
|
|
self.iphoto_libpath = get_iphoto_database_path()
|
|
|
|
self.set_state(self.iphoto_libpath[:-1], directories.STATE_EXCLUDED)
|
|
|
|
except directories.InvalidPathError:
|
|
|
|
self.iphoto_libpath = None
|
2009-10-24 12:21:39 +00:00
|
|
|
|
|
|
|
def _get_files(self, from_path):
|
2010-12-31 11:10:44 +00:00
|
|
|
if from_path == IPHOTO_PATH:
|
2009-12-16 15:51:26 +00:00
|
|
|
if self.iphoto_libpath is None:
|
|
|
|
return []
|
2009-10-24 12:21:39 +00:00
|
|
|
is_ref = self.get_state(from_path) == directories.STATE_REFERENCE
|
|
|
|
photos = get_iphoto_pictures(self.iphoto_libpath)
|
|
|
|
for photo in photos:
|
|
|
|
photo.is_ref = is_ref
|
|
|
|
return photos
|
|
|
|
else:
|
|
|
|
return directories.Directories._get_files(self, from_path)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_subfolders(path):
|
2010-12-31 11:10:44 +00:00
|
|
|
if path == IPHOTO_PATH:
|
2009-10-24 12:21:39 +00:00
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return directories.Directories.get_subfolders(path)
|
|
|
|
|
|
|
|
def add_path(self, path):
|
2010-12-31 11:10:44 +00:00
|
|
|
if path == IPHOTO_PATH:
|
2010-01-14 15:33:27 +00:00
|
|
|
if path not in self:
|
|
|
|
self._dirs.append(path)
|
2009-10-24 12:21:39 +00:00
|
|
|
else:
|
|
|
|
directories.Directories.add_path(self, path)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-12-31 11:10:44 +00:00
|
|
|
def has_iphoto_path(self):
|
|
|
|
return any(path == IPHOTO_PATH for path in self._dirs)
|
|
|
|
|
2010-08-15 13:07:44 +00:00
|
|
|
def has_any_file(self):
|
|
|
|
# If we don't do that, it causes a hangup in the GUI when we click Start Scanning because
|
|
|
|
# checking if there's any file to scan involves reading the whole library. If we have the
|
|
|
|
# iPhoto library, we assume we have at least one file.
|
2010-12-31 11:10:44 +00:00
|
|
|
if self.has_iphoto_path():
|
2010-08-15 13:07:44 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return directories.Directories.has_any_file(self)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
class DupeGuruPE(app_cocoa.DupeGuru):
|
|
|
|
def __init__(self):
|
2010-09-29 14:49:50 +00:00
|
|
|
app_cocoa.DupeGuru.__init__(self, data, 'dupeGuru Picture Edition')
|
2009-10-18 09:26:04 +00:00
|
|
|
self.scanner = ScannerPE()
|
2009-10-24 12:21:39 +00:00
|
|
|
self.directories = Directories()
|
2010-01-14 15:14:26 +00:00
|
|
|
self.scanner.cache_path = op.join(self.appdata, 'cached_pictures.db')
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-09-25 13:37:18 +00:00
|
|
|
def _do_delete(self, j, replace_with_hardlinks):
|
2009-06-01 09:55:11 +00:00
|
|
|
def op(dupe):
|
|
|
|
j.add_progress()
|
2010-09-25 13:37:18 +00:00
|
|
|
return self._do_delete_dupe(dupe, replace_with_hardlinks)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
marked = [dupe for dupe in self.results.dupes if self.results.is_marked(dupe)]
|
|
|
|
self.path2iphoto = {}
|
|
|
|
if any(isinstance(dupe, IPhoto) for dupe in marked):
|
2011-01-18 16:33:33 +00:00
|
|
|
j = j.start_subjob([6, 4], tr("Probing iPhoto. Don't touch it during the operation!"))
|
2009-12-15 16:23:02 +00:00
|
|
|
try:
|
|
|
|
a = app('iPhoto')
|
|
|
|
a.activate(timeout=0)
|
|
|
|
a.select(a.photo_library_album(timeout=0), timeout=0)
|
|
|
|
photos = as_fetch(a.photo_library_album().photos, k.item)
|
|
|
|
for photo in j.iter_with_progress(photos):
|
|
|
|
try:
|
2010-08-11 14:39:06 +00:00
|
|
|
self.path2iphoto[str(photo.image_path(timeout=0))] = photo
|
2009-12-15 16:23:02 +00:00
|
|
|
except CommandError:
|
|
|
|
pass
|
2010-12-31 11:10:44 +00:00
|
|
|
except (CommandError, RuntimeError, ApplicationNotFoundError):
|
2009-12-15 16:23:02 +00:00
|
|
|
pass
|
2011-01-18 16:33:33 +00:00
|
|
|
j.start_job(self.results.mark_count, tr("Sending dupes to the Trash"))
|
2010-04-12 10:21:01 +00:00
|
|
|
self.results.perform_on_marked(op, True)
|
2009-06-01 09:55:11 +00:00
|
|
|
del self.path2iphoto
|
|
|
|
|
2010-09-25 13:37:18 +00:00
|
|
|
def _do_delete_dupe(self, dupe, replace_with_hardlinks):
|
2009-06-01 09:55:11 +00:00
|
|
|
if isinstance(dupe, IPhoto):
|
2010-08-11 14:39:06 +00:00
|
|
|
if str(dupe.path) in self.path2iphoto:
|
|
|
|
photo = self.path2iphoto[str(dupe.path)]
|
2009-12-15 16:23:02 +00:00
|
|
|
try:
|
|
|
|
a = app('iPhoto')
|
|
|
|
a.remove(photo, timeout=0)
|
2010-04-12 10:21:01 +00:00
|
|
|
except (CommandError, RuntimeError) as e:
|
2010-08-11 14:39:06 +00:00
|
|
|
raise EnvironmentError(str(e))
|
2009-06-06 13:49:45 +00:00
|
|
|
else:
|
2010-08-11 14:39:06 +00:00
|
|
|
msg = "Could not find photo %s in iPhoto Library" % str(dupe.path)
|
2010-04-12 10:21:01 +00:00
|
|
|
raise EnvironmentError(msg)
|
2009-06-01 09:55:11 +00:00
|
|
|
else:
|
2010-09-25 13:37:18 +00:00
|
|
|
app_cocoa.DupeGuru._do_delete_dupe(self, dupe, replace_with_hardlinks)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def _get_file(self, str_path):
|
|
|
|
p = Path(str_path)
|
2009-12-16 16:16:22 +00:00
|
|
|
if (self.directories.iphoto_libpath is not None) and (p in self.directories.iphoto_libpath[:-1]):
|
2009-10-24 12:21:39 +00:00
|
|
|
return IPhoto(p)
|
|
|
|
return app_cocoa.DupeGuru._get_file(self, str_path)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-06-07 07:15:56 +00:00
|
|
|
def copy_or_move(self, dupe, copy, destination, dest_type):
|
2009-06-01 09:55:11 +00:00
|
|
|
if isinstance(dupe, IPhoto):
|
|
|
|
copy = True
|
2009-06-07 07:15:56 +00:00
|
|
|
return app_cocoa.DupeGuru.copy_or_move(self, dupe, copy, destination, dest_type)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def selected_dupe_path(self):
|
|
|
|
if not self.selected_dupes:
|
|
|
|
return None
|
|
|
|
return self.selected_dupes[0].path
|
|
|
|
|
|
|
|
def selected_dupe_ref_path(self):
|
|
|
|
if not self.selected_dupes:
|
|
|
|
return None
|
|
|
|
ref = self.results.get_group_of_duplicate(self.selected_dupes[0]).ref
|
2009-10-25 11:46:26 +00:00
|
|
|
if ref is self.selected_dupes[0]: # we don't want the same pic to be displayed on both sides
|
|
|
|
return None
|
2009-06-01 09:55:11 +00:00
|
|
|
return ref.path
|
|
|
|
|
2010-12-31 11:10:44 +00:00
|
|
|
def start_scanning(self):
|
|
|
|
result = app_cocoa.DupeGuru.start_scanning(self)
|
|
|
|
if self.directories.has_iphoto_path():
|
|
|
|
try:
|
|
|
|
app('iPhoto')
|
|
|
|
except ApplicationNotFoundError:
|
|
|
|
return 4
|
|
|
|
return result
|
|
|
|
|