Пример #1
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
Пример #2
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
Пример #3
0
 def get_mtime(self, item):
     return io.get_mtime(self.get_path(item))
Пример #4
0
 def get_mtime(self,item):
     return io.get_mtime(self.get_path(item))
Пример #5
0
    def get_mtime(self, item):
        log.info('mtime {} from collection {}'.format(item, self))

        return io.get_mtime(self.get_path(item))
Пример #6
0
    def __call__(self):
        collection = self.collection
        jobs = self.worker.jobs
        self.last_update_time = time.time()
        try:
            if not self.collection_walker:
                log.info('Starting WebAlbums theme walk on {}'.format(collection.name))
                self.collection_walker = self._walk_albums()
                self.done = False
                pluginmanager.mgr.suspend_collection_events(self.collection)
                
        except StopIteration:
            self.notify_items = []
            self.collection_walker = None
            log.error('Aborted directory walk on {}'.format(collection.name))
            return True

        while jobs.ishighestpriority(self) and not self.done:

            backend.idle_add(self.browser.update_backstatus, True, 'Scanning for new images')
            
            while jobs.ishighestpriority(self):
                try:
                    photo = self.collection_walker.next()
                except StopIteration:
                    self.done = True
                    break
                
                #import_pdb_set_trace()
                
                try:
                    if photo.find("details").get("isGpx") == "true":
                        continue
                except Exception as e:
                    log.warn(e)
                    pass

                path = photo.find("details").find("photoId").text
                
                fullpath = "{}/images/{}".format(ROOT, path)
                
                item = baseobjects.Item(fullpath)
                
                if collection.find(item) < 0:
                    # if collection.load_meta:
                    #     collection.load_metadata(item, notify_plugins=False)
                    # elif collection.load_preview_icons:
                    #     collection.load_thumbnail(item)
                    #     if not item.thumb:
                    #         item.thumb = False
                            
                    item.mtime = io.get_mtime(fullpath)
                    
                    self.browser.lock.acquire()
                    collection.add(item)
                    self.browser.lock.release()
                    
                    backend.idle_add(self.browser.resize_and_refresh_view, self.collection)
                        
            # once we have found enough items, add to collection and notify browser
            if time.time() > self.last_update_time+1.0 or len(self.notify_items) > 100:
                self.last_update_time = time.time()
                
                self.browser.lock.acquire()
                for item in self.notify_items:
                    collection.add(item, False)
                    
                self.browser.lock.release()
                
                backend.idle_add(self.browser.resize_and_refresh_view, self.collection)
                self.notify_items = []
                
        if not self.done:
            return False
    
        log.info('Directory walk complete for {}'.format(collection))
            
        backend.idle_add(self.browser.resize_and_refresh_view, self.collection)
        backend.idle_add(self.browser.update_backstatus, False, 'Search complete')
                            
        self.notify_items = []
        self.collection_walker = None
        self.done = False
            
        pluginmanager.mgr.resume_collection_events(self.collection)
        
        return True
Пример #7
0
    def get_mtime(self, item):
        log.info('mtime {} from collection {}'.format(item, self))

        return io.get_mtime(self.get_path(item))
Пример #8
0
    def __call__(self):
        collection = self.collection
        jobs = self.worker.jobs
        self.last_update_time = time.time()
        try:
            if not self.collection_walker:
                log.info('Starting WebAlbums theme walk on {}'.format(
                    collection.name))
                self.collection_walker = self._walk_albums()
                self.done = False
                pluginmanager.mgr.suspend_collection_events(self.collection)

        except StopIteration:
            self.notify_items = []
            self.collection_walker = None
            log.error('Aborted directory walk on {}'.format(collection.name))
            return True

        while jobs.ishighestpriority(self) and not self.done:

            backend.idle_add(self.browser.update_backstatus, True,
                             'Scanning for new images')

            while jobs.ishighestpriority(self):
                try:
                    photo = self.collection_walker.next()
                except StopIteration:
                    self.done = True
                    break

                #import_pdb_set_trace()

                try:
                    if photo.find("details").get("isGpx") == "true":
                        continue
                except Exception as e:
                    log.warn(e)
                    pass

                path = photo.find("details").find("photoId").text

                fullpath = "{}/images/{}".format(ROOT, path)

                item = baseobjects.Item(fullpath)

                if collection.find(item) < 0:
                    # if collection.load_meta:
                    #     collection.load_metadata(item, notify_plugins=False)
                    # elif collection.load_preview_icons:
                    #     collection.load_thumbnail(item)
                    #     if not item.thumb:
                    #         item.thumb = False

                    item.mtime = io.get_mtime(fullpath)

                    self.browser.lock.acquire()
                    collection.add(item)
                    self.browser.lock.release()

                    backend.idle_add(self.browser.resize_and_refresh_view,
                                     self.collection)

            # once we have found enough items, add to collection and notify browser
            if time.time() > self.last_update_time + 1.0 or len(
                    self.notify_items) > 100:
                self.last_update_time = time.time()

                self.browser.lock.acquire()
                for item in self.notify_items:
                    collection.add(item, False)

                self.browser.lock.release()

                backend.idle_add(self.browser.resize_and_refresh_view,
                                 self.collection)
                self.notify_items = []

        if not self.done:
            return False

        log.info('Directory walk complete for {}'.format(collection))

        backend.idle_add(self.browser.resize_and_refresh_view, self.collection)
        backend.idle_add(self.browser.update_backstatus, False,
                         'Search complete')

        self.notify_items = []
        self.collection_walker = None
        self.done = False

        pluginmanager.mgr.resume_collection_events(self.collection)

        return True