-
Notifications
You must be signed in to change notification settings - Fork 0
/
dayturnover.py
152 lines (114 loc) · 3.89 KB
/
dayturnover.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
# coding=utf-8
import datetime
from typing import Sequence, Dict, Tuple, Callable, Optional, TypeVar
import hexchat
__module_name__ = "DayTurnover"
__module_author__ = "linuxdaemon"
__module_version__ = "0.1.0"
__module_description__ = "Adds a 'Day changed' message to buffers on day turnovers"
WordList = Sequence[Optional[str]]
CommandCallback = Callable[[WordList, WordList], None]
Func = TypeVar('Func')
CmdDecoReturn = Callable[[Func], Func]
DEFAULT_FMT = "Day changed to %d %b %Y"
DEFAULT_INTERVAL = 30
timer_hook = None
state = 0
commands = dict() # type: Dict[str, Tuple[CommandCallback, int]]
def getpref(name, default):
val = hexchat.get_pluginpref("{}_{}".format(__module_name__, name))
if val is None:
return default
return val
def setpref(name, value):
return hexchat.set_pluginpref("{}_{}".format(__module_name__, name), value)
def timer_cb(userdata):
global state
now = datetime.datetime.now()
if now.hour == 23 and now.minute == 59:
state = 1
elif now.hour == 0 and now.minute == 0 and state == 1:
state = 2
for chan in hexchat.get_list("channels"):
chan.context.prnt(now.strftime(getpref("format", DEFAULT_FMT)))
return True
def start_timer():
global timer_hook
assert timer_hook is None, \
"Attempted to start timer that was already running"
timer_hook = hexchat.hook_timer(
getpref("interval", DEFAULT_INTERVAL) * 1000, timer_cb
)
def stop_timer():
global timer_hook
assert timer_hook is not None, \
"Attempted to stop timer that wasn't running"
hexchat.unhook(timer_hook)
timer_hook = None
def command(*names: str, min_args: int = 0) -> CmdDecoReturn:
def _command(func: Func):
for name in names:
commands[name] = (func, name, min_args)
return func
return _command
@command("on", "enable")
def enable(word: Sequence[str], word_eol: Sequence[str]) -> None:
setpref("enabled", True)
if timer_hook is None:
start_timer()
print(__module_name__, "enabled")
else:
print(__module_name__, "already enabled")
@command("off", "disable")
def disable(word: Sequence[str], word_eol: Sequence[str]) -> None:
setpref("enabled", False)
global timer_hook
if timer_hook is not None:
stop_timer()
print(__module_name__, "disabled")
else:
print(__module_name__, "not enabled")
@command("format")
def fmt(word: Sequence[str], word_eol: Sequence[str]) -> None:
if word_eol and word_eol[0]:
if not setpref("format", word_eol[0]):
print("Failed to set format")
return
print(__module_name__, "format:", getpref("format", DEFAULT_FMT))
@command("interval")
def interval(word: Sequence[str], word_eol: Sequence[str]) -> None:
if word and word[0]:
try:
intv = float(word[0])
except ValueError:
print("Invalid interval value")
return
if not setpref("interval", intv):
print("Failed to set interval")
return
if timer_hook is not None:
stop_timer()
start_timer()
print(
__module_name__, "interval:", getpref("interval", DEFAULT_INTERVAL),
"seconds"
)
def cmd_cb(word, word_eol, userdata):
if len(word) < 2:
hexchat.command("HELP {}".format(word[0]))
else:
subcmd = word[1].lower()
if subcmd in commands:
# TODO support min_args properly
commands[subcmd][0](word[2:], word_eol[2:])
else:
hexchat.command("HELP {}".format(word[0]))
return hexchat.EAT_ALL
hexchat.hook_unload(lambda userdata: print(__module_name__, "plugin unloaded"))
hexchat.hook_command(
"DAYCHANGE", cmd_cb,
help="DAYCHANGE [ON|OFF|FORMAT <format string>|INTERVAL <seconds>]"
)
if getpref("enabled", True):
start_timer()
print(__module_name__, "plugin loaded")