def get(self, filename=None):
        """return snippets listing according to the project-id or read a specific script"""
        user_profile = get_user(request=self.request)
        workspace = self.request.args.get("workspace", "common")

        if filename is None:
            success, details = snippetstorage.instance().get_files(workspace=workspace,
                                                                user=user_profile)
        else:
            filename = fix_encoding_uri_param(filename)
            success, details = snippetstorage.instance().read_file(filename=filename,
                                                                workspace=workspace,
                                                                user=user_profile)

        if success != constant.OK:
            raise HTTP_500(details)

        return {"cmd": self.request.path,
                "snippets": details}
    def delete(self, item_path):
        """delete item like a file or folder"""
        user_profile = get_user(request=self.request)
        workspace = self.request.args.get("workspace", "common")
        item_path = fix_encoding_uri_param(item_path)

        success, details = snippetstorage.instance().delete(item_path=item_path,
                                                         workspace=workspace,
                                                         user=user_profile)

        if success != constant.OK:
            raise HTTP_500(details)

        return {"cmd": self.request.path,
                "message": details}
    def patch(self, item_path):
        """update item"""
        user_profile = get_user(request=self.request)
        workspace = self.request.args.get("workspace", "common")
        item_path = fix_encoding_uri_param(item_path)

        new_name = self.request.data.get("name")

        success, details = snippetstorage.instance().update(item_path=item_path,
                                                         workspace=workspace,
                                                         user=user_profile,
                                                         new_name=new_name)

        if success != constant.OK:
            raise HTTP_500(details)

        return {"cmd": self.request.path,
                "message": details}
    def post(self, item_path):
        """add script"""
        user_profile = get_user(request=self.request)
        workspace = self.request.args.get("workspace", "common")
        item_path = fix_encoding_uri_param(item_path)

        content_file = self.request.data.get("content", "")

        success, details = snippetstorage.instance().add(item_path=item_path,
                                                      workspace=workspace,
                                                      user=user_profile,
                                                      content_file=content_file)

        if success != constant.OK:
            raise HTTP_500(details)

        return {"cmd": self.request.path,
                "message": details}
def load_yamlfile(yaml_file, workspace, user, repo):
    """load yaml file"""
    logger.debug('jobmodel - loading yaml from file')

    if repo == constant.REPO_ACTIONS:
        repo_path = actionstorage.instance().get_path(workspace=workspace)
        file_path = n("%s/%s" % (repo_path, yaml_file))
    elif repo == constant.REPO_SNIPPETS:
        repo_path = snippetstorage.instance().get_path(workspace=workspace)
        file_path = n("%s/%s" % (repo_path, yaml_file))
    else:
        file_path = n(yaml_file)

    if not os.path.exists(file_path):
        error_str = "file=%s not found " % yaml_file
        error_str += "in workspace=%s" % workspace
        logger.error('jobmodel - %s' % error_str)
        return (constant.NOT_FOUND, error_str)

    with open(file_path, 'r') as fd:
        yaml_str = fd.read()

    return load_yamlstr(yaml_str=yaml_str)