fcb449e0e74763dfd3f77371f0f90769bc861c9f
1#
2# load_parsers.py
3#
4# Search for and load files which parse logs for particular services
5#
6
7import imp
8import os
9import glob
10
11parser_dir = "/usr/share/logparse/"
12main_module = "__init__.py"
13
14import logging
15logger = logging.getLogger(__name__)
16
17def search():
18 logger.debug("Searching for parsers in {0}".format(parser_dir))
19 parsers = []
20 parser_candidates = glob.glob(os.path.join(os.path.dirname(parser_dir), "*.py"))
21 for p in parser_candidates:
22 location = os.path.join(parser_dir, p)
23 if not os.path.isdir(parser_dir) or not main_module + '.py' in os.listdir(location):
24 continue
25 info = imp.find_module(main_module, [location])
26 parsers.append({"name": p, "info": info})
27 return parsers
28
29def load(parser):
30 logger.debug("Loading {0}".format(parser["name"]))
31 return imp.load_module(parser["name"], *parser["info"])
32