def rename_file(self, text):
     self.ids.filename.disabled = True
     self.ids.filename.focus = False
     self.space.on_popup_dismiss()
     if not self.filename_valid(text):
         return
     if text != self.filename:
         full_old_path = pure_posix_path(self.path, self.filename)
         full_new_path = pure_posix_path(self.path, text)
         self.space.rename_file(full_old_path, full_new_path, file=self)
         self.ids.filename.disabled = True
         self.focus = False
    def makedirs(self):
        for _dir in self.data['name']:
            full_path = pure_posix_path(self.dst_path, _dir)
            try:
                self.sftp.makedirs(full_path)
                attrs = get_dir_attrs(full_path, self.sftp)
            except OSError:
                ex_log(
                    f'Could not make dir {full_path}. Detected regular file.')
                self.manager.locked_paths.add(full_path)
                confirm_popup(
                    callback=self.delete_file,
                    movable=True,
                    _args=full_path,
                    text=
                    f'Path {full_path} already exists on remote server but it is a file\n'
                    f'To be able to upload data to that destination it must be removed\n\n'
                    f'Do you agree to delete the file and create directory?')

            else:
                logger.info(f'Created directory - {_dir}')
                self.done = True
                if self.manager.is_current_path(self.dst_path):
                    self.manager.uploaded(self.dst_path, attrs)

        else:
            self.manager.sftp_queue.put(self.sftp)
            self.manager.thread_queue.put('.')
            self.manager.next_transfer()
    def rmdir(self, _path):

        try:
            for f in self.sftp.listdir_attr(_path):
                rpath = pure_posix_path(_path, f.filename)
                if stat.S_ISDIR(f.st_mode):
                    self.rmdir(rpath)
                else:
                    rpath = pure_posix_path(_path, f.filename)
                    self.sftp.remove(rpath)
                    # avoid to inform about files from directories like .thumbnails
                    for name in forbidden_names:
                        if name in _path:
                            break
                    else:
                        self.info(f.filename)

        except IOError as io:
            ex_log(f'Failed to remove directory {io}')
            if 'Socket is closed' in io:
                self.manager.connection_error()
                self.manager.put_transfer({**self.data})
                ex_log(f'Uploading {filename(self.remote_path)} exception. {io}')
            elif io.errno == 13:  # Permission denied
                logger.warning('Could not list directory. Permission denied.')

        except Exception as ex:
            ex_log(f'Failed to remove directory {ex}')
            self.error(ex)
            return

        try:
            self.sftp.rmdir(_path)
        except Exception as ex:
            ex_log(f'Failed to remove directory {ex}')
            self.error(ex)
        else:
            self.info(path.split(_path)[1])
            self.done = True
            if _path == self.remote_path:
                self.on_remove()
        finally:
            self.manager.sftp_queue.put(self.sftp)
            self.manager.thread_queue.put('.')
            self.manager.next_transfer()
Esempio n. 4
0
 def thumb_dir_exists(self):
     thdr = None
     try:
         thdr = pure_posix_path(self.dst_path, thumb_dir)
         if not self.sftp.exists(thdr):
             self.sftp.makedirs(thdr)
     except Exception as ex:
         ex_log(f'Failed to make thumbnail directory {thdr}, {ex}')
         return False
     else:
         return True
Esempio n. 5
0
    def paste_files(self):
        clipboard.OpenClipboard()
        files = ()
        if clipboard.IsClipboardFormatAvailable(clipboard.CF_HDROP):
            files = copy.deepcopy(
                clipboard.GetClipboardData(clipboard.CF_HDROP))
        if files:
            clipboard.EmptyClipboard()
            for file in files:
                file = bytes(file, 'utf-8')
                self.external_dropfile(None, file)
        elif self.copied_files:
            path = self.originator.current_path
            if path:
                for file in self.copied_files:
                    old = pure_posix_path(file.path, file.filename)
                    new = pure_posix_path(path, file.filename)
                    self.rename_file(old, new, file)
                    self.copied_files.remove(file)

        clipboard.CloseClipboard()
