Ejemplo n.º 1
0
 def load_metadata(self,item,missing_only=False,notify_plugins=True):
     'retrieve metadata for an item from the source'
     if self.load_embedded_thumbs:
         result=imagemanip.load_metadata(item,collection=self,filename=self.get_path(item),
             get_thumbnail=True,missing_only=missing_only,check_for_sidecar=self.use_sidecars,
             notify_plugins=notify_plugins)
     else:
         result=imagemanip.load_metadata(item,collection=self,filename=self.get_path(item),
             get_thumbnail=False,missing_only=missing_only,check_for_sidecar=self.use_sidecars,
             notify_plugins=notify_plugins)
     if self.load_embedded_thumbs and not item.thumb:
         item.thumb=False
     return result
Ejemplo n.º 2
0
 def load_metadata(self, item, missing_only=False, notify_plugins=True):
     'retrieve metadata for an item from the source'
     if self.load_embedded_thumbs:
         result = imagemanip.load_metadata(
             item,
             collection=self,
             filename=self.get_path(item),
             get_thumbnail=True,
             missing_only=missing_only,
             check_for_sidecar=self.use_sidecars,
             notify_plugins=notify_plugins)
     else:
         result = imagemanip.load_metadata(
             item,
             collection=self,
             filename=self.get_path(item),
             get_thumbnail=False,
             missing_only=missing_only,
             check_for_sidecar=self.use_sidecars,
             notify_plugins=notify_plugins)
     if self.load_embedded_thumbs and not item.thumb:
         item.thumb = False
     return result
