def save_manifest( path: str, manifest_path: str, ) -> None: """Save a manifest of the given 'path' into the given 'manifest_path'. Warning: the paths should be checked before call this function. :param path: The path to explore. :param manifest_path: The path of the output manifest (it would be overriding). """ # Assert the paths are correct and absolutely path = os.path.abspath(os.path.expanduser(path)) manifest_path = os.path.abspath(os.path.expanduser(manifest_path)) # Get all the child files and folders recursively children = _get_children_of_path(path) # Prepare the content entries after explore the child elements content = [] for child in children: tags = get_finder_tags_for_path(child) if not tags == []: content.append(TagAssociation(child, tags)) # Finally, create the manifest and write it to a file manifest = Manifest(content) # Save the manifest manifest.save(manifest_path)
def test_save_and_dump_manifest(self): """Test the full process to save and dump a manifest.""" with tempfile.TemporaryDirectory() as sample_node: # Generate random stuff _generate_random_folders_tree(sample_node) # Select some random files and folders children = _get_children_of_path(sample_node) tagged_children = children[: (len(children) // 3)] # Children subset # Create a list with a random tag name to each tagged child tags = [] for _ in tagged_children: tags.append( "".join(random.choice(string.ascii_letters) for i in range(15)) ) # Random tag name # Tag children additional_tag_1 = "Multiple of eleven" additional_tag_2 = "Multiple of 11" for i in range(0, len(tagged_children)): add_finder_tag_for_path(tagged_children[i], tags[i]) if not i % 11: add_finder_tag_for_path(tagged_children[i], additional_tag_1) add_finder_tag_for_path(tagged_children[i], additional_tag_2) # Save the manifest manifest_path = os.path.join(sample_node, MANIFEST_FILE_NAME) save_manifest(sample_node, manifest_path) # Remove all the tags for child in tagged_children: rm_all_finder_tags_for_path(child) # Now dump the saved manifest to restore the previous state dump_manifest(manifest_path, sample_node) # Check that the machine name is correctly stored with open(manifest_path, "r") as infile: file_manifest = yaml.load(infile, Loader=yaml.FullLoader) self.assertEqual(file_manifest.machine, socket.gethostname()) # Get current state and check against the previous one for i in range(0, len(tagged_children)): res_tags = get_finder_tags_for_path(tagged_children[i]) self.assertTrue(tags[i] in res_tags) if not i % 11: self.assertTrue(additional_tag_1 in res_tags) self.assertTrue(additional_tag_2 in res_tags) # Achieve an empty list res_tags.remove(tags[i]) res_tags.remove(additional_tag_1) res_tags.remove(additional_tag_2) self.assertFalse(res_tags) else: # Achieve an empty list res_tags.remove(tags[i]) self.assertFalse(res_tags)
def test_set_and_get_tag_on_dir(self): """Test get and set Finder tags. This version uses a directory. """ with tempfile.TemporaryDirectory() as sample_folder: manual_tag = "Comprobación" add_finder_tag_for_path(sample_folder, manual_tag) res_tags = get_finder_tags_for_path(sample_folder)[0] self.assertEqual(manual_tag, res_tags)
def test_set_and_get_tag_on_file(self): """Test get and set Finder tags. This version uses a file. """ with tempfile.NamedTemporaryFile() as f: sample_file_path = f.name manual_tag = "5w&sfdbdb!$·!!Y&%" add_finder_tag_for_path(sample_file_path, manual_tag) res_tags = get_finder_tags_for_path(sample_file_path)[0] self.assertEqual(manual_tag, res_tags)
def test_set_and_get_tags_on_dir(self): """Test get and set Finder tags.""" with tempfile.TemporaryDirectory() as sample_folder: manual_tag1 = "Sample tag 1" manual_tag2 = "Sample tag 2" manual_tag3 = "Sample tag 3" add_finder_tag_for_path( sample_folder, manual_tag1, ) add_finder_tag_for_path( sample_folder, manual_tag2, ) add_finder_tag_for_path( sample_folder, manual_tag3, ) res_tags = get_finder_tags_for_path(sample_folder) self.assertTrue(manual_tag1 in res_tags) self.assertTrue(manual_tag2 in res_tags) self.assertTrue(manual_tag3 in res_tags)
def test_remove_all_tags_on_dir(self): """Test remove all Finder tags.""" with tempfile.TemporaryDirectory() as sample_folder: manual_tag1 = "Sample tag 1" manual_tag2 = "Sample tag 2" manual_tag3 = "Sample tag 3" add_finder_tag_for_path( sample_folder, manual_tag1, ) add_finder_tag_for_path( sample_folder, manual_tag2, ) add_finder_tag_for_path( sample_folder, manual_tag3, ) # Remove rm_all_finder_tags_for_path(sample_folder) res_tags = get_finder_tags_for_path(sample_folder) self.assertEqual(res_tags, [])
def test_remove_tag_on_dir(self): """Test remove a Finder tag.""" with tempfile.TemporaryDirectory() as sample_folder: manual_tag1 = "Sample tag 1" manual_tag2 = "Sample tag 2" manual_tag3 = "Sample tag 3" add_finder_tag_for_path( sample_folder, manual_tag1, ) add_finder_tag_for_path( sample_folder, manual_tag2, ) add_finder_tag_for_path( sample_folder, manual_tag3, ) # Remove rm_finder_tag_for_path(sample_folder, manual_tag1) res_tags = get_finder_tags_for_path(sample_folder) self.assertTrue(manual_tag2 in res_tags) self.assertTrue(manual_tag3 in res_tags)