-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator.py
executable file
·141 lines (108 loc) · 4.38 KB
/
indicator.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
from datetime import datetime
import os
import re
from subprocess import Popen, PIPE, STDOUT
from threading import Thread
import time
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AyatanaAppIndicator3', '0.1')
from gi.repository import Gtk as gtk, AyatanaAppIndicator3 as appindicator
from extras.jenkins_desktop_notify import JenkinsChecker, JenkinsNotifier
#from autoupdate import AutoUpdate
#from extras.hours import HoursReporter
class Indicator:
devs = {}
selected_names = []
selected_emails = []
select_time = None
ind = None
menu = None
def __init__(self):
self.load_devs()
current_git_username = Popen(["git", "config", "--global", "user.name"], stdout=PIPE).communicate()[0].decode('utf-8').strip()
self.selected_names = list(filter(lambda name: name in self.devs, [name.strip() for name in current_git_username.split(",")]))
self.menu = self.build_menu(current_git_username)
def load_devs(self):
with open("developers.txt") as f:
for line in f:
name, email = line.partition(":")[::2]
self.devs[name.strip()] = email.strip()
def is_selected(self, name):
return name in self.selected_names
def add(self, name):
self.selected_names.append(name)
self.select_time = datetime.now()
def remove(self, name):
self.selected_names.remove(name)
def reset_git_username(self):
os.system("git config --global --unset user.name")
os.system("git config --global --unset user.email")
self.ind.set_label('', '')
def name_selected(self, widget):
name = widget.get_label()
if self.is_selected(name):
self.remove(name)
else:
self.add(name)
self.selected_emails = [self.devs[name] for name in self.selected_names]
git_username = ", ".join(self.selected_names)
git_email = ", ".join(self.selected_emails)
self.reset_git_username()
os.system("git config --global user.name '%s'" % git_username)
os.system("git config --global user.email '%s'" % git_email)
self.ind.set_label(git_username, git_username)
def _add_name_item(self, menu, name):
menu_item = gtk.CheckMenuItem.new_with_label(name)
if self.is_selected(name):
menu_item.set_active(True)
menu.append(menu_item)
menu_item.connect("activate", self.name_selected)
def _add_action_item(self, menu, text, handler):
menu_item = gtk.MenuItem.new_with_label(text)
menu.append(menu_item)
menu_item.connect("activate", handler)
def quit(self, widget=None):
gtk.main_quit()
def restart(self, widget=None):
print("Restarting")
os.execl(__file__, __file__)
def build_menu(self, current_git_username):
self.ind = appindicator.Indicator.new("git-indicator", "krb-valid-ticket", appindicator.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status(appindicator.IndicatorStatus.ACTIVE)
self.ind.set_label(current_git_username, current_git_username)
menu = gtk.Menu()
for name in sorted(self.devs): self._add_name_item(menu, name)
separator = gtk.SeparatorMenuItem()
menu.append(separator)
self._add_action_item(menu, 'Restart', self.restart)
self._add_action_item(menu, 'Quit', self.quit)
menu.show_all()
self.ind.set_menu(menu)
return menu
def uncheck_all_names(self):
for menu_item in self.menu.get_children():
if isinstance(menu_item, gtk.CheckMenuItem):
menu_item.set_active(False)
class UserReset(Thread):
def __init__(self, indicator):
super(UserReset, self).__init__(name='UserReset')
self.setDaemon(True)
self.indicator = indicator
def reset_user_at_midnight(self):
hour = datetime.now().hour
if hour == 0:
print("Reset git user at midnight %s" % datetime.now())
self.indicator.reset_git_username()
self.indicator.uncheck_all_names()
def run(self):
while True:
self.reset_user_at_midnight()
time.sleep(60*55)
if __name__ == "__main__":
indicator = Indicator()
UserReset(indicator).start()
#AutoUpdate(indicator).start()
#HoursReporter(indicator).start()
JenkinsNotifier(JenkinsChecker()).start()
gtk.main()