-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh_repl.py
186 lines (143 loc) · 4.89 KB
/
gh_repl.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""Use this program to interact with your repos (note that there are better
solutions out there).
"""
from __future__ import annotations
import json
import sys
from typing import Any, Callable
from lib import github_rest, utils
from lib.metprint import LogType
# pylint:disable=import-outside-toplevel
def printMarkdown(raw: str, maxpages: int = 0) -> None:
"""Pretty print markdown."""
try:
import pypandoc
from catpandoc import pandoc2ansi, processpandoc
output = json.loads(pypandoc.convert_text(raw, "json", "md"))
pandoc = pandoc2ansi.Pandoc2Ansi(80, 0, (4, 0, 0))
markdown = ""
for block in output["blocks"]:
processpandoc.processBlock(block, pandoc)
markdown = pandoc.genOutput()
except ModuleNotFoundError:
markdown = raw
paginatedList(markdown.split("\n"), 30, print, maxpages)
def listRepos(data: str, user: str) -> None:
"""List the user repos."""
userRepos = github_rest.getListOfUserRepos(user, data)
paginatedList(userRepos, 8, github_rest.printRepo)
def paginatedList(
iterable: list[Any],
perPage: int,
printFunc: Callable[[dict[str, Any]], None],
maxpages: int = 0,
) -> None:
"""Print a paginated list."""
totalPages = len(iterable) // perPage + 1
for index, iteration in enumerate(iterable):
page = index // perPage
if maxpages != 0 and page >= maxpages:
return
if index > 0 and index % perPage == 0:
input(f"Page {index // perPage} of {totalPages} (Next)>")
printFunc(iteration)
"""REPL functions
"""
def replhelp() -> None:
"""Return help text for the REPL."""
utils.clear()
utils.printf.logPrint("Most of the time the 'user' arg can be omitted", LogType.INFO)
utils.printf.logPrint("Functions: ", LogType.BOLD)
for name, function in functions.items():
params = list(function.__code__.co_varnames[: function.__code__.co_argcount])
utils.printf.logPrint(f"- {name} : {params}")
def replexit() -> None:
"""Exit the REPL."""
sys.exit(0)
def repos(user: str | None = None) -> None:
"""List repos."""
user = user if user is not None else username
listRepos("repos", user)
def stars(user: str | None = None) -> None:
"""List repos the user has starred."""
user = username if user is None else user
listRepos("stargazing", user)
def watching(user: str | None = None) -> None:
"""List repos the user is watching."""
user = username if user is None else user
listRepos("subscriptions", user)
def profile(user: str | None = None) -> None:
"""Print user profile info."""
user = username if user is None else user
utils.clear()
userData = github_rest.getUser(user)
utils.printf.logPrint(f"{userData['name']}", LogType.BOLD)
utils.printf.logPrint(
f"""{userData['login']}
Avatar: {userData['avatar_url']}
Company: {userData['company']}
Location: {userData['location']}
Email: {userData['email']}
Followers: {userData['followers']} Following: {userData['following']}"""
)
def gists(user: str | None = None) -> None:
"""Print paginated list of user gists."""
user = username if user is None else user
userGists = github_rest.getUserGists(user)
paginatedList(userGists, 30, github_rest.printGist)
def showrepo(repo: str, user: str | None = None) -> None:
"""Print user repo data for a given repo."""
utils.clear()
user = username if user is None else user
rawMarkdown = github_rest.getReadme(user + "/" + repo)
repoText = github_rest.getRepo(user + "/" + repo)
github_rest.printRepo(repoText)
utils.printf.logPrint("README", LogType.BOLD)
printMarkdown(rawMarkdown, 1)
def showreadme(repo: str, user: str | None = None) -> None:
"""Print the readme for a given repo."""
utils.clear()
user = username if user is None else user
printMarkdown(github_rest.getReadme(user + "/" + repo))
def searchissues(searchTerm: str) -> None:
"""Search function for issues."""
issues = github_rest.search(searchTerm, context="issues")
paginatedList(issues, 30, github_rest.printIssue)
def searchrepos(searchTerm: str) -> None:
"""Search function for repos."""
searchRepos = github_rest.search(searchTerm, context="repositories")
paginatedList(searchRepos, 10, github_rest.printRepo)
def searchusers(searchTerm: str) -> None:
"""Search function for users."""
users = github_rest.search(searchTerm, context="users")
paginatedList(users, 30, github_rest.printUser)
functions = {
"exit": replexit,
"help": replhelp,
"repos": repos,
"stars": stars,
"watching": watching,
"profile": profile,
"showrepo": showrepo,
"showreadme": showreadme,
"searchissues": searchissues,
"searchrepos": searchrepos,
"searchusers": searchusers,
"gists": gists,
}
def repl() -> None:
"""Read Eval Print Loop."""
while True:
command = input(">")
try:
func, *params = command.split()
functions[func.lower()](*params)
except TypeError as error:
utils.printf.logPrint(str(error), LogType.ERROR)
except KeyError as error:
utils.printf.logPrint(str(error) + " is not a function", LogType.ERROR)
except ValueError:
pass
username = utils.getUsername()
replhelp()
repl()