lib.core.config

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

# Source Code for Module lib.core.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 ConfigParser
     7
     8 -class Config:
     9 -    def __init__(self, cfg):
    10          """@param cfg: configuration file."""
    11          config = ConfigParser.ConfigParser(allow_no_value=True)
    12          config.read(cfg)
    13
    14          for section in config.sections():
    15              for name, raw_value in config.items(section):
    16                  if name == "file_name":
    17                      value = config.get(section, name).decode("utf8")
    18                  elif name == "options":
    19                      value = self.parse_options(config.get(section, name))
    20                  else:
    21                      try:
    22                          value = config.getboolean(section, name)
    23                      except ValueError:
    24                          try:
    25                              value = config.getint(section, name)
    26                          except ValueError:
    27                              value = config.get(section, name)
    28                  setattr(self, name, value)
    29
    30          # Just make sure the options field is available.
    31          if not hasattr(self, "options"):
    32              self.options = {}
    33
    34 -    def parse_options(self, options):
    35          """Get analysis options.
    36          @return: options dict.
    37          """
    38          # The analysis package can be provided with some options in the
    39          # following format:
    40          #   option1=value1,option2=value2,option3=value3
    41          ret = {}
    42          for field in options.split(","):
    43              if "=" not in field:
    44                  continue
    45
    46              key, value = field.split("=", 1)
    47              ret[key.strip()] = value.strip()
    48          return ret
    49