sgpe cocoa: adjusted to hsfs removal.

--HG--
extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%40210
This commit is contained in:
hsoft 2009-10-24 12:21:39 +00:00
parent b8c11b5aae
commit 25dadc83eb
2 changed files with 71 additions and 102 deletions

View File

@ -12,7 +12,6 @@ from dupeguru_pe import app_cocoa as app_pe_cocoa
# Fix py2app imports which chokes on relative imports # Fix py2app imports which chokes on relative imports
from dupeguru import app, app_cocoa, data, directories, engine, export, ignore, results, scanner from dupeguru import app, app_cocoa, data, directories, engine, export, ignore, results, scanner
from dupeguru_pe import block, cache, matchbase, data from dupeguru_pe import block, cache, matchbase, data
from hsfs import auto, stats, tree
from hsutil import conflict from hsutil import conflict
class PyApp(NSObject): class PyApp(NSObject):

View File

@ -7,25 +7,21 @@
# which should be included with this package. The terms are also available at # which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/hs_license # http://www.hardcoded.net/licenses/hs_license
import os
import os.path as op import os.path as op
import logging import logging
import plistlib import plistlib
import re import re
import objc
from Foundation import * from Foundation import *
from AppKit import * from AppKit import *
from appscript import app, k from appscript import app, k
from hsutil import job, io from hsutil import io
import hsfs as fs
from hsfs import phys, InvalidPath
from hsutil import files
from hsutil.str import get_file_ext from hsutil.str import get_file_ext
from hsutil.path import Path from hsutil.path import Path
from hsutil.cocoa import as_fetch from hsutil.cocoa import as_fetch
from dupeguru import fs
from dupeguru import app_cocoa, directories from dupeguru import app_cocoa, directories
from . import data from . import data
from .cache import string_to_colors, Cache from .cache import string_to_colors, Cache
@ -35,14 +31,19 @@ mainBundle = NSBundle.mainBundle()
PictureBlocks = mainBundle.classNamed_('PictureBlocks') PictureBlocks = mainBundle.classNamed_('PictureBlocks')
assert PictureBlocks is not None assert PictureBlocks is not None
class Photo(phys.File): class Photo(fs.File):
INITIAL_INFO = phys.File.INITIAL_INFO.copy() INITIAL_INFO = fs.File.INITIAL_INFO.copy()
INITIAL_INFO.update({ INITIAL_INFO.update({
'dimensions': (0,0), 'dimensions': (0,0),
}) })
HANDLED_EXTS = set(['png', 'jpg', 'jpeg', 'gif', 'psd', 'bmp', 'tiff', 'nef', 'cr2'])
@classmethod
def can_handle(cls, path):
return fs.File.can_handle(path) and get_file_ext(path[-1]) in cls.HANDLED_EXTS
def _read_info(self, field): def _read_info(self, field):
super(Photo, self)._read_info(field) fs.File._read_info(self, field)
if field == 'dimensions': if field == 'dimensions':
size = PictureBlocks.getImageSize_(unicode(self.path)) size = PictureBlocks.getImageSize_(unicode(self.path))
self.dimensions = (size.width, size.height) self.dimensions = (size.width, size.height)
@ -50,7 +51,7 @@ class Photo(phys.File):
def get_blocks(self, block_count_per_side): def get_blocks(self, block_count_per_side):
try: try:
blocks = PictureBlocks.getBlocksFromImagePath_blockCount_(unicode(self.path), block_count_per_side) blocks = PictureBlocks.getBlocksFromImagePath_blockCount_(unicode(self.path), block_count_per_side)
except Exception, e: except Exception as e:
raise IOError('The reading of "%s" failed with "%s"' % (unicode(self.path), unicode(e))) raise IOError('The reading of "%s" failed with "%s"' % (unicode(self.path), unicode(e)))
if not blocks: if not blocks:
raise IOError('The picture %s could not be read' % unicode(self.path)) raise IOError('The picture %s could not be read' % unicode(self.path))
@ -58,90 +59,80 @@ class Photo(phys.File):
class IPhoto(Photo): class IPhoto(Photo):
def __init__(self, parent, whole_path):
super(IPhoto, self).__init__(parent, whole_path[-1])
self.whole_path = whole_path
def _build_path(self):
return self.whole_path
@property @property
def display_path(self): def display_path(self):
return super(IPhoto, self)._build_path() return Path(('iPhoto Library', self.name))
def get_iphoto_database_path():
ud = NSUserDefaults.standardUserDefaults()
prefs = ud.persistentDomainForName_('com.apple.iApps')
if 'iPhotoRecentDatabases' not in prefs:
raise directories.InvalidPathError()
plisturl = NSURL.URLWithString_(prefs['iPhotoRecentDatabases'][0])
return Path(plisturl.path())
class Directory(phys.Directory): def get_iphoto_pictures(plistpath):
cls_file_class = Photo if not io.exists(plistpath):
cls_supported_exts = ('png', 'jpg', 'jpeg', 'gif', 'psd', 'bmp', 'tiff', 'nef', 'cr2') raise InvalidPath(self)
s = io.open(plistpath).read()
def _fetch_subitems(self): # There was a case where a guy had 0x10 chars in his plist, causing expat errors on loading
subdirs, subfiles = super(Directory,self)._fetch_subitems() s = s.replace('\x10', '')
return subdirs, [name for name in subfiles if get_file_ext(name) in self.cls_supported_exts] # 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
class IPhotoLibrary(fs.Directory): s, count = re.subn(r'&(?![a-zA-Z0-9_-]+|#[0-9]+|#x[0-9a-fA-F]+;)', '', s)
def __init__(self, plistpath): if count:
self.plistpath = plistpath logging.warning("%d invalid XML entities replacement made", count)
self.refpath = plistpath[:-1] plist = plistlib.readPlistFromString(s)
# the AlbumData.xml file lives right in the library path result = []
super(IPhotoLibrary, self).__init__(None, 'iPhoto Library') for photo_data in plist['Master Image List'].values():
if not io.exists(plistpath):
raise InvalidPath(self)
def _update_photo(self, photo_data):
if photo_data['MediaType'] != 'Image': if photo_data['MediaType'] != 'Image':
return continue
photo_path = Path(photo_data['ImagePath']) photo_path = Path(photo_data['ImagePath'])
subpath = photo_path[len(self.refpath):-1] photo = IPhoto(photo_path)
subdir = self result.append(photo)
for element in subpath: return result
try:
subdir = subdir[element] class Directories(directories.Directories):
except KeyError: def __init__(self):
subdir = fs.Directory(subdir, element) directories.Directories.__init__(self, fileclasses=[Photo])
try: self.iphoto_libpath = get_iphoto_database_path()
IPhoto(subdir, photo_path) self.set_state(self.iphoto_libpath[:-1], directories.STATE_EXCLUDED)
except fs.AlreadyExistsError:
# it's possible for 2 entries in the plist to point to the same path. Ignore one of them.
pass
def update(self): def _get_files(self, from_path):
self.clear() if from_path == Path('iPhoto Library'):
s = open(unicode(self.plistpath)).read() is_ref = self.get_state(from_path) == directories.STATE_REFERENCE
# There was a case where a guy had 0x10 chars in his plist, causing expat errors on loading photos = get_iphoto_pictures(self.iphoto_libpath)
s = s.replace('\x10', '') for photo in photos:
# It seems that iPhoto sometimes doesn't properly escape & chars. The regexp below is to find photo.is_ref = is_ref
# any & char that is not a &-based entity (&, ", etc.). based on TextMate's XML return photos
# bundle's regexp else:
s, count = re.subn(r'&(?![a-zA-Z0-9_-]+|#[0-9]+|#x[0-9a-fA-F]+;)', '', s) return directories.Directories._get_files(self, from_path)
if count:
logging.warning("%d invalid XML entities replacement made", count)
plist = plistlib.readPlistFromString(s)
for photo_data in plist['Master Image List'].values():
self._update_photo(photo_data)
def force_update(self): # Don't update @staticmethod
pass 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'):
if path in self:
raise AlreadyThereError()
self._dirs.append(path)
else:
directories.Directories.add_path(self, path)
class DupeGuruPE(app_cocoa.DupeGuru): class DupeGuruPE(app_cocoa.DupeGuru):
def __init__(self): def __init__(self):
app_cocoa.DupeGuru.__init__(self, data, 'dupeGuru Picture Edition', appid=5) app_cocoa.DupeGuru.__init__(self, data, 'dupeGuru Picture Edition', appid=5)
self.scanner = ScannerPE() self.scanner = ScannerPE()
self.directories.dirclass = Directory self.directories = Directories()
self.directories.special_dirclasses[Path('iPhoto Library')] = lambda _, __: self._create_iphoto_library()
p = op.join(self.appdata, 'cached_pictures.db') p = op.join(self.appdata, 'cached_pictures.db')
self.scanner.cached_blocks = Cache(p) self.scanner.cached_blocks = Cache(p)
def _create_iphoto_library(self):
ud = NSUserDefaults.standardUserDefaults()
prefs = ud.persistentDomainForName_('com.apple.iApps')
if 'iPhotoRecentDatabases' not in prefs:
raise directories.InvalidPathError
plisturl = NSURL.URLWithString_(prefs['iPhotoRecentDatabases'][0])
plistpath = Path(plisturl.path())
return IPhotoLibrary(plistpath)
def _do_delete(self, j): def _do_delete(self, j):
def op(dupe): def op(dupe):
j.add_progress() j.add_progress()
@ -175,40 +166,19 @@ class DupeGuruPE(app_cocoa.DupeGuru):
def _do_load(self, j): def _do_load(self, j):
self.directories.load_from_file(op.join(self.appdata, 'last_directories.xml')) self.directories.load_from_file(op.join(self.appdata, 'last_directories.xml'))
for d in self.directories:
if isinstance(d, IPhotoLibrary):
d.update()
self.results.load_from_xml(op.join(self.appdata, 'last_results.xml'), self._get_file, j) self.results.load_from_xml(op.join(self.appdata, 'last_results.xml'), self._get_file, j)
def _get_file(self, str_path): def _get_file(self, str_path):
p = Path(str_path) p = Path(str_path)
for d in self.directories: if p in self.directories.iphoto_libpath[:-1]:
result = None return IPhoto(p)
if p in d.path: return app_cocoa.DupeGuru._get_file(self, str_path)
result = d.find_path(p[d.path:])
if isinstance(d, IPhotoLibrary) and p in d.refpath:
result = d.find_path(p[d.refpath:])
if result is not None:
return result
def add_directory(self, d):
result = app_cocoa.DupeGuru.add_directory(self, d)
if (result == 0) and (d == 'iPhoto Library'):
[iphotolib] = [dir for dir in self.directories if dir.path == d]
iphotolib.update()
return result
def copy_or_move(self, dupe, copy, destination, dest_type): def copy_or_move(self, dupe, copy, destination, dest_type):
if isinstance(dupe, IPhoto): if isinstance(dupe, IPhoto):
copy = True copy = True
return app_cocoa.DupeGuru.copy_or_move(self, dupe, copy, destination, dest_type) return app_cocoa.DupeGuru.copy_or_move(self, dupe, copy, destination, dest_type)
def start_scanning(self):
for directory in self.directories:
if isinstance(directory, IPhotoLibrary):
self.directories.set_state(directory.refpath, directories.STATE_EXCLUDED)
return app_cocoa.DupeGuru.start_scanning(self)
def selected_dupe_path(self): def selected_dupe_path(self):
if not self.selected_dupes: if not self.selected_dupes:
return None return None