Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
active-project

*.pyc
*.sqlite

.DS_Store
6 changes: 6 additions & 0 deletions bin/start_goto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ _goto_completions()
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi

if [[ ${prev} == "github" ]]; then
COMPREPLY=( $(compgen -W "$(goto --github-list $cur)" ${cur}) )
return 0
fi

}
complete -o default -F _goto_completions goto

Expand Down
Binary file added github_cache.sqlite
Binary file not shown.
68 changes: 68 additions & 0 deletions gotomagic/githubmagic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
import requests
Copy link
Collaborator

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 error

jonas in ~/git/technocake/goto on feature/github-autocompletions
$ sudo ./uninstall.sh && sudo ./install.sh 
remove the line: source start_goto from your bash config file
(most likely .bash_profile or .bashrc in your home folder)

 Also, your data in ~/.goto is not deleted
Step 1: Installing goto into /usr/local/opt/goto
Step 2: Adding symlinks to /usr/local/bin
Step 3: Setting up magic data folder in /home/jonas/.goto
~/.bash_profile does not exist

To make goto function properly, add this line to your bash config file: 

         source start_goto

into one of these (.bashrc | .profile | .bash_profile)
Want to append to .bashrc? [y|n]: y
active project is now: goto
active project is now: goto
Traceback (most recent call last):
  File "/usr/local/opt/goto/the_real_goto.py", line 9, in <module>
    from gotomagic.githubmagic import GitHub
  File "/usr/local/opt/goto/gotomagic/githubmagic.py", line 2, in <module>
    import requests
ImportError: No module named requests
ERROR: Installation step failed with exit code 1

Copy link
Collaborator

@Arxcis Arxcis Sep 19, 2018

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... wierd

Copy link
Collaborator

@Arxcis Arxcis Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@Arxcis Arxcis Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My python versions

$ python --version
Python 2.7.15rc1

$ python3 --version
Python 3.6.5

Copy link
Owner Author

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

Copy link
Owner Author

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

Copy link
Collaborator

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.

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]
Copy link
Collaborator

Choose a reason for hiding this comment

The 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():
Copy link
Collaborator

Choose a reason for hiding this comment

The 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()
1 change: 1 addition & 0 deletions gotomagic/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def get_uri(self, magicword):
text.warning["magicword_does_not_exist"],
magicword=magicword
)
return None

def list_shortcuts(self, verbose=False):
""" Lists all magicwords.
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pyperclip
pyperclip
requests
requests-cache
25 changes: 25 additions & 0 deletions the_real_goto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

command = sys.argv[2]`
if command == 'github':

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)
Expand Down