-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmsg_parser.py
More file actions
29 lines (23 loc) · 770 Bytes
/
msg_parser.py
File metadata and controls
29 lines (23 loc) · 770 Bytes
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
import re
import logging
def split_msg(msg):
"""
splits msg
example: this: is, some msg -> [this, is, some, msg]
"""
return re.findall(r"[^\s]+", msg)
def trim_msg(to_trim, full_msg):
"""
trims full_msg to get just command without following to_trim
returns empty string when didn't found to_trim
"""
splited_msg = split_msg(full_msg)
command = ''
try:
if splited_msg and full_msg.startswith(to_trim):
to_trim_impl = re.compile(to_trim + r'\s*').findall(full_msg)[0]
command = full_msg.replace(to_trim_impl, '', 1)
except Exception as e:
logging.getLogger(__name__).error(f'exception caught while parsing msg: {type(e).__name__}: {e}')
return ''
return command