summaryrefslogtreecommitdiff
path: root/rcon.py
blob: 0185f12b5455cb0eb846b85743e89230cbfbe3af (plain)
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
import urwid, os, subprocess, fcntl, glob

try:
	from local_config import *
except ImportError:
	SERVER_PATH = "testing/home@*"
	PIPE_PATH = "testing/home@%s/slacker/pipe"
	LOG_COMMAND = ["/usr/bin/env", "python3", "testing/log_simulator.py"]



class JournalViewer:
	def __init__(self, status, loop):
		self.status = status
		self.loop = loop

		self.list_walker = urwid.SimpleFocusListWalker([])
		self.list_box = urwid.ListBox(self.list_walker)

		self.root = self.list_box
		self.proc = None

		self.following = True

	# FIXME: call this whenever the user scrolls through the log
	def update(self):
		lc = len(self.list_walker)

		if self.following:
			self.list_box.set_focus(lc - 1)

		self.status.set_text("%d/%d line%s, [F]ollow: %s" % (
			self.list_box.focus_position + 1,
			lc, "" if lc == 1 else "s",
			"on" if self.following else "off"))

	def write(self, text):
		for line in str(text).split("\n"):
			self.list_walker.append(urwid.Text(line))
		self.update()

	def proc_open(self, argv):
		if self.proc is not None:
			self.loop.remove_watch_file(self.pipefd)
			# FIXME: closing the fd fucks up the event loop
			#os.close(self.pipefd) 
			self.proc.kill()

		self.pipefd, writefd = os.pipe()
		fl = fcntl.fcntl(self.pipefd, fcntl.F_GETFL)
		fcntl.fcntl(self.pipefd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
		self.pipe = os.fdopen(self.pipefd)

		self.proc = subprocess.Popen(argv, stdout=writefd, stderr=writefd)
		self.loop.watch_file(self.pipefd, self.proc_write)

	def proc_write(self):
		self.write(self.pipe.read())



class ServerSelectorPopup(urwid.WidgetWrap):
	def __init__(self, parent, console):
		self.parent = parent
		self.console = console

		buttons = list()
		for server in ServerSelector.list_servers():
			button = urwid.Button(server)
			urwid.connect_signal(button, "click", self.click)
			buttons.append(button)

		filler = urwid.Filler(urwid.Pile(buttons))
		super().__init__(urwid.AttrWrap(filler, "popup"))

	def click(self, button):
		server = button.label # jfc
		self.parent.close_pop_up()
		self.console.select_server(server)

class ServerSelector(urwid.PopUpLauncher):
	def list_servers():
		return [path.split("@")[1]
		        for path in glob.glob(SERVER_PATH)]

	def __init__(self, console):
		self.console = console
		self.button = urwid.Button("...")
		super().__init__(self.button)
		urwid.connect_signal(self.button, "click", lambda _: self.open_pop_up())

	def create_pop_up(self):
		pop_up = ServerSelectorPopup(self, self.console)
		return pop_up

	def get_pop_up_parameters(self):
		return {'left':0, 'top':1, 'overlay_width':20, 'overlay_height':5}



class Console:
	def __init__(self, loop):
		self.loop = loop

		self.edit_pre = ServerSelector(self)
		self.edit_pre_attrmap = urwid.AttrMap(self.edit_pre, "button",
		                                      "button focus")
		self.edit = urwid.Edit()
		self.edit_attrmap = urwid.AttrMap(self.edit, "edit", "edit focus")
		self.edit_box = urwid.Columns(
			[self.edit_pre_attrmap, ("weight", 6, self.edit_attrmap)]
		)

		self.status = urwid.Text("", align="center")
		self.status_attrmap = urwid.AttrMap(self.status, "status", None)

		self.viewer = JournalViewer(self.status, self.loop)

		self.root = urwid.Frame(self.viewer.root,
		                        header=self.status_attrmap,
		                        footer=self.edit_box)

		# Focus on the edit box
		self.root.focus_position = "footer"
		self.edit_box.focus_position = 1

		servers = ServerSelector.list_servers()
		if len(servers) == 0:
			print("There are no servers in '%s'" % SERVER_PATH)
			exit(1)
		self.select_server("trench" if "trench" in servers else servers[0])

	def unhandled_input(self, text):
		if text == "tab":
			if self.root.focus_position == "body":
				self.root.focus_position = "footer"
				self.edit_box.focus_position = 1
			else:
				self.root.focus_position = "body"
		elif text == "enter":
			command = self.edit.get_edit_text()
			self.edit.set_edit_text("")
			self.execute(command)
		elif text == "f":
			self.viewer.following = not self.viewer.following
			self.viewer.update()

	def select_server(self, server):
		self.server = server
		self.viewer.write("*** Now viewing logs from %s ***" % server)

		self.edit_pre.button.set_label(server)
		argv = [arg % server if "%s" in arg else arg for arg in LOG_COMMAND]
		self.viewer.proc_open(argv)

	def execute(self, command):
		self.viewer.write(PIPE_PATH % self.server)
		with open(PIPE_PATH % self.server, "w") as fp:
			fp.write(command + "\n")



palette = [
	("button",       "white",       "dark red"),
	("button focus", "white, bold", "light red"),
	("status",       "black",       "white"),
	("edit focus",   "white",       "black"),
	("edit",         "white",       "dark gray"),
	("popup",        "white",       "dark red"),
]

loop = urwid.MainLoop(None, palette, pop_ups=True)
console = Console(loop)
loop.widget = console.root
loop.unhandled_input = console.unhandled_input

try:
	loop.run()
except KeyboardInterrupt:
	exit()