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
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")},
)
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"
Related
I just tried the standalone PyQGIS application by running the custom script "Proximity.py"* in a VS Code project without the need of a GUI (such as QGIS).
But, when I run the python-program I get the following message:
proj_create_from_database: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.2\proj\proj.db contains DATABASE.LAYOUT.VERSION.MINOR = 0 whereas a number >= 2 is expected. It comes from another PROJ installation. (see also: Error Message after launching the configuration (launch.json) from VS Code (when pressing F5))
I'm trying this online example with the following installations:
PostgreSQL 14
Python39
.vscode\extensions\ms-python.python-2022.4.1\pythonFiles\lib\python\debugpy\launcher
osgeo4w-setup.exe (including QGIS LTR)
I read that there is a solution by undefining [PROJ_LIB] before importing pyproj or osgeo: del os.environ ['PROJ_LIB'] as described under this link. If this is also supposed to be the correct solution in this case, can someone help me with step-by-step instructions (for dummies)?
. * The "Proximity.py" script is a pyqgis standalone example from "https://github.com/MarByteBeep/pyqgis-standalone"
Finally, I got a solution to be able to run the "standalone PyQGIS"* example "Proximity" (provided by MarByteBeep).
This solution was possible without needing to launch the configuration file "launch.json" as above described. And so, avoiding the need to make any configuration to the environment variable "PROJ_LIB" by trying to circumvent the above issue.
I just first added the following two code-lines (see here line 2 and 3) in the python file "main.py" so as to be able to use the plugin "PROCESSING" (initially line 8 of the "main.py" file), then I store it and finally I ran it.
Line 1: from qgis.core import
Line 2: import sys
Line 3: sys.path.append('C:\Program Files\QGIS 3.24.1\apps\qgis\python\plugins')
Line 4: qgs = QgsApplication([], False)
Line 5: ...
The Proximity example is based on the answer of "Mar Tjin" to the following Question: "Looking for manual on how to properly setup standalone PyQGIS without GUI"
. * By "Standalone PyQGIS" I refer to code/scripts that can be run outside the QGIS-GUI (=> QGIS-Desktop/Server Application). In my case under the external Editor VS Code
My goal is to use python3 (3.8 preferred) to read and eventually set the comments tag on an MP3 file.
I have installed eyed3 (python3.8 -m pip install eyed3) and the following code will load and can read all the tags, except when it comes to the comments. I've tried reading the documentation from the creator's website (http://eyed3.nicfit.net/) and the GitHub site with no luck and I'm not fully understanding the output from the following code:
import eyed3
music = "/path/to/valid/music/file.mp3"
audio = eyed3.load(music)
print(audio.tag.comments)
this spits out the following:
<eyed3.id3.tag.CommentsAccessor object at 0x7f54ca55d2b0>
I've tried doing a dir(audio.tag.comments) and that doesn't provide anything except a bunch of class bits, and the following functions "get", "remove", "set"
Using something like:
moo = audio.tag.comments.get()
throws an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mrhobbits/.local/lib/python3.8/site-packages/eyed3/utils/__init__.py", line 138, in wrapped_fn
return fn(*args, **kwargs)
TypeError: get() missing 1 required positional argument: 'description'
Yet I can't find anything that would show me what 'description' is, or maybe I have to drill deeper to get the comment information?
I should also mention that doing:
print(audio.tag.artist)
works just fine and returns:
Alestorm
I'm a bit lost here. Any help would be great.
Nevermind,
A quick search here revealed that the 'comments' is a list sort of object, and that doing:
audio.tag.comments[0].text
reveals the information I was looking for.
Is there anything that offers replay-type functionality, by pointing at a predefined prompt-answer file?
What works and what I'd like to achieve.
Let's take an example, using a cookiecutter to prep a Python package for pypi
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git
You've downloaded /Users/jluc/.cookiecutters/cookiecutter-pypackage before. Is it okay to delete and re-download it? [yes]:
full_name [Audrey Roy Greenfeld]: Spartacus 👈 constant for me/my organization
email [audreyr#example.com]: spartacus#example.com 👈 constant for me/my organization
...
project_name [Python Boilerplate]: GladiatorRevolt 👈 this will vary.
project_slug [q]: gladiator-revolt 👈 this too
...
OK, done.
Now, I can easily redo this, for this project, via:
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --replay
This is great!
What I want:
Say I create another project, UnleashHell.
I want to prep a file somehow that has my developer-info and project level info for Unleash. And I want to be able to run it multiple times against this template, without having to deal with prompts. This particular pypi template gets regular updates, for example python 2.7 support has been dropped.
The problem:
A --replay will just inject the last run for this cookiecutter template. If it was run against a different pypi project, too bad.
I'm good with my developer-level info, but I need to vary all the project level info.
I tried copying the replay file via:
cp ~/.cookiecutter_replay/cookiecutter-pypackage.json unleash.json
Edit unleash.json to reflect necessary changes.
Then specify it via --config-file flag
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --config-file unleash.json
I get an ugly error, it wants YAML, apparently.
cookiecutter.exceptions.InvalidConfiguration: Unable to parse YAML file .../000.packaging/unleash.json. Error: None of the known patterns match for {
"cookiecutter": {
"full_name": "Spartacus",
No problem, json2yaml to the rescue.
That doesn't work either.
cookiecutter.exceptions.InvalidConfiguration: Unable to parse YAML file ./cookie.yaml. Error: Unable to determine type for "
full_name: "Spartacus"
I also tried a < stdin redirect:
cookiecutter.prompts.txt:
yes
Spartacus
...
It doesn't seem to use it and just aborts.
cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git < ./cookiecutter.prompts.txt
You've downloaded ~/.cookiecutters/cookiecutter-pypackage before. Is it okay to delete and re-download it? [yes]
: full_name [Audrey Roy Greenfeld]
: email [audreyr#example.com]
: Aborted
I suspect I am missing something obvious, not sure what. To start with, what is the intent and format expected for the --config file?
Debrief - how I got it working from accepted answer.
Took accepted answer, but adjusted it for ~/.cookiecutterrc usage. It works but the format is not super clear. Especially not on the rc which has to be yaml, though that's not always/often the case with rc files.
This ended up working:
file ~/.cookiecutterrc:
without nesting under default_context... tons of unhelpful yaml parse errors (on a valid yaml doc).
default_context:
#... cut out for privacy
add_pyup_badge: y
command_line_interface: "Click"
create_author_file: "y"
open_source_license: "MIT license"
# the names to use here are:
# full_name:
# email:
# github_username:
# project_name:
# project_slug:
# project_short_description:
# pypi_username:
# version:
# use_pytest:
# use_pypi_deployment_with_travis:
# add_pyup_badge:
# command_line_interface:
# create_author_file:
# open_source_license:
I still could not get a combination of ~/.cookiecutterrc and a project-specific config.yaml to work. Too bad that expected configuration format is so lightly documented.
So I will use the .rc but enter the project name, slug and description each time. Oh well, good enough for now.
You are near.
Try this cookiecutter https://github.com/audreyr/cookiecutter-pypackage.git --no-input --config-file config.yaml
The --no-input parameter will suppress the terminal user input, it is optional of course.
The config.yaml file could look like this:
default_context:
full_name: "Audrey Roy"
email: "audreyr#example.com"
github_username: "audreyr"
cookiecutters_dir: "/home/audreyr/my-custom-cookiecutters-dir/"
replay_dir: "/home/audreyr/my-custom-replay-dir/"
abbreviations:
pp: https://github.com/audreyr/cookiecutter-pypackage.git
gh: https://github.com/{0}.git
bb: https://bitbucket.org/{0}
Reference to this example file: https://cookiecutter.readthedocs.io/en/1.7.0/advanced/user_config.html
You probably just need the default_context block since that is where the user input goes.
I have the following code:
import github
token = "my gitHub token"
g = github.Github(token)
new_repo = g.get_user().create_repo("NewMyTestRepo")
print("New repo: ", new_repo)
new_repo.create_file("new_file.txt", "init commit", "file_content ------ ")
I have run this code, and this is the result:
New repo: Repository(full_name="myname/NewMyTestRepo")
Traceback (most recent call last):
...
File "/home/serega/PycharmProjects/GitProj/myvenv/lib/python3.5/site-packages/github/Requester.py", line 180, in __check
raise self.__createException(status, responseHeaders, output)
github.GithubException.UnknownObjectException: 404 {'message': 'Not Found', 'documentation_url': 'https://developer.github.com/v3'}
I think there may be problem in the scope of my token, it has repo scope. Nevertheless, I have managed to create a repo, so it seems, it should be allowed to make commit in that repo with new file inside.
About scopes I saw that link : https://developer.github.com/v3/oauth/#scopes
And it states:
repo
Grants read/write access to code, commit statuses, repository
invitations, collaborators, and deployment statuses for public and
private repositories and organizations.
I will really appreciate if somebody can clarify about required token's scope, and what could be the problem.
repo scope is enough to create files in a repository. It would seem from this question that the problem is that your file must have a leading slash:
new_repo.create_file("new_file.txt", "init commit", "file_content ------ ")
In my cucumber -jvm, Maven, junit Setup I have my testRunner file as
package com.lebara.testrunner;
import cucumber.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#Cucumber.Options(
glue = {"com.lebara.stepdefs","com.lebara.framework.main", "com.lebara.testrunner"},
features = "C:/Users/sarthak.dayanand/Documents/WebRefreshTest/CukeAutomation/LebaraWebAutomationTest1/src/main/resources",
format = {"pretty", "html:target/cucumber-html-report", "json-pretty:target/cucumber-report.json"},
tags = {"#UserJourney"}
)
public class RunCukesTest {
}
I have my feature file in the above mentioned directory.
If I run it, I get the following exception:
cucumber.runtime.CucumberException: No features found at [C:/Users/sarthak.dayanand/Documents/WebRefreshTest/CukeAutomation/LebaraWebAutomationTest1/src/main/resources/cucumber]...
If I remove the "features" option in the testrunner, it tries to look for feature files in the same directory as my testrunner.java
cucumber.runtime.CucumberException: No features found at [com/lebara/testrunner]
And if I put the feature files there, it works.
My question is why is my feature file not being picked up from my previous location, which I thought to be the default file structure for cucumber - maven setup.
How do I make it pick up from there? Help appreciated.
Where exactly are your test runner and feature files? I've got the following setup which works perfectly:
src/test/
java/
com/mypackage/
TestRunner.java
StepDefinition.java
resources
com/mypackage/
fancy.feature
The Maven/Cuke conventions will have the tests executed from the tests/java directory and the feature files found in the test/resources directory. My test runner is basically the same as yours but with less options:
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty"})
public class TestRunner { }
Hope this helps if you hadn't already found an answer.
I have a setup similar to yours (not using the Maven/Cucumber conventions). In my options, I don't specify the path from root, but from the project's source folder where the features are held. It makes sense, since otherwise the tests would only be runnable from your machine.
In your case, I think it should be:
features = "src/main/resources"
Just add features = { "classpath:features/feature.feature"}, and the feature must under test/resources/features/feature.feature.
#CucumberOptions(
format = {"pretty", "html:target/html"},
features = {"classpath:features/feature.feature"},
snippets = SnippetType.CAMELCASE
Note classpath.
When you compile your code if you are using maven open up target/test-classes/features and you will see feature.feature
//Removing the space between "**classpath**" and "**:com/**" helped.
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"classpath:com/tk/feature/"}, //NOTE: NO SPACE
glue = {"classpath: com.tk.cucumber"},
plugin = {
"pretty",
"html:build/reports/cucumber"
,"json:build/reports/cucumber-tests/test.json"}
)
public class RunAPITests {}
If you are providing the complete path of the feature file i.e.
"C:/Users/sarthak.dayanand/Documents/WebRefreshTest/CukeAutomation/LebaraWebAutomationTest1/src/main/resources" as in your query, try again by replacing the '/' character with '\\'(double back slash) as below.
"C:\\Users\\sarthak.dayanand\\Documents\\WebRefreshTest\\CukeAutomation\\LebaraWebAutomationTest1\\src\main\\resources\\abc.feature"
This is a git repo which uses the latest cucumber version : Cucumber- Example
Clone this repo and run it in your local machine. The #Given is defined and it should pass. The #Then and #When should be shown as undefined.
This is how the output for it should look :
Output for the Belly feature
Use the structure mentioned :
src / test / java/ io /cucumber / {Step definitions java and run cucumber test files here}
src /test / resources/ io/ cucumber / {feature files here}
You can run the gradle build using ./gradlew clean build
and the cucumber test using ./gradlew clean test --info
If this works, then use the same format in your project.
Just changing .Feature to .feature the problem got resolved for me.
Also make sure the path for feature is righly mention in CucumberOptions as per your feature folder
Some of the online tutorial have mentioned .Feature which brings this problem
so changing the case will solve this problem
There is another instance in which 'Feature Not Found' error occurs. I am posting the solution under this answer as there is no similar question.
I got this error when trying to run the Runner file first time after setting up Cucumber project in Maven. The solution i found was as follows: Go to the folder in which the 'feature' file is present in Windows Explorer. Check the size of the feature file you are trying to run. If the size is '0' KB, it will show the 'Feature Not Found' error. Make some changes to file until a value greater than zero is displayed. Run again after making changes.
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"src/main/resources/cucumber/features"},//your feature path
tags = "not #Wip",
glue = {"classpath:steps"},
plugin = {"pretty", "html:target/cucumber/html"})
You must set the feature directory correctly
By putting the feature file under src/test/java where the runner and steps file or
by putting it under src/main/java the problem will get resolved.