def test_load_success__skips_none(self): data = {"dots": True, "scene": None} expected = {"dots": True} mocked_open = mock_open(read_data=json.dumps(data)) with patch(OPEN_TARGET, mocked_open) as _: actual = config_load(DUMMY_FILE) self.assertDictEqual(expected, actual)
def main(): """ Program entry point """ notify = Notify() # Process parameters targets, config, directives = get_parameters() # Allow colour printing to cmd and PowerShell ascii_colour_init(autoreset=True) # Display version information and exit if requested if directives.get("version") is True: print("mnamer v%s" % VERSION) return # Detect file(s) notify.heading("Starting mnamer") for path in [ ".mnamer.json", normpath("%s/mnamer.json" % user_config_dir()), normpath("%s/.mnamer.json" % expanduser("~")), directives["config_load"], ]: if not path: continue try: config = merge_dicts(config_load(path), config) notify.success("success loading config from %s" % path, True) except MnamerConfigException: if config.get("verbose") is True: notify.verbose("skipped loading config from %s" % path, True) # Backfill configuration with defaults config = merge_dicts(CONFIG_DEFAULTS, config) # Save config to file if requested if directives.get("config_save"): path = directives["config_save"] try: config_save(path, config) notify.success("success saving to %s" % directives["config_save"]) except (TypeError, IOError): notify.error("error saving config to %s" % path) # Display config information if config["verbose"]: notify.heading("Configuration") for key, value in config.items(): notify.info("%s: %s" % (key, None if value == "" else value), True) # Process Files user_media = directives.get("media") test_run = directives.get("test_run") id_key = directives.get("id") process_files(targets, user_media, test_run, id_key, **config)
def test_load_fail__io(self): mocked_open = mock_open() with patch(OPEN_TARGET, mocked_open) as patched_open: patched_open.side_effect = IOError with self.assertRaises(MnamerConfigException): config_load(DUMMY_FILE)
def test_environ_substitution(self): os.environ["HOME"] = DUMMY_DIR with patch(OPEN_TARGET, mock_open(read_data="{}")) as mock_file: config_load("$HOME/config.json") mock_file.assert_called_with(DUMMY_DIR + "/config.json", mode="r")
def test_load_fail__invalid_json(self): mocked_open = mock_open(read_data=BAD_JSON) with patch(OPEN_TARGET, mocked_open) as patched_open: patched_open.side_effect = TypeError with self.assertRaises(MnamerConfigException): config_load(DUMMY_FILE)