1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# pip install github3.py
from github3 import login as github_login

import argparse

import code
import getpass


interpreter = None

try:
    import IPython
    interpreter = 'ipython'
except ImportError:
    pass

try:
    import bpython
    interpreter = 'bpython'
except ImportError:
    pass


BANNER = """
You are now connected to Github and have access to a github object,
for example: github.user()
"""


def REPL(github):
    """
    Open an interactive REPL (read-eval-print-loop) with access to github object
    :param github: The authentiated github object.
    :returns:
      Interactive shell with `github` object.
    """
    msg = BANNER
    if interpreter == 'bpython':
        bpython.embed(locals_ = locals(), banner = msg)
    elif interpreter == 'ipython':
        IPython.embed(banner2 = msg)
    elif interpreter is None:
        code.interact(local = locals(), banner = msg)


parser = argparse.ArgumentParser(description = "Github API Shell")
parser.add_argument('-u', '--username', help="github user", default=None)
parser.add_argument('-t', '--token', help="github user token", default=None)
args = parser.parse_args()

if args.username is not None:
    github = github_login(args.username, password=getpass.getpass())
elif args.token is not None:
    github = github_login(token=args.token)

REPL(github)