def _write(path, data): """Write YAML helper.""" # Do it before opening file. If dump causes error it will now not # truncate the file. data = dump(data) with open(path, "w", encoding="utf-8") as outfile: outfile.write(data)
def update_ip_bans_config(path: str, ip_ban: IpBan) -> None: """Update config file with new banned IP address.""" with open(path, "a") as out: ip_ = { str(ip_ban.ip_address): { ATTR_BANNED_AT: ip_ban.banned_at.strftime("%Y-%m-%dT%H:%M:%S") } } out.write("\n") out.write(dump(ip_))
def update_ip_bans_config(path: str, ip_ban: IpBan) -> None: """Update config file with new banned IP address.""" with open(path, "a") as out: ip_ = { str(ip_ban.ip_address): { ATTR_BANNED_AT: ip_ban.banned_at.isoformat() } } out.write("\n") out.write(yaml.dump(ip_))
def update_config(path: str, dev_id: str, device: Device) -> None: """Add device to YAML configuration file.""" with open(path, "a") as out: device_config = { device.dev_id: { ATTR_NAME: device.name, ATTR_MAC: device.mac, ATTR_ICON: device.icon, "picture": device.config_picture, "track": device.track, } } out.write("\n") out.write(dump(device_config))
def mock_write(path, data): """Mock writing data.""" data = dump(data) written.append(data)
def yaml(self) -> str: """Dump blueprint as YAML.""" return yaml.dump(self.data)
def test_input(): """Test loading inputs.""" data = {"hello": yaml.Input("test_name")} assert yaml.parse_yaml(yaml.dump(data)) == data
def test_representing_yaml_loaded_data(): """Test we can represent YAML loaded data.""" files = {YAML_CONFIG_FILE: 'key: [1, "2", 3]'} with patch_yaml_files(files): data = load_yaml_config_file(YAML_CONFIG_FILE) assert yaml.dump(data) == "key:\n- 1\n- '2'\n- 3\n"
def test_dump_unicode(): """The that the dump method returns empty None values.""" assert yaml.dump({"a": None, "b": "привет"}) == "a:\nb: привет\n"
def test_dump(): """The that the dump method returns empty None values.""" assert yaml.dump({"a": None, "b": "b"}) == "a:\nb: b\n"