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
|
|
|
#
|
|
|
|
# This software is licensed under the "HS" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.hardcoded.net/licenses/hs_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-03-01 11:20:21 +00:00
|
|
|
from lxml import etree
|
2009-12-15 16:23:02 +00:00
|
|
|
from appscript import app, k, CommandError
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-10-24 12:21:39 +00:00
|
|
|
from hsutil import io
|
2009-06-01 09:55:11 +00:00
|
|
|
from hsutil.str import get_file_ext
|
|
|
|
from hsutil.path import Path
|
2010-07-13 06:08:18 +00:00
|
|
|
from hscommon.cocoa import as_fetch
|
|
|
|
from hscommon.cocoa.objcmin import NSUserDefaults, NSURL
|
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
|
|
|
|
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),
|
|
|
|
})
|
2009-10-24 13:54:57 +00:00
|
|
|
HANDLED_EXTS = set(['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-02-04 12:13:08 +00:00
|
|
|
self.dimensions = _block_osx.get_image_size(unicode(self.path))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def get_blocks(self, block_count_per_side):
|
|
|
|
try:
|
2010-02-04 12:13:08 +00:00
|
|
|
blocks = _block_osx.getblocks(unicode(self.path), block_count_per_side)
|
2009-10-24 12:21:39 +00:00
|
|
|
except Exception as e:
|
2009-06-01 09:55:11 +00:00
|
|
|
raise IOError('The reading of "%s" failed with "%s"' % (unicode(self.path), unicode(e)))
|
|
|
|
if not blocks:
|
|
|
|
raise IOError('The picture %s could not be read' % unicode(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-03-01 11:20:21 +00:00
|
|
|
# We make the xml go through lxml so that it can fix broken xml which iPhoto sometimes produces.
|
|
|
|
parser = etree.XMLParser(recover=True)
|
|
|
|
root = etree.parse(io.open(plistpath), parser=parser).getroot()
|
|
|
|
s = etree.tostring(root)
|
2009-10-24 12:21:39 +00:00
|
|
|
plist = plistlib.readPlistFromString(s)
|
|
|
|
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):
|
|
|
|
if from_path == Path('iPhoto Library'):
|
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):
|
|
|
|
if path == Path('iPhoto Library'):
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return directories.Directories.get_subfolders(path)
|
|
|
|
|
|
|
|
def add_path(self, path):
|
|
|
|
if path == Path('iPhoto Library'):
|
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
|
|
|
|
|
|
|
|
|
|
|
class DupeGuruPE(app_cocoa.DupeGuru):
|
|
|
|
def __init__(self):
|
2009-09-29 14:07:50 +00:00
|
|
|
app_cocoa.DupeGuru.__init__(self, data, 'dupeGuru Picture Edition', appid=5)
|
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
|
|
|
|
|
|
|
def _do_delete(self, j):
|
|
|
|
def op(dupe):
|
|
|
|
j.add_progress()
|
|
|
|
return self._do_delete_dupe(dupe)
|
|
|
|
|
|
|
|
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):
|
2009-06-06 13:49:45 +00:00
|
|
|
j = j.start_subjob([6, 4], "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:
|
|
|
|
self.path2iphoto[unicode(photo.image_path(timeout=0))] = photo
|
|
|
|
except CommandError:
|
|
|
|
pass
|
|
|
|
except (CommandError, RuntimeError):
|
|
|
|
pass
|
2009-06-06 13:49:45 +00:00
|
|
|
j.start_job(self.results.mark_count, "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
|
|
|
|
|
|
|
|
def _do_delete_dupe(self, dupe):
|
|
|
|
if isinstance(dupe, IPhoto):
|
2009-06-06 13:49:45 +00:00
|
|
|
if unicode(dupe.path) in self.path2iphoto:
|
|
|
|
photo = self.path2iphoto[unicode(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:
|
|
|
|
raise EnvironmentError(unicode(e))
|
2009-06-06 13:49:45 +00:00
|
|
|
else:
|
2010-04-12 10:21:01 +00:00
|
|
|
msg = u"Could not find photo %s in iPhoto Library" % unicode(dupe.path)
|
|
|
|
raise EnvironmentError(msg)
|
2009-06-01 09:55:11 +00:00
|
|
|
else:
|
2010-04-12 10:21:01 +00:00
|
|
|
app_cocoa.DupeGuru._do_delete_dupe(self, dupe)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def _do_load(self, j):
|
2009-06-18 18:37:53 +00:00
|
|
|
self.directories.load_from_file(op.join(self.appdata, 'last_directories.xml'))
|
2009-06-01 09:55:11 +00:00
|
|
|
self.results.load_from_xml(op.join(self.appdata, 'last_results.xml'), self._get_file, j)
|
|
|
|
|
|
|
|
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
|
|
|
|
|