1

this is my first question to stackoverflow. So be kind, if I am not on topic or precise and help me improve for the next time.

I am trying to modify and existing Github Gist through Python3 using pyGithub. I created an API-token and authentification works ok, but I am struggeling to edit the Gist. I could not find an appropriate example, that made it clear to me.

Here is my code:

from github import Github
g = Github("XXX")

test2 = {"description": "the description for this gist",
         "files": {"filter": {"content": "updated file contents"},
                   "Task": {"filename": "new_name.txt",
                   "content": "modified content"},
"new_file.txt": {
  "content": "a new file"
}
}
}

g.get_gist(id="b2c5668fefe1f2e80252aabf4ef4e96c").edit(test2)

This is the error message I am getting:

Traceback (most recent call last):
File "gist.py", line 15, in <module>
g.get_gist(id="b2c5668fefe1f2e80252aabf4ef4e96c").edit(test2)
  File "/Users/DSpreitz/ogn-silentwings/venv/lib/python3.6/site-packages/github/Gist.py", line 249, in edit
  assert description is github.GithubObject.NotSet or isinstance(description, str), description
AssertionError: {'description': 'the description for this gist', 'files': {'filter': {'content': 'updated file contents'}}}

I found some description of the pygithub lib here: pyGithub Docu

This is the Gist I am trying to modify: Gist

Any help to solve this problem is greatly appreciated.

Dominic

Dominic
  • 11
  • 1
  • The assertion error you got is a clue : the ``description`` parameter received by ``Gist.edit`` must be ``NotSet`` or a string, and it's a dictionary (with a "description" key, which is confusing). This is a general pattern in PyGithub : argument types are asserted before calling the GitHub API. – jacquev6 May 16 '18 at 06:42

2 Answers2

0

The main issue with this code is that it's passing a dictionary to Gist.edit. Gist.edit accepts keyword arguments.

PyGithub's documentation says:

edit(description=NotSet, files=NotSet)

so it should be called as g.edit(description="new description", files=...). Regarding files, the same documentation says:

files – dict of string to github.InputFileContent.InputFileContent

so the files parameter could look like:

{"foo.txt": github.InputFileContent(content="bar")}

Summarized:

import github

token = "..."  # https://github.com/settings/tokens

gh = github.Github(token)
gist = gh.get_gist("f04c4b19919c750602f4d0c5f7feacbf")
gist.edit(
    description="new description",
    files={"foo.txt": github.InputFileContent(content="bar")},
)
jacquev6
  • 620
  • 4
  • 18
0

If using pyGithub lib is NOT a hard constraint then I'd suggest using gifc gist client also written in python. So for your case, editing or updating the gist can be done as follows after installing it via pip (after cloning) -

Update a gist

  • Edit all (or some) files iteratively

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
      You can get the gist id from the get method from earlier
  • Change description

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
      You can get the gist id from the get method from earlier
  • Edit contents of a file interactively in an editor like nano, vim or gedit

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
  • Do both
    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"
jar
  • 2,646
  • 1
  • 22
  • 47