Esempio n. 6
0
    def thumbnail_drop(self, _, src_path):
        src_path = src_path.decode(encoding='UTF-8', errors='strict')
        self.thumbnail = path.split(src_path)[1]
        self.pic_name = f'{self.filename}.jpg'
        cache_pic = pure_windows_path(cache_path, self.thumbnail)
        try:
            if path.exists(cache_pic):
                remove(cache_pic)
            if not path.exists(cache_path):
                makedirs(cache_path)
            copyfile(src_path, cache_pic)

        except Exception as ex:
            self.popup.title = f'Failed to generate thumbnail {ex}'
            return

        th = ThumbnailGenerator(src_path=cache_pic, dst_path='')
        th.start()
        th.join()
        if th.ok:
            self.popup.title = 'Thumbnail generated'
            cache_pic = pure_windows_path(cache_path,
                                          self.destination.strip('/'),
                                          thumb_dir, self.pic_name)
            print('CACHE PIC', th.thumb_path, cache_pic)
            try:
                if not path.exists(dst_path(cache_pic)):
                    makedirs(dst_path(cache_pic))

                if path.exists(cache_pic):
                    remove(cache_pic)

                copyfile(th.thumb_path, cache_pic)
                self.sftp.put(cache_pic,
                              pure_posix_path(self.destination, thumb_dir,
                                              self.pic_name),
                              preserve_mtime=True)
            except Exception as ex:
                self.popup.title = f'Failed to upload thumbnail for {self.filename} {type(ex)}'
                ex_log(f'Failed to upload thumbnail for {self.filename} {ex}')
            else:
                self.popup.title = 'Thumbnail uploaded succesfully'
                self.originator.bind_external_drop()

                def dismiss_popup(_):
                    self.popup.dismiss()
                    self.originator.refresh_thumbnail(self.filename)

                Clock.schedule_once(dismiss_popup, .8)
                Window.unbind(on_dropfile=self.thumbnail_drop)
        else:
            self.popup.title = 'Failed to upload thumbnail'
Esempio n. 7
0
 def __init__(self, data, manager, bar, sftp, preserve_mtime=False):
     super().__init__()
     self.data = data
     self.dst_path = data['dst_path']
     self.src_path = data['src_path']
     self.file_name = os.path.split(self.src_path)[1]
     self.full_remote_path = pure_posix_path(self.dst_path, self.file_name)
     self.manager = manager
     self.bar = bar
     self.sftp = sftp
     self.preserve_mtime = preserve_mtime or data.get('preserve_mtime')
     self.done = False
     self.waiting_for_directory = None
     self.attrs = None
     self.thumbnails = data['thumbnails']
     self.settings = data['settings']
Esempio n. 8
0
    def upload_thumbnail(self):
        if self.thumbnails:
            try:
                th = ThumbnailGenerator(self.src_path, self.dst_path)
                th.start()
                th.join()
                if th.ok:
                    self.bar.flush()
                    self.bar.set_values(f'Uploading thumbnail of {self.file_name}')

                    if self.thumb_dir_exists():
                        self.put(th.thumb_path, pure_posix_path(self.dst_path, thumb_dir, th.thumb_name), True)
                    else:
                        logger.info(f'Thumbnail for {self.file_name} not uploaded')
            except Exception as ex:
                ex_log(f'Failed to upload thumbnail for {self.file_name}. {ex}')
Esempio n. 9
0
    def local_walk(self, task):
        src_path = task['src_path']
        dst_path = task['dst_path']
        local_base, dir_to_walk = os.path.split(src_path)

        for root, dirs, files in os.walk(src_path):
            relative_path = root.replace(local_base, '')[1:]
            relative_path = pure_posix_path(dst_path,
                                            *relative_path.split('\\'))

            for file in files:
                self.put_transfer({
                    'type': 'upload',
                    'dir': False,
                    'src_path': pure_windows_path(root, file),
                    'dst_path': relative_path,
                    'thumbnails': task['thumbnails'],
                    'settings': self.upload_settings
                })
            if dirs:
                empty_dirs = []
                for _dir in dirs:
                    if not len(os.listdir(pure_windows_path(root, _dir))):
                        empty_dirs.append(_dir)
                if empty_dirs:
                    self.put_transfer({
                        'type': 'upload',
                        'dir': True,
                        'dst_path': relative_path,
                        'name': empty_dirs,
                        'thumbnails': task['thumbnails'],
                        'settings': self.upload_settings
                    })

        self.put_transfer({
            'type': 'upload',
            'dir': True,
            'dst_path': dst_path,
            'name': [dir_to_walk],
            'thumbnails': task['thumbnails'],
            'settings': self.upload_settings
        })
    def run(self):
        for thumbnail in self.thumbnails:
            try:
                src = pure_posix_path(self.src_path, thumbnail)
                if not local_path_exists(self.dst_path):
                    makedirs(self.dst_path)
                self.sftp.get(src,
                              pure_windows_path(self.dst_path, thumbnail),
                              preserve_mtime=True)

            except FileNotFoundError:
                pass

            except Exception as ex:
                ex_log(f'Failed to download thumbnail {str(ex)}')

        self.manager.thread_queue.put('.')
        self.manager.sftp_queue.put(self.sftp)
        self.manager.next_transfer()
        self.callback()
Esempio n. 11
0
 def test_pure_posix_path(self):
     result = common.pure_posix_path('/home/users/patrick', 'Test',
                                     'config.ini')
     self.assertEqual(result, '/home/users/patrick/Test/config.ini')
Esempio n. 12
0
 def internal_file_drop(self, file):
     """Called from IconController when it detects widget collision"""
     for mfile in self.selected_files:
         old = mfile.filename
         new = pure_posix_path(file.filename, mfile.filename)
         self.originator.rename_file(old, new, mfile)