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-2013 Claudio Guarnieri. 
 2  # Copyright (C) 2014-2016 Cuckoo Foundation. 
 3  # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org 
 4  # See the file 'docs/LICENSE' for copying permission. 
 5   
 6  import os 
 7  import ConfigParser 
 8   
 9  from lib.cuckoo.common.constants import CUCKOO_ROOT 
10  from lib.cuckoo.common.exceptions import CuckooOperationalError 
11  from lib.cuckoo.common.objects import Dictionary 
12   
13 -class Config:
14 """Configuration file parser.""" 15
16 - def __init__(self, file_name="cuckoo", cfg=None):
17 """ 18 @param file_name: file name without extension. 19 @param cfg: configuration file path. 20 """ 21 env = {} 22 for key, value in os.environ.items(): 23 if key.startswith("CUCKOO_"): 24 env[key] = value 25 26 config = ConfigParser.ConfigParser(env) 27 28 if cfg: 29 config.read(cfg) 30 else: 31 config.read(os.path.join(CUCKOO_ROOT, "conf", "%s.conf" % file_name)) 32 33 for section in config.sections(): 34 setattr(self, section, Dictionary()) 35 for name, raw_value in config.items(section): 36 try: 37 # Ugly fix to avoid '0' and '1' to be parsed as a 38 # boolean value. 39 # We raise an exception to goto fail^w parse it 40 # as integer. 41 if config.get(section, name) in ["0", "1"]: 42 raise ValueError 43 44 value = config.getboolean(section, name) 45 except ValueError: 46 try: 47 value = config.getint(section, name) 48 except ValueError: 49 value = config.get(section, name) 50 51 setattr(getattr(self, section), name, value)
52
53 - def get(self, section):
54 """Get option. 55 @param section: section to fetch. 56 @raise CuckooOperationalError: if section not found. 57 @return: option value. 58 """ 59 try: 60 return getattr(self, section) 61 except AttributeError as e: 62 raise CuckooOperationalError("Option %s is not found in " 63 "configuration, error: %s" % 64 (section, e))
65
66 -def parse_options(options):
67 """Parse the analysis options field to a dictionary.""" 68 ret = {} 69 for field in options.split(","): 70 if "=" not in field: 71 continue 72 73 key, value = field.split("=", 1) 74 ret[key.strip()] = value.strip() 75 return ret
76
77 -def emit_options(options):
78 """Emit the analysis options from a dictionary to a string.""" 79 return ",".join("%s=%s" % (k, v) for k, v in options.items())
80