-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom_manager.py
More file actions
28 lines (20 loc) · 889 Bytes
/
com_manager.py
File metadata and controls
28 lines (20 loc) · 889 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
import re
import os
import importlib.machinery
from command import Command
class Commanager:
def __init__(self, path):
""" When the class is instantiated, it loads all commands (<name>_com_.py) in the directory pointed by 'path' """
self.path = os.path.expanduser(path)
self.commands = dict()
files = os.listdir(self.path)
for f in files:
match = re.match(r'^(?P<name>\w+)_com_\.py$', f)
if match: self.commands[match.group("name")] = \
getattr(importlib.machinery.SourceFileLoader(\
match.group("name"), self.path + '/' + f).load_module(), match.group("name"))
def mkcom(self, name, args, sender, receiver):
try:
return Command(name, self.commands[name], args, sender, receiver)
except KeyError:
return None