-
Notifications
You must be signed in to change notification settings - Fork 3
add github sub-url autocompletion #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6119897
ad6291e
41807db
913f39a
8804199
6704824
b92b255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,5 +4,6 @@ | |
| active-project | ||
|
|
||
| *.pyc | ||
| *.sqlite | ||
|
|
||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import requests | ||
| import requests_cache | ||
| import re | ||
|
|
||
| requests_cache.install_cache('github_cache') | ||
|
|
||
|
|
||
| class GitHub(): | ||
| BASE_URL = "https://api.github.com" | ||
| repoinfo = "/repos/{user}/{repo}" | ||
|
|
||
| def __init__(self, url): | ||
| self.user, self.repo = self.parse_github_url(url) | ||
|
|
||
| def search_for_urls(self, word, return_keys=False): | ||
| path = self.repoinfo.format(user=self.user, repo=self.repo) | ||
| r = requests.get(self.BASE_URL + path) | ||
|
|
||
| if r and r.status_code == 200: | ||
| urls = r.json() | ||
| query = re.compile(r'{word}.*_url'.format(word=word)) | ||
| keys = list(filter(query.match, urls.keys())) | ||
|
|
||
| if return_keys: | ||
| return [key.split("_url")[0] for key in keys] | ||
| else: | ||
| return [[key.split("_url")[0], urls[key]] for key in keys] | ||
|
|
||
| def parse_github_url(self, url): | ||
| """ Extracts user and repo from url """ | ||
| parts = url.split("github.com")[1].split("/") | ||
| return parts[1], parts[2] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider naming your magic numbers |
||
|
|
||
| def get_repo_url(self): | ||
| return "https://github.com/{user}/{repo}".format( | ||
| user=self.user, | ||
| repo=self.repo | ||
| ) | ||
|
|
||
| def url_for(self, endpoint): | ||
| return self.get_repo_url() + "/" + endpoint | ||
|
|
||
|
|
||
| def test_parse_github_url(): | ||
| url = "https://github.com/technocake/goto" | ||
|
|
||
| api = GitHub(url) | ||
| assert api.user == "technocake", "user is not correct" | ||
| assert api.repo == "goto", "repo is not correct" | ||
|
|
||
|
|
||
| def test_search_for_urls(): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love the tests 👍 |
||
| url = "https://github.com/technocake/goto" | ||
| api = GitHub(url) | ||
| url = api.search_for_urls("issues") | ||
| print(url) | ||
| url = api.search_for_urls("pu") | ||
| print(url) | ||
| url = api.search_for_urls("c") | ||
| print(url) | ||
| url = api.search_for_urls("Å") | ||
| print(url) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| test_parse_github_url() | ||
| test_search_for_urls() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| pyperclip | ||
| pyperclip | ||
| requests | ||
| requests-cache |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from gotomagic.magic import GotoMagic | ||
| import gotomagic.text as text | ||
| from gotomagic.text import print_text | ||
| from gotomagic.githubmagic import GitHub | ||
|
|
||
|
|
||
| def usage(): | ||
|
|
@@ -108,6 +109,30 @@ def usage(): | |
| print(text.warning["no_magicword_named_code"]) | ||
| exit(0) | ||
|
|
||
| # Generating dynamic auto completion list | ||
| # used by start_goto | ||
| if sys.argv[2] == '--github-list': | ||
| try: | ||
| api = GitHub(magic['github']) | ||
| query = "" | ||
| if len(sys.argv) == 4: | ||
| query = sys.argv[3] | ||
| keys = api.search_for_urls(query, return_keys=True) | ||
| print("\n".join(keys)) | ||
| exit(0) | ||
| except KeyError: | ||
| exit(1) | ||
|
|
||
| # going to sub-url in github | ||
| if sys.argv[2] == 'github': | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
| api = GitHub(magic.get_uri('github')) | ||
| if len(sys.argv) == 4: | ||
| url = api.url_for(endpoint=sys.argv[3]) | ||
| open_link(url) | ||
| else: | ||
| open_link(magic.get_uri('github')) | ||
| exit(0) | ||
|
|
||
| if '-o' in sys.argv or '--open' in sys.argv or 'open' in sys.argv: | ||
| open_folder(magic.get_uri(sys.argv[3])) | ||
| exit(0) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When doing a fresh install of this branch i got a
missing requests errorUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UPDATE
I ran
pip install -r requirements.txt(python2-pip) and got the same result... wierdUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UPDATE - Today pip2 works! Pip3 still not working
pip3: https://asciinema.org/a/y4R65wKjtsDxY1OvmFIznZYdi
pip2: https://asciinema.org/a/qLCM0R42RyKdTOqtiu3TmiZbV
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My python versions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goto uses "python" from the environment, not python3 so I guess you install requests with pip3 but goto finds python, which is symlinked to python2.7 -- and requests isn't installed there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until this point, I made effort into making goto dependency-less - to solve these issues.
But it limits its design. I could implement standard python versions of both fetching urls (using urllib) and copying to clipboard ( using tkinter) -- it would remove dependencies.
Open to discuss.
Think structuring goto as a python package is the way to go if we choose to use dependencies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting discussion indeed. The idea of having as few dependencies as possible is very attractive. If Python has standard libraries which solves our problem (without having to jump through hoops) why not use them?
If standard libraries does not solve our problem, we have to look somewhere else.