forked from kantinen/kantinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kantinfo-order.py
executable file
·60 lines (49 loc) · 1.69 KB
/
kantinfo-order.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
#!/usr/bin/env python3
# Copyright © 2014-2016 Infoskærms-gruppen <[email protected]>
#
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the COPYING file for more details.
# This is the control script for the informational monitors in the DIKU
# canteen, but it can be (and is) used in other places as well. See the
# README.md file to find out how to use it.
'''
Send an order to a running infoscreen.
'''
import sys
import os.path
import socket
import readline
_base_dir = os.path.dirname(__file__)
_socket_filename = os.path.join(_base_dir, 'kantinfo.sock')
def send_orders(args):
'''
Send standard in orders based on the list of string arguments in `args`.
'''
if '--help' in args:
_print_help()
sys.exit(0)
with socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) as client:
try:
client.connect(_socket_filename)
except FileNotFoundError:
print('Could not find socket.')
sys.exit(1)
for line in sys.stdin:
line = line.rstrip()
order = line.encode('utf-8')
if len(order) > 1024:
print('Datagram too large.')
client.send(order)
def _print_help():
print('kantinfo from git; see README.md for instructions')
print()
print('Usage:')
print(' ./kantinfo-order.py [OPTION...]')
print()
print('Read lines from standard in and send them to a running kantinfo.py process.')
print()
print('Options:')
print(' --help Print this text.')
if __name__ == '__main__':
send_orders(sys.argv[1:])