1
2
3
4
5 import os
6 import ConfigParser
7
8 from lib.cuckoo.common.constants import CUCKOO_ROOT
9 from lib.cuckoo.common.exceptions import CuckooOperationalError
10 from lib.cuckoo.common.objects import Dictionary
11
13 """Configuration file parser."""
14
15 - def __init__(self, file_name="cuckoo", cfg=None):
16 """
17 @param file_name: file name without extension.
18 @param cfg: configuration file path.
19 """
20 config = ConfigParser.ConfigParser()
21
22 if cfg:
23 config.read(cfg)
24 else:
25 config.read(os.path.join(CUCKOO_ROOT, "conf", "%s.conf" % file_name))
26
27 for section in config.sections():
28 setattr(self, section, Dictionary())
29 for name, raw_value in config.items(section):
30 try:
31
32
33
34
35 if config.get(section, name) in ["0", "1"]:
36 raise ValueError
37
38 value = config.getboolean(section, name)
39 except ValueError:
40 try:
41 value = config.getint(section, name)
42 except ValueError:
43 value = config.get(section, name)
44
45 setattr(getattr(self, section), name, value)
46
47 - def get(self, section):
48 """Get option.
49 @param section: section to fetch.
50 @raise CuckooOperationalError: if section not found.
51 @return: option value.
52 """
53 try:
54 return getattr(self, section)
55 except AttributeError as e:
56 raise CuckooOperationalError("Option %s is not found in "
57 "configuration, error: %s" %
58 (section, e))
59