forked from sbp/duxlot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
64 lines (51 loc) · 1.56 KB
/
functions.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
# Copyright 2012, Sean B. Palmer
# Code at http://inamidst.com/duxlot/
# Apache License 2.0
commands = {}
def register(name, function):
"Register a function as an IRC command"
if name in commands:
print("Warning: Duplicate command: %s" % name)
commands[name] = function
def command(function):
"Decorate a function as an IRC command"
def canonical(name):
return name.strip("_").replace("_", "-")
name = canonical(function.__name__)
register(name, function)
return function
def named(*names):
"Decorate a function as an IRC command, with custom names"
def decorate(function):
for name in names:
register(name, function)
return function
return decorate
events = {"high": {}, "medium": {}, "low": {}}
def event(name, concurrent=False):
"Decorate a function to match IRC events"
def decorate(function):
function.concurrent = concurrent
events["high"].setdefault(name, [])
events["high"][name].append(function)
return function
return decorate
# decorators = (command, named, event)
startups = []
def startup(function):
"Decorate a function to run on startup"
startups.append(function)
return function
builders = []
def builder(function):
"Decorate a function to augment an enviroment"
builders.append(function)
return function
def clear():
"Clear registered IRC commands and events"
commands.clear()
events["high"].clear()
events["medium"].clear()
events["low"].clear()
del startups[:]
del builders[:]