Exemplo n.º 1
0
class GetGist(object):
    """
    Main GetGist objects linking inputs from the CLI to the helpers from
    GitHubTools (to deal with the API) and LocalTools (to deal with the local
    file system.
    """

    def __init__(self, **kwargs):
        """
        Instantiate GitHubTools & LocalTools, and set the variables required
        to get, create or update gists (filename and public/private flag)
        :param user: (str) GitHub username
        :param filename: (str) name of file from any Gist or local file system
        :param allow_none: (bool) flag to use GitHubTools.select_gist
        differently with `getgist` and `putgist` commands (if no gist/filename
        is found it raises an error for `getgist`, or sets `putgist` to create
        a new gist).
        :param create_private: (bool) create a new gist as private
        :param assume_yes: (bool) assume yes (or first option) for all prompts
        :return: (None)
        """

        # get arguments
        user = kwargs.get('user')
        allow_none = kwargs.get('allow_none', False)
        assume_yes = kwargs.get('assume_yes', False)
        filename = kwargs.get('filename')
        self.public = not kwargs.get('create_private', False)

        # instantiate local tools & check for user
        self.local = LocalTools(filename, assume_yes)
        if not user:
            message = """
                No default user set yet. To avoid this prompt set an
                environmental variable called  `GETGIST_USER`.'
            """
            self.local.oops(message)

        # instantiate filename, guthub tools and fetch gist
        self.github = GitHubTools(user, filename, assume_yes)
        self.gist = self.github.select_gist(allow_none)

    def get(self):
        """Reads the remote file from Gist and save it locally"""
        if self.gist:
            content = self.github.read_gist_file(self.gist)
            self.local.save(content)

    def put(self):
        """ Reads local file & update the remote gist (or create a new one)"""
        content = self.local.read(self.github.filename)
        if self.gist:
            self.github.update(self.gist, content)
        else:
            self.github.create(content, public=self.public)
Exemplo n.º 2
0
class GetGist(object):
    """
    Main GetGist objects linking inputs from the CLI to the helpers from
    GitHubTools (to deal with the API) and LocalTools (to deal with the local
    file system.
    """
    def __init__(self, **kwargs):
        """
        Instantiate GitHubTools & LocalTools (if needed), and set the variables required
        to get, create or update gists (filename and public/private flag)
        :param user: (str) GitHub username
        :param filename: (str) name of file from any Gist or local file system
        :param allow_none: (bool) flag to use GitHubTools.select_gist
        differently with `getgist` and `putgist` commands (if no gist/filename
        is found it raises an error for `getgist`, or sets `putgist` to create
        a new gist).
        :param create_private: (bool) create a new gist as private
        :param assume_yes: (bool) assume yes (or first option) for all prompts
        :return: (None)
        """
        user = kwargs.get("user")
        allow_none = kwargs.get("allow_none", False)
        assume_yes = kwargs.get("assume_yes", False)
        filename = kwargs.get("filename")
        self.public = not kwargs.get("create_private", False)

        if not user:
            message = """
            No default user set yet. To avoid this prompt set an
            environmental variable called  `GETGIST_USER`.'
            """
            GetGistCommons().oops(message)
            exit(1)

        self.github = GitHubTools(user, filename, assume_yes)
        self.local = LocalTools(filename, assume_yes) if filename else None
        self.gist = self.github.select_gist(allow_none) if filename else None

    def get(self):
        """Reads the remote file from Gist and save it locally"""
        if self.gist:
            content = self.github.read_gist_file(self.gist)
            self.local.save(content)

    def put(self):
        """ Reads local file & update the remote gist (or create a new one)"""
        content = self.local.read()
        if self.gist:
            self.github.update(self.gist, content)
        else:
            self.github.create(content, public=self.public)

    def ls(self):
        """ Lists all gists from a github user """
        self.github.list_gists()
Exemplo n.º 3
0
def test_create_gist_with_no_file(mocker, response):
    get = mocker.patch("getgist.request.GetGistRequests.get")
    token = mocker.patch("getgist.github.GitHubTools._get_token")
    get.return_value = response("user")
    token.return_value = GETGIST_TOKEN
    github = GitHubTools(GETGIST_USER, ".gist")
    assert not github.create(False)
Exemplo n.º 4
0
 def test_failed_create_gist(self, mock_token, mock_post, mock_get):
     mock_token.return_value = GETGIST_TOKEN
     mock_post.return_value = request_mock('gist/id_gist_1', case=False,
                                           status_code=404)
     mock_get.return_value = request_mock('user')
     yeah = GitHubTools(GETGIST_USER, '.gist.sample')
     self.assertFalse(yeah.create('42', public=False))
Exemplo n.º 5
0
 def test_create_gist(self, mock_token, mock_post, mock_get):
     mock_token.return_value = GETGIST_TOKEN
     mock_post.return_value = request_mock('gist/id_gist_1',
                                           status_code=201)
     mock_get.return_value = request_mock('user')
     yeah = GitHubTools(GETGIST_USER, '.gist.sample')
     self.assertTrue(yeah.create('42', public=False))
Exemplo n.º 6
0
def test_failed_create_gist(mocker, response):
    get = mocker.patch("getgist.request.GetGistRequests.get")
    post = mocker.patch("getgist.request.GetGistRequests.post")
    token = mocker.patch("getgist.github.GitHubTools._get_token")
    get.return_value = response("user")
    post.return_value = response("gist/id_gist_1", case=False, status_code=404)
    token.return_value = GETGIST_TOKEN
    github = GitHubTools(GETGIST_USER, ".gist.sample")
    assert not github.create("42", public=False)
Exemplo n.º 7
0
def test_create_gist_without_authorization(mocker):
    token = mocker.patch("getgist.github.GitHubTools._get_token")
    token.return_value = None
    github = GitHubTools(GETGIST_USER, ".gist.sample")
    assert not github.create("42")
Exemplo n.º 8
0
 def test_create_gist_with_no_file(self, mock_token, mock_get):
     mock_token.return_value = GETGIST_TOKEN
     mock_get.return_value = request_mock('user')
     yeah = GitHubTools(GETGIST_USER, '.gist')
     self.assertFalse(yeah.create(False))
Exemplo n.º 9
0
 def test_create_gist_without_authorization(self, mock_token):
     mock_token.return_value = None
     oops = GitHubTools(GETGIST_USER, '.gist.sample')
     self.assertFalse(oops.create('42'))
Exemplo n.º 10
0
 def test_create_gist_with_no_file(self, mock_token, mock_get):
     mock_token.return_value = GETGIST_TOKEN
     mock_get.return_value = request_mock('user')
     yeah = GitHubTools(GETGIST_USER, '.gist')
     self.assertFalse(yeah.create(False))
Exemplo n.º 11
0
 def test_create_gist_without_authorization(self, mock_token):
     mock_token.return_value = None
     oops = GitHubTools(GETGIST_USER, '.gist.sample')
     self.assertFalse(oops.create('42'))