This repository was archived by the owner on Feb 10, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathconfig_generator.py
More file actions
78 lines (66 loc) · 2.96 KB
/
Copy pathconfig_generator.py
File metadata and controls
78 lines (66 loc) · 2.96 KB
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
import os, json, requests;
from time import sleep;
from response_handler import ResponseHandler
class ConfigGenerator:
def __init__(self):
if not os.path.exists("config.json"):
os.system('touch config.json')
os.system('echo "{}" >> config.json')
jsonFile = open("config.json", "r")
self.current_config = json.load(jsonFile)
jsonFile.close()
self.current_config = json.load(open('./config.json'))
self.set_config()
self.check_config_and_launch()
def set_config(self):
config = {
"github_username": self.ask_username(self.current_config.get('github_username')),
"github_bearer_token": self.ask_api_key(self.current_config.get('github_bearer_token')),
"max_nb_notifications": self.ask_max_nb(self.current_config.get('max_nb_notifications')),
"show_security_updates": self.ask_security_updates(self.current_config.get('show_security_updates'))
}
jsonFile = open("config.json", "w+")
jsonFile.write(json.dumps(config))
def ask_username(self, current_value):
os.system('clear')
print('Enter your github username:')
print(f'Current: {current_value}, press Enter to skip')
return self.return_value(input(), current_value)
def ask_api_key(self, current_value):
os.system('clear')
print('Enter your API key: (you can get an API key at \033[1;31mhttps://github.com/settings/tokens\u001b[0m)')
print(f'Current: {current_value}, press Enter to skip')
return self.return_value(input(), current_value)
def ask_max_nb(self, current_value):
os.system('clear')
print('How many notifications do you want to see maximum?')
print(f'Current: {current_value}, press Enter to skip')
return self.return_value(input(), current_value)
def ask_security_updates(self, current_value):
os.system('clear')
print('Do you want to see security updates? Y/N')
print(f'Current: {current_value}, press Enter to skip')
answer = input().upper()
while answer != 'Y' and answer != 'N' and answer != '':
answer = input().upper()
return self.return_value(answer, current_value)
def return_value(self, value, current_value):
if value == '':
return current_value
else:
return value
def check_config_and_launch(self):
self.load_json()
response = requests.get("https://api.github.com/notifications", auth=(self.current_config['github_username'], self.current_config['github_bearer_token'])).json()
if isinstance(response, dict) and 'Bad credentials' in response.values():
print("Wrong credentials, starting config.")
sleep(1)
ConfigGenerator()
else:
show_security_updates = self.current_config['max_nb_notifications'] == "Y"
max_nb = int(self.current_config['max_nb_notifications'])
ResponseHandler(api_response = response, max_nb = max_nb, show_security_updates = show_security_updates)
def load_json(self):
jsonFile = open("config.json", "r")
self.current_config = json.load(jsonFile)
jsonFile.close()