Пример #1
0
def save_config_file(name, config, directory=None, extension="yaml"):
    """Save config object to file name.extension in config directory"""
    if directory is not None:
        directory = os.path.join(__config_dir__, directory)
        try:
            if not os.path.isdir(directory):
                os.makedirs(directory)
        except:
            raise Exception("Cannot create config directory: " + directory)
    else:
        directory = __config_dir__
    file_name = os.path.join(directory, name + "." + extension)
    data = Serializable.dump(config)
    yaml_file = open(file_name, "w")
    yaml.dump(data, yaml_file)
    yaml_file.close()
Пример #2
0
def get_config_file(name, directory=None, cls=None, extension="yaml"):
    """
    Get config object from filename in config directory

    return: Config object or None (if file not exist)
    """
    if directory is not None:
        directory = os.path.join(__config_dir__, directory)
        if not os.path.isdir(directory):
            return None
    else:
        directory = __config_dir__
    file_name = os.path.join(directory, name + "." + extension)
    try:
        yaml_file = open(file_name, "r")
    except (FileNotFoundError, IOError):
        return None
    config = yaml.load(yaml_file)
    yaml_file.close()
    config = Serializable.load(config, cls)
    return config