def write_out_scripts(script_dict, path, out_path): """Writes scripts out to a given path""" names = os.path.basename(path) file_name = names.lower().replace("-", "_") + ".json" path_dir = get_directory(os.path.expanduser(path)) if out_path is not None: path_dir = os.path.expanduser(out_path) if not os.path.exists(path_dir): os.mkdir(path_dir) write_path = os.path.join(path_dir, file_name) if not (script_dict and "resources" in script_dict): print(write_path + " creation skipped because resources were empty.") return if os.path.exists(write_path): choice = clean_input(write_path + " already exists. Overwrite the script? [y/n]") if choice == "n": print(write_path + " creation skipped.") return try: with open_fw(write_path) as output_path: sorted_dict = collections.OrderedDict(script_dict.items()) json_str = json.dumps(sorted_dict, sort_keys=True, indent=4) output_path.write(json_str) print("Successfully wrote scripts to " + os.path.abspath(write_path)) output_path.close() except Exception as error: print(write_path + " could not be created. {}".format(error.message))
def test_clean_input_empty_list_ignore_empty(monkeypatch): """Test with empty list ignored.""" def mock_input(prompt): return ", , ," monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", ignore_empty=True, split_char=",") == []
def test_clean_input_string_input(monkeypatch): """Test with non-empty input.""" def mock_input(prompt): return "not empty" monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("") == "not empty"
def test_clean_input_bool(monkeypatch): """Test with correct datatype input.""" def mock_input(prompt): return "True " monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", dtype=bool) == "True"
def test_clean_input_empty_input_ignore_empty(monkeypatch): """Test with empty input ignored.""" def mock_input(prompt): return "" monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", ignore_empty=True) == ""
def test_clean_input_not_empty_list(monkeypatch): """Test with list input.""" def mock_input(prompt): return "1, 2, 3" monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", ignore_empty=True, split_char=',', dtype=None) == \ ["1", "2", "3"]
def test_clean_input_not_empty_list(monkeypatch): """Test with list input""" def mock_input(prompt): return "1, 2, 3" monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", ignore_empty=True, split_char=',', dtype=None) == ["1", "2", "3"]
def test_clean_input_empty_list_not_ignore_empty(monkeypatch): """Test with empty list not ignored.""" def mock_input(prompt): mock_input.counter += 1 if mock_input.counter <= 1: return ", , ," else: return "1 , 2, 3," mock_input.counter = 0 monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", split_char=",") == ["1", "2", "3"]
def test_clean_input_empty_input_not_ignore_empty(monkeypatch): """Test with empty input not ignored.""" def mock_input(prompt): mock_input.counter += 1 if mock_input.counter <= 1: return "" else: return "not empty" mock_input.counter = 0 monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", ignore_empty=False) == "not empty"
def test_clean_input_not_bool(monkeypatch): """Test with incorrect datatype input.""" def mock_input(prompt): mock_input.counter += 1 if mock_input.counter <= 1: return "non bool input" else: return "True " mock_input.counter = 0 monkeypatch.setattr('retriever.lib.datapackage.input', mock_input) assert clean_input("", dtype=bool) == "True"