Ejemplo n.º 3
0
    def copy_item(self, src_collection, src_item, prefs):
        'copy an item from another collection source'
        try:
            print 'copying item', src_item, prefs
            name = os.path.split(src_item.uid)[1]
            dest_dir = prefs['base_dest_dir']
            src_filename = None
            if src_collection.local_filesystem:
                src_filename = src_collection.get_path(src_item)
            temp_filename = ''
            temp_dir = ''
            name_scheme = naming_schemes[prefs['name_scheme']]
            dest_name_template = name_scheme[1]
            dest_needs_meta = name_scheme[2]

            if dest_needs_meta and src_item.meta == None:
                temp_dir = tempfile.mkdtemp('', '.image-', dest_dir)
                temp_filename = os.path.join(temp_dir, name)
                try:
                    if src_collection.local_filesystem:
                        io.copy_file(
                            src_filename, temp_filename
                        )  ##todo: this may be a desirable alternative for local images
                        if src_collection.use_sidecars and 'sidecar' in src_item.__dict__ and os.path.exists(
                                src_collection.get_path(src_item.sidecar)):
                            temp_sidecar = os.path.join(
                                temp_dir,
                                os.path.split(src_item.sidecar)[1])
                            io.copy_file(
                                src_collection.get_path(src_item.sidecar),
                                temp_sidecar)
                    else:
                        open(temp_filename, 'wb').write(
                            src_collection.get_file_stream(src_item).read())
                except IOError:
                    print 'Error copying file', src_item
                    import traceback, sys
                    tb_text = traceback.format_exc(sys.exc_info()[2])
                    print tb_text
                    ##todo: log an error
                    ##todo: maybe better to re-raise the exception here
                    return False
                src_filename = temp_filename
                try:
                    imagemanip.load_metadata(src_item, self, src_filename)
                except:
                    src_item.meta = {}
            dest_path, dest_name = name_item(src_item, dest_dir,
                                             dest_name_template)
            if not src_collection.local_filesystem:
                local_name = src_collection.get_file_name(src_item)
                if local_name:
                    dest_name = local_name
            if not os.path.exists(dest_path):
                os.makedirs(dest_path)
            dest_filename = os.path.join(dest_path, dest_name)
            print 'copying to dest filename', dest_filename
            if os.path.exists(dest_filename):
                if prefs['action_if_exists'] == EXIST_SKIP:
                    print 'SKIPPING', src_item
                    ##TODO: LOGGING TO IMPORT LOG
                    return False
                if prefs['action_if_exists'] == EXIST_RENAME:
                    dest_filename = altname(dest_filename)

            try:
                if prefs['move_files'] or temp_filename:
                    io.move_file(
                        src_filename,
                        dest_filename,
                        overwrite=prefs['action_if_exists'] == EXIST_OVERWRITE)
                else:
                    if src_collection.local_filesystem:
                        io.copy_file(src_filename,
                                     dest_filename,
                                     overwrite=prefs['action_if_exists'] ==
                                     EXIST_OVERWRITE)
                    else:
                        open(dest_filename, 'wb').write(
                            src_collection.get_file_stream(src_item).read())
            except IOError:
                ##todo: log an error
                ##todo: maybe better to re-raise the exception here
                print 'Error copying image', src_item
                import traceback, sys
                tb_text = traceback.format_exc(sys.exc_info()[2])
                print tb_text
                return False

            try:
                if prefs['move_files'] and not temp_filename:
                    src_collection.delete(src_item)
                if temp_filename and temp_filename != src_filename:
                    io.remove_file(temp_filename)
                if temp_dir:
                    shutil.rmtree(temp_dir)
            except IOError:
                ##todo: log an error
                ##todo: maybe better to re-raise the exception here
                print 'Error cleaning up after copying image', src_item
                import traceback, sys
                tb_text = traceback.format_exc(sys.exc_info()[2])
                print tb_text

            item = baseobjects.Item(self.get_relpath(dest_filename))
            item.mtime = io.get_mtime(dest_filename)
            item.selected = src_item.selected
            #copy metadata from the original
            #TODO: drop metadata not supported by local store?
            if src_item.meta is not None:
                item.init_meta(src_item.meta.copy(), self)
            if src_collection.local_filesystem:
                #for local filesystems lets copy the backup metadata to ensure that the use
                #will see there is unsaved data. could also just write the metadata first, but
                #this gives the user a bit more flexibility.
                if 'meta_backup' in dir(src_item):
                    item.meta_backup = src_item.meta_backup
            else:
                #the item.meta might contain data that isn't in the original, so lets write
                #it to the image (since a localstore image should always represent what is in
                #the image) and then reload it to get anything else that wasn't in item.meta
                self.write_metadata(
                    item)  #potential risk of data loss by writing data here
                self.load_metadata(
                    item,
                    notify_plugins=False)  ##TODO: Shoudln't we notify plugins?
            self.make_thumbnail(item)
            self.add(
                item
            )  ##todo: should we lock the image browser rendering updates for this call??
            return True
        except:
            print 'Error copying src item'
            import traceback, sys
            tb_text = traceback.format_exc(sys.exc_info()[2])
            print tb_text
            return False
