Exemple #1
0
    def save(self, file_path, image_type='png'):
        """
        Saves screen to a file
        :param file_path: str, path to save image into
        :param image_type: str, image type (png or jpg)
        """

        saved = None
        if not file_path:
            LOGGER.error('Path not specificed for snapshot.')
            self.saved.emit(saved)
            return
        file_dir, file_name, file_ext = path_utils.split_path(file_path)
        if not os.path.isdir(file_dir):
            folder_utils.create_folder(file_dir)
        else:
            if os.path.isfile(file_path):
                fileio.delete_file(file_path)

        image_type = image_type if image_type.startswith('.') else '.{}'.format(image_type)
        if image_type != file_ext:
            if file_ext not in ('.png', '.jpg'):
                LOGGER.warning('Image of type "{}" is not supported by snapshot!'.format(file_ext))
                return
            image_type = file_ext

        if image_type.startswith('.'):
            image_type = image_type[1:]
        image_type = image_type.upper()

        saved = self._snapshot_pixmap.save(file_path, image_type)
        self._last_saved_location = file_path
        self.saved.emit(file_path if saved else None)
    def move(self, new_folder):

        if not new_folder or not os.path.isdir(new_folder):
            return

        identifier = self.format_identifier()

        file_directory, file_name, file_extension = path_utils.split_path(
            identifier)
        new_path = path_utils.join_path(
            new_folder, '{}{}'.format(file_name, file_extension))

        valid = fileio.move_file(self.format_identifier(), new_path)
        if not valid:
            return

        dependencies = self.get_dependencies()

        self._db.move(identifier, new_path)

        skin_folder = dependencies.get('skin_folder', None)
        if not skin_folder or not os.path.isdir(skin_folder):
            return

        skin_folder_item = self.library.get(skin_folder)
        move_function = skin_folder_item.functionality().get('move')
        if move_function:
            move_function(new_folder)

        return new_folder
Exemple #3
0
    def rename(self, new_name):

        identifier = self.format_identifier()

        file_directory, file_name, file_extension = path_utils.split_path(identifier)
        if not new_name.endswith(file_extension):
            new_name = '{}{}'.format(new_name, file_extension)
        new_name = fileio.rename_file('{}{}'.format(file_name, file_extension), file_directory, new_name)

        self._db.rename(identifier, new_name)

        return new_name
Exemple #4
0
    def full_name(self):
        """
        Returns data full name
        :return: str
        """

        directory, name, extension = path_utils.split_path(self.format_identifier())
        extension = extension or self.extension()
        if extension:
            return path_utils.clean_path('{}{}'.format(name, extension))

        return name
Exemple #5
0
    def copy(self, target_path, replace=True):

        # TODO :Take into account dependencies

        if not target_path:
            return

        if os.path.isfile(target_path):
            if not replace:
                return
            fileio.delete_file(target_path)

        _, _, file_extension = path_utils.split_path(self.format_identifier())
        target_directory, target_name, target_extension = path_utils.split_path(target_path)
        if target_extension != file_extension:
            target_path = path_utils.join_path(target_directory, '{}{}'.format(target_name, file_extension))

        copy_path = fileio.copy_file(self.format_identifier(), target_path)

        self._db.sync()

        return copy_path
def format_path(format_string, path='', **kwargs):
    """
    Resolves given path by replacing necessary info with proper data
    :param format_string: str
    :param path: str
    :param kwargs:
    :return: str
    """

    dirname, name, extension = path_utils.split_path(path)
    encoding = locale.getpreferredencoding()

    temp = tempfile.gettempdir()
    if temp:
        try:
            temp = temp.decode(encoding)
        except Exception:
            pass

    username = osplatform.get_user(lower=True)
    if username:
        try:
            username = username.decode(encoding)
        except Exception:
            pass

    local = os.getenv('APPDATA') or os.getenv('HOME')
    if local:
        try:
            local = local.decode(encoding)
        except Exception:
            pass

    kwargs.update(os.environ)

    format_dict = {
        'name': name,
        'path': path,
        'root': path,
        'user': username,
        'temp': temp,
        'home': local,
        'local': local,
        'dirname': dirname,
        'extension': extension
    }
    kwargs.update(format_dict)

    resolve_string = path_utils.normalize_path(format_string).format(**kwargs)

    return path_utils.clean_path(resolve_string)
Exemple #7
0
    def move(self, new_folder):

        if not new_folder or not os.path.isdir(new_folder):
            return

        identifier = self.format_identifier()

        file_directory, file_name, file_extension = path_utils.split_path(identifier)
        new_path = path_utils.join_path(new_folder, '{}{}'.format(file_name, file_extension))

        valid = fileio.move_file(self.format_identifier(), new_path)
        if not valid:
            return

        self._db.move(identifier, new_path)

        return new_path
Exemple #8
0
    def _format_path(self, format_string, path='', **kwargs):
        """
        Resolves the given string with the given path and keyword arguments
        :param format_string: str
        :param path: str
        :param kwargs: dict
        :return: str
        """

        LOGGER.debug('Format String: {}'.format(format_string))

        dirname, name, extension = path_utils.split_path(path)
        encoding = locale.getpreferredencoding()
        temp = tempfile.gettempdir()
        if temp:
            temp = temp.decode(encoding)

        username = osplatform.get_user().lower()
        if username:
            username = username.decode(encoding)

        local = os.getenv('APPDATA') or os.getenv('HOME')
        if local:
            local = local.decode(encoding)

        kwargs.update(os.environ)

        labels = {
            "name": name,
            "path": path,
            "user": username,
            "temp": temp,
            "local": local,
            "dirname": dirname,
            "extension": extension,
        }

        kwargs.update(labels)

        resolved_string = six.u(str(format_string)).format(**kwargs)

        LOGGER.debug('Resolved string: {}'.format(resolved_string))

        return path_utils.clean_path(resolved_string)
Exemple #9
0
    def move(self, new_folder):

        if not new_folder or not os.path.isdir(new_folder):
            return

        identifier = self.format_identifier()

        file_directory, file_name, file_extension = path_utils.split_path(
            identifier)
        new_path = path_utils.join_path(
            new_folder, '{}{}'.format(file_name, file_extension))

        valid = folder.move_folder(self.format_identifier(),
                                   new_path,
                                   only_contents=True)
        if not valid:
            return

        return new_path