【问题标题】:Edit github gist through Python with pyGithub lib使用 pyGithub lib 通过 Python 编辑 github gist
【发布时间】:2018-04-30 10:40:39
【问题描述】:

这是我对 stackoverflow 的第一个问题。所以请善待我,如果我不是主题或精确,请帮助我下次改进。

我正在尝试使用 pyGithub 通过 Python3 修改和现有 Github Gist。 我创建了一个 API 令牌并且身份验证工作正常,但我正在努力编辑 Gist。我找不到合适的例子,这让我很清楚。

这是我的代码:

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)

这是我收到的错误消息:

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'}}}

我在这里找到了 pygithub 库的一些描述: pyGithub Docu

这是我要修改的要点:Gist

非常感谢任何解决此问题的帮助。

多米尼克

【问题讨论】:

  • 你得到的断言错误是一个线索:Gist.edit接收到的description参数必须是NotSet或者一个字符串,并且是一个字典(带有一个“description”键,即令人困惑)。这是 PyGithub 中的一般模式:参数类型在调用 GitHub API 之前被断言。

标签: python-3.x github gist pygithub


【解决方案1】:

这段代码的主要问题是它将字典传递给Gist.editGist.edit 接受关键字参数。

PyGithub's documentation 说:

编辑(描述=未设置,文件=未设置)

所以它应该被称为g.edit(description="new description", files=...)。关于files,同样的文档说:

files - 到 github.InputFileContent.InputFileContent 的字符串字典

所以files 参数可能如下所示:

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

总结:

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")},
)

【讨论】:

    【解决方案2】:

    如果使用 pyGithub lib 不是硬约束,那么我建议使用同样用 python 编写的gifc gist 客户端。因此,对于您的情况,在通过 pip 安装后(克隆后)可以按如下方式编辑或更新要点 -

    更新要点

    • 迭代编辑所有(或部分)文件

      • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
        您可以从之前的 get 方法中获取 gist id
    • 更改说明

      • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
        您可以从之前的 get 方法中获取 gist id
    • nanovimgedit

      等编辑器中以交互方式编辑文件的内容
      • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
    • 两者都做
      • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      相关资源
      最近更新 更多