Ejemplo n.º 4
0
    def copy_item(self,src_collection,src_item,prefs):
        'copy an item from another collection source'
        try:
            print 'copying item',src_item,prefs
            name=os.path.split(src_item.uid)[1]
            dest_dir=prefs['base_dest_dir']
            src_filename=None
            if src_collection.local_filesystem:
                src_filename=src_collection.get_path(src_item)
            temp_filename=''
            temp_dir=''
            name_scheme=naming_schemes[prefs['name_scheme']]
            dest_name_template=name_scheme[1]
            dest_needs_meta=name_scheme[2]

            if dest_needs_meta and src_item.meta==None:
                temp_dir=tempfile.mkdtemp('','.image-',dest_dir)
                temp_filename=os.path.join(temp_dir,name)
                try:
                    if src_collection.local_filesystem:
                        io.copy_file(src_filename,temp_filename) ##todo: this may be a desirable alternative for local images
                        if src_collection.use_sidecars and 'sidecar' in src_item.__dict__ and os.path.exists(src_collection.get_path(src_item.sidecar)):
                            temp_sidecar=os.path.join(temp_dir,os.path.split(src_item.sidecar)[1])
                            io.copy_file(src_collection.get_path(src_item.sidecar),temp_sidecar)
                    else:
                        open(temp_filename,'wb').write(src_collection.get_file_stream(src_item).read())
                except IOError:
                    print 'Error copying file',src_item
                    import traceback,sys
                    tb_text=traceback.format_exc(sys.exc_info()[2])
                    print tb_text
                    ##todo: log an error
                    ##todo: maybe better to re-raise the exception here
                    return False
                src_filename=temp_filename
                try:
                    imagemanip.load_metadata(src_item,self,src_filename)
                except:
                    src_item.meta={}
            dest_path,dest_name=name_item(src_item,dest_dir,dest_name_template)
            if not src_collection.local_filesystem:
                local_name=src_collection.get_file_name(src_item)
                if local_name:
                    dest_name=local_name
            if not os.path.exists(dest_path):
                os.makedirs(dest_path)
            dest_filename=os.path.join(dest_path,dest_name)
            print 'copying to dest filename',dest_filename
            if os.path.exists(dest_filename):
                if prefs['action_if_exists']==EXIST_SKIP:
                    print 'SKIPPING',src_item
                    ##TODO: LOGGING TO IMPORT LOG
                    return False
                if prefs['action_if_exists']==EXIST_RENAME:
                    dest_filename=altname(dest_filename)

            try:
                if prefs['move_files'] or temp_filename:
                    io.move_file(src_filename,dest_filename,overwrite=prefs['action_if_exists']==EXIST_OVERWRITE)
                else:
                    if src_collection.local_filesystem:
                        io.copy_file(src_filename,dest_filename,overwrite=prefs['action_if_exists']==EXIST_OVERWRITE)
                    else:
                        open(dest_filename,'wb').write(src_collection.get_file_stream(src_item).read())
            except IOError:
                ##todo: log an error
                ##todo: maybe better to re-raise the exception here
                print 'Error copying image',src_item
                import traceback,sys
                tb_text=traceback.format_exc(sys.exc_info()[2])
                print tb_text
                return False

            try:
                if prefs['move_files'] and not temp_filename:
                    src_collection.delete(src_item)
                if temp_filename and temp_filename!=src_filename:
                    io.remove_file(temp_filename)
                if temp_dir:
                    shutil.rmtree(temp_dir)
            except IOError:
                ##todo: log an error
                ##todo: maybe better to re-raise the exception here
                print 'Error cleaning up after copying image',src_item
                import traceback,sys
                tb_text=traceback.format_exc(sys.exc_info()[2])
                print tb_text

            item=baseobjects.Item(self.get_relpath(dest_filename))
            item.mtime=io.get_mtime(dest_filename)
            item.selected=src_item.selected
            #copy metadata from the original
            #TODO: drop metadata not supported by local store?
            if src_item.meta is not None:
                item.init_meta(src_item.meta.copy(),self)
            if src_collection.local_filesystem:
                #for local filesystems lets copy the backup metadata to ensure that the use
                #will see there is unsaved data. could also just write the metadata first, but
                #this gives the user a bit more flexibility.
                if 'meta_backup' in dir(src_item):
                    item.meta_backup=src_item.meta_backup
            else:
                #the item.meta might contain data that isn't in the original, so lets write
                #it to the image (since a localstore image should always represent what is in
                #the image) and then reload it to get anything else that wasn't in item.meta
                self.write_metadata(item) #potential risk of data loss by writing data here
                self.load_metadata(item,notify_plugins=False) ##TODO: Shoudln't we notify plugins?
            self.make_thumbnail(item)
            self.add(item) ##todo: should we lock the image browser rendering updates for this call??
            return True
        except:
            print 'Error copying src item'
            import traceback,sys
            tb_text=traceback.format_exc(sys.exc_info()[2])
            print tb_text
            return False