Exemplo n.º 1
0
def post(public, upload_files, filepath, description, facade):
    """ Create a new Gist.

    Currently only support create Gist with single files. (Then you can
    'update' the gist and attach more files in it, but the creation only
    supports one file)

    You are able to specify if you want to create a public or private
    gist and set its description.

    :param public: whenever new Gist should be public or private
    :param upload_files: list with  input files to upload
    :param filepath: input parameter path
    :param description: brief description of the Gist
    :param facade: instance of the object that actually performs the request
    """

    # Prepare the Gist file object and set its description and 'public' value
    gist = model.Gist()
    if description:
        gist.description = description
    gist.public = public

    for upfile in upload_files:
        # Prepare the content reading the file
        gist_file = model.GistFile()
        gist_file.filename = upfile
        with open(os.path.join(filepath, upfile), 'r') as f:
            file_content = f.read()
            gist_file.content = file_content
        gist.addFile(gist_file)

    response = facade.create_gist(gist)
    # Parse the response
    if response.ok:
        result = build_result(True, model.Gist(response.json()))
    else:
        if response.json():
            result = build_result(False, response.json()['message'])
        else:
            result = build_result(False, literals.UNHANDLED_EXCEPTION)
    return result
Exemplo n.º 2
0
def update(gistid, description, filenames, filepath, new, remove, facade):
    """ Updates a gist.

    :param gistid: identifier of the Gist to update
    :param description: new description of the Gist. If 'None' it won't be
    updated
    :param filenames: list with names of the files to modify its contents
    :param filepath: input parameter path
    :param new: whenever the file is new or already exists
    :param remove: if the file should be deleted instead of modified
    :param facade: instance of the object that actually performs the request
    """

    # First get the result
    response = facade.request_gist(gistid)

    if response.ok:
        # Gist found.
        gist = model.Gist(response.json())
    else:
        result = build_result(False, literals.UPDATE_NOK,
                              response.json()['message'])
        return result

    if description:
        # Update the description of a Gist if requested
        gist.description = description

    if filenames:
        for filename in filenames:
            # File to update
            file_obj = gist.getFile(filename)
            if not file_obj:
                if remove:
                    return build_result(False, literals.UPDATE_RM_NF)
                if new:
                    # Upload a new file to gist
                    gist_file = model.GistFile()
                    gist_file.filename = filename
                    with open(os.path.join(filepath, filename), 'r') as f:
                        file_content = f.read()
                        gist_file.content = file_content
                    gist.addFile(gist_file)
                else:
                    # File not found and option --new it does not exist
                    return build_result(False, literals.UPDATE_NF)
            else:
                if new:
                    # File not found and option --new it does not exist
                    return build_result(False, literals.UPDATE_NEW_DUP)
                if remove:
                    # Remove a file
                    gist.setFile(filename, "null")
                else:
                    # Update the contents of the file
                    with open(os.path.join(filepath, filename), 'r') as f:
                        file_content = f.read()
                        file_obj.content = file_content
                    gist.setFile(filename, file_obj)

    # prepare the request
    response = facade.update_gist(gist)
    if response.ok:
        return build_result(True, gist)
    else:
        return build_result(False, literals.UPDATE_NOK,
                            response.json()['message'])