Exemplo n.º 1
0
def get(gist_id, requested_file, destination_dir, facade):
    """ Download a gist file.

    Gists can have several files. This method searches for and downloads
    a single file from a gist.
    If the 'requested_file' is not informed, then it won't raise an error
    only if the gist have just a single file.

    :param gist_id: identifier of the gist to download
    :param requested_file: name of the Gist file to download
    :param destination_dir: destination directory after the download
    :param facade: instance of the object that actually perform the request
    """

    # Get the gist information
    response = facade.request_gist(gist_id)

    if response.ok:
        # Gist file found. Parse it into a 'model.Gist' class.
        gist_obj = model.Gist(response.json())
        list_names = [gistfile.filename for gistfile in gist_obj.files]

        if len(gist_obj.files) == 1 and not requested_file:
            # Download the only file in the gist
            gistfile = gist_obj.files[0]
            download(gistfile.raw_url, destination_dir,
                     gistfile.filename, gistfile.size)

            result = build_result(True, literals.DOWNLOAD_OK,
                                  gistfile.filename)
        else:
            # Gist have more than one file and filename not specified. Error
            if not requested_file:
                list_names = ", ".join(list_names)
                result = build_result(False, literals.DOWNLOAD_MORE_FILES,
                                      list_names)
            else:
                # Search for the Gist file
                gistfile = gist_obj.getFile(requested_file)
                if gistfile:

                    # Gist file found. Download it.
                    download(gistfile.raw_url, destination_dir,
                             gistfile.filename, gistfile.size)

                    result = build_result(True, literals.DOWNLOAD_OK,
                                          gistfile.filename)
                else:
                    # Requested file not found in Gist
                    list_of_names = ", ".join(list_names)
                    result = build_result(False, literals.FILE_NOT_FOUND,
                                          list_of_names)

    else:
        # Handle GitHub response error
        result = build_result(False, literals.DOWNLOAD_ERROR,
                              response.json()['message'])

    return result
Exemplo n.º 2
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.º 3
0
def fork(gist_id, facade):
    """ Forks a gist.

    :param gist_id: identifier of the Gist to fork
    :param facade: instance of the object that actually performs the request
    """
    response = facade.fork_gist(gist_id)

    if response.ok:
        result = build_result(True, model.Gist(response.json()))
    else:
        if response.json():
            result = build_result(False, literals.FORK_ERROR, gist_id,
                                  response.json()['message'])
        else:
            result = build_result(False, literals.UNHANDLED_EXCEPTION)
    return result
Exemplo n.º 4
0
def show(gist_id, requested_file, facade):
    """ Retrieve a single gist.

    If the 'requested_file' is None, then it will show the
    'metadata' of the Gist. This is: its description, urls, file names..

    If the 'requested_file' is informed, then it will show
    the content of the gist file.

    :param gist_id: identifier of the Gist to print
    :param requested_file: Gist File to show
    :param facade: instance of the object that actually performs the request
    """

    # get the gist information
    response = facade.request_gist(gist_id)

    if response.ok:
        # Gist found. Parse the json response into the 'model.Gist' class
        gist_obj = model.Gist(response.json())
        if not requested_file:
            # Fill the response with the metadata of the gist
            result = build_result(True, gist_obj)
        else:
            # We want to return the content of the file. Search for the content
            list_names = [gistfile.filename for gistfile in gist_obj.files]
            file_gist = gist_obj.getFile(requested_file)
            if file_gist:
                # Fill the response with the metadata of the gist
                result = build_result(True, file_gist)
            else:
                # File not found in Gist
                list_of_names = ", ".join(list_names)
                result = build_result(False, literals.FILE_NOT_FOUND,
                                      list_of_names)

    else:
        # GitHub response not ok. Parse the response
        result = build_result(False, literals.SHOW_ERROR,
                              response.json()['message'])

    return result
Exemplo n.º 5
0
def list_gists(username, facade):
    """ Retrieve the list of gists for a concrete user.

    :param token: GitHub authentication token
    :param facade: instance of the object that actually performs the request
    """

    response = facade.request_list_of_gists(username)

    if response.ok:
        # List of gists for the requested user found.
        list_gists = []
        for gist in response.json:
            list_gists.append(model.Gist(gist))

        return build_result(True, list_gists)
    else:
        # GitHub response error. Parse the response
        return build_result(False, literals.LISTS_ERROR,
                            response.json['message'])
Exemplo n.º 6
0
def list_gists(username, facade, want_starred):
    """ Retrieve the list of gists for a concrete user.

    :param username: GitHub authentication token
    :param facade: instance of the object that actually performs the request
    :param want_starred: if we want the starred ones
    """

    if not want_starred:
        response = facade.request_list_of_gists(username)
    else:
        response = facade.request_list_starred_gists(username)

    if response.ok:
        # List of gists for the requested user found.
        _list_gists = []
        for gist in response.json():
            _list_gists.append(model.Gist(gist))

        return build_result(True, _list_gists)
    else:
        # GitHub response error. Parse the response
        return build_result(False, literals.LISTS_ERROR,
                            response.json()['message'])
Exemplo n.º 7
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'])