18
18
from cloudbot import clients
19
19
from cloudbot .client import Client
20
20
from cloudbot .config import Config
21
- from cloudbot .event import Event , CommandEvent , RegexEvent , EventType
21
+ from cloudbot .event import CommandEvent , Event , EventType , RegexEvent
22
22
from cloudbot .hook import Action
23
23
from cloudbot .plugin import PluginManager
24
- from cloudbot .reloader import PluginReloader , ConfigReloader
25
- from cloudbot .util import database , formatting , async_util
24
+ from cloudbot .reloader import ConfigReloader , PluginReloader
25
+ from cloudbot .util import async_util , database , formatting
26
26
from cloudbot .util .mapping import KeyFoldDict
27
27
28
28
logger = logging .getLogger ("cloudbot" )
@@ -58,28 +58,34 @@ def clean_name(n):
58
58
:type n: str
59
59
:rtype: str
60
60
"""
61
- return re .sub (' [^A-Za-z0-9_]+' , '' , n .replace (" " , "_" ))
61
+ return re .sub (" [^A-Za-z0-9_]+" , "" , n .replace (" " , "_" ))
62
62
63
63
64
64
def get_cmd_regex (event ):
65
65
conn = event .conn
66
66
is_pm = event .chan .lower () == event .nick .lower ()
67
- command_prefix = re .escape (conn .config .get (' command_prefix' , '.' ))
67
+ command_prefix = re .escape (conn .config .get (" command_prefix" , "." ))
68
68
conn_nick = re .escape (event .conn .nick )
69
69
cmd_re = re .compile (
70
70
r"""
71
71
^
72
72
# Prefix or nick
73
73
(?:
74
- (?P<prefix>[""" + command_prefix + r"""])""" + ('?' if is_pm else '' ) + r"""
74
+ (?P<prefix>["""
75
+ + command_prefix
76
+ + r"""])"""
77
+ + ("?" if is_pm else "" )
78
+ + r"""
75
79
|
76
- """ + conn_nick + r"""[,;:]+\s+
80
+ """
81
+ + conn_nick
82
+ + r"""[,;:]+\s+
77
83
)
78
84
(?P<command>\w+) # Command
79
85
(?:$|\s+)
80
86
(?P<text>.*) # Text
81
87
""" ,
82
- re .IGNORECASE | re .VERBOSE
88
+ re .IGNORECASE | re .VERBOSE ,
83
89
)
84
90
return cmd_re
85
91
@@ -126,7 +132,7 @@ def __init__(self, loop=asyncio.get_event_loop()):
126
132
self .memory = collections .defaultdict ()
127
133
128
134
# declare and create data folder
129
- self .data_dir = os .path .abspath (' data' )
135
+ self .data_dir = os .path .abspath (" data" )
130
136
if not os .path .exists (self .data_dir ):
131
137
logger .debug ("Data folder not found, creating." )
132
138
os .mkdir (self .data_dir )
@@ -141,11 +147,14 @@ def __init__(self, loop=asyncio.get_event_loop()):
141
147
self .config_reloading_enabled = reloading_conf .get ("config_reloading" , True )
142
148
143
149
# this doesn't REALLY need to be here but it's nice
144
- self .user_agent = self .config .get ('user_agent' , 'CloudBot/3.0 - CloudBot Refresh '
145
- '<https://github.com/CloudBotIRC/CloudBot/>' )
150
+ self .user_agent = self .config .get (
151
+ "user_agent" ,
152
+ "CloudBot/3.0 - CloudBot Refresh "
153
+ "<https://github.com/CloudBotIRC/CloudBot/>" ,
154
+ )
146
155
147
156
# setup db
148
- db_path = self .config .get (' database' , ' sqlite:///cloudbot.db' )
157
+ db_path = self .config .get (" database" , " sqlite:///cloudbot.db" )
149
158
self .db_engine = create_engine (db_path )
150
159
self .db_factory = sessionmaker (bind = self .db_engine )
151
160
self .db_session = scoped_session (self .db_factory )
@@ -201,15 +210,14 @@ def register_client(self, name, cls):
201
210
202
211
def create_connections (self ):
203
212
""" Create a BotConnection for all the networks defined in the config """
204
- for config in self .config [' connections' ]:
213
+ for config in self .config [" connections" ]:
205
214
# strip all spaces and capitalization from the connection name
206
- name = clean_name (config [' name' ])
207
- nick = config [' nick' ]
215
+ name = clean_name (config [" name" ])
216
+ nick = config [" nick" ]
208
217
_type = config .get ("type" , "irc" )
209
218
210
219
self .connections [name ] = self .get_client (_type )(
211
- self , _type , name , nick , config = config ,
212
- channels = config ['channels' ]
220
+ self , _type , name , nick , config = config , channels = config ["channels" ]
213
221
)
214
222
logger .debug ("[%s] Created connection." , name )
215
223
@@ -283,7 +291,9 @@ async def _init_routine(self):
283
291
conn .active = True
284
292
285
293
# Connect to servers
286
- await asyncio .gather (* [conn .try_connect () for conn in self .connections .values ()], loop = self .loop )
294
+ await asyncio .gather (
295
+ * [conn .try_connect () for conn in self .connections .values ()], loop = self .loop
296
+ )
287
297
logger .debug ("Connections created." )
288
298
289
299
# Run a manual garbage collection cycle, to clean up any unused objects created during initialization
@@ -294,7 +304,7 @@ def load_clients(self):
294
304
Load all clients from the "clients" directory
295
305
"""
296
306
scanner = Scanner (bot = self )
297
- scanner .scan (clients , categories = [' cloudbot.client' ])
307
+ scanner .scan (clients , categories = [" cloudbot.client" ])
298
308
299
309
def process (self , event ):
300
310
"""
@@ -348,12 +358,16 @@ def add_hook(hook, _event):
348
358
cmd_match = get_cmd_regex (event ).match (event .content )
349
359
350
360
if cmd_match :
351
- command_prefix = event .conn .config .get (' command_prefix' , '.' )
352
- prefix = cmd_match .group (' prefix' ) or command_prefix [0 ]
353
- command = cmd_match .group (' command' ).lower ()
354
- text = cmd_match .group (' text' ).strip ()
361
+ command_prefix = event .conn .config .get (" command_prefix" , "." )
362
+ prefix = cmd_match .group (" prefix" ) or command_prefix [0 ]
363
+ command = cmd_match .group (" command" ).lower ()
364
+ text = cmd_match .group (" text" ).strip ()
355
365
cmd_event = partial (
356
- CommandEvent , text = text , triggered_command = command , base_event = event , cmd_prefix = prefix
366
+ CommandEvent ,
367
+ text = text ,
368
+ triggered_command = command ,
369
+ base_event = event ,
370
+ cmd_prefix = prefix ,
357
371
)
358
372
if command in self .plugin_manager .commands :
359
373
command_hook = self .plugin_manager .commands [command ]
@@ -373,7 +387,9 @@ def add_hook(hook, _event):
373
387
command_event = cmd_event (hook = command_hook )
374
388
add_hook (command_hook , command_event )
375
389
else :
376
- commands = sorted (command for command , plugin in potential_matches )
390
+ commands = sorted (
391
+ command for command , plugin in potential_matches
392
+ )
377
393
txt_list = formatting .get_text_list (commands )
378
394
event .notice ("Possible matches: {}" .format (txt_list ))
379
395
@@ -390,12 +406,16 @@ def add_hook(hook, _event):
390
406
regex_match = regex .search (event .content )
391
407
if regex_match :
392
408
regex_matched = True
393
- regex_event = RegexEvent (hook = regex_hook , match = regex_match , base_event = event )
409
+ regex_event = RegexEvent (
410
+ hook = regex_hook , match = regex_match , base_event = event
411
+ )
394
412
if not add_hook (regex_hook , regex_event ):
395
413
# The hook has an action of Action.HALT* so stop adding new tasks
396
414
break
397
415
398
416
tasks .sort (key = lambda t : t [0 ].priority )
399
417
400
418
for _hook , _event in tasks :
401
- async_util .wrap_future (self .plugin_manager .launch (_hook , _event ))
419
+ async_util .wrap_future (
420
+ self .plugin_manager .launch (_hook , _event ), loop = self .loop
421
+ )
0 commit comments