Package lib :: Package cuckoo :: Package common :: Module config
[hide private]
[frames] | no frames]

Source Code for Module lib.cuckoo.common.config

 1  # Copyright (C) 2010-2015 Cuckoo Foundation. 
 2  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org 
 3  # See the file 'docs/LICENSE' for copying permission. 
 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   
12 -class Config:
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 # Ugly fix to avoid '0' and '1' to be parsed as a 32 # boolean value. 33 # We raise an exception to goto fail^w parse it 34 # as integer. 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