Changing aur-shell.py name to pyshell.py
[AurShell.git] / plugload.py
blobd1aac80a71b50b49df9635bb2fc69f837dd2196d
1 #!/usr/bin/python
2 # -*- coding: utf-8-*-
5 import os
6 import os.path
7 import sys
8 import types
9 import string
11 def load(modules_path):
12 """Load all commands modules from modules_path direcotory.
14 Returns a dictionary :
16 "class_name" : class_instance,
17 ...
19 """
20 command_modules = []
21 # adding plugins path to sys.path
22 if modules_path not in sys.path:
23 sys.path.append(modules_path)
24 # importing all modules from modules_path
25 for module_name in os.listdir(modules_path):
26 module_path = os.path.abspath(
27 string.join(modules_path, module_name))
28 (module_name, module_extension) = os.path.splitext(module_name)
29 # if it shouldn't be load
30 if os.path.islink(module_path) or \
31 module_path == __file__ or \
32 module_extension != ".py":
33 continue
34 for key in sys.modules.keys():
35 if key == module_name:
36 del sys.modules[key]
37 module = __import__(module_name)
38 command_modules.append({"obj": module_name, "name": module,})
39 # search for class in each module
40 module_objs = {}
41 for module in command_modules:
42 for obj in dir(module["name"]):
43 # TODO
44 # better class serch 2008-01-27 16:56:42
45 if not obj.startswith("_") and \
46 type(getattr(module['name'], obj)) == types.TypeType:
47 # create instance for each class
48 module_objs[obj] = \
49 getattr(module['name'], obj)()
50 return module_objs