# (c) Copyright 2014,2015 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from freezer import utils try: import configparser except ImportError: import ConfigParser as configparser import logging import os import re class Config: @staticmethod def parse(config_path): if config_path: if not os.path.exists(config_path): logging.error("[*] Critical Error: Configuration file {0} not" " found".format(config_path)) raise Exception("Configuration file {0} not found !".format( config_path)) config = configparser.SafeConfigParser() config.read([config_path]) sections = config.sections() storages = [] default_options = {} for section in sections: dict = {} for option in config.options(section): option_value = config.get(section, option) if option_value in ('False', 'None'): option_value = False dict[option] = option_value if section.startswith("storage:"): storages.append(dict) else: default_options.update(dict) return Config(default_options, storages) def __init__(self, default, storages): """ :param default: :type default: dict :param storages: :type storages: list[dict] :return: """ self.default = default self.storages = storages EXPORT = re.compile(r"^\s*export\s+([^=^#^\s]+)\s*=\s*([^#^\n]*)\s*$", re.MULTILINE) INI = re.compile(r"^\s*([^=#\s]+)\s*=[\t]*([^#\n]*)\s*$") def osrc_parse(lines): """ :param lines: :type lines: str :return: """ return find_all(EXPORT, lines) def ini_parse(lines): return find_all(INI, lines) def find_all(regex, lines): return dict([(k.strip(), utils.dequote(v.strip())) for k, v in regex.findall(lines)])