def check_filename_prefix_postfix(contents, min_length_of_prefix=6, min_files_for_prefix=5): """ Checks whether the project has some files with filenames having certain prefix or postfix. :param contents: The python object containing the file and directory structure written to '.project_data.yaml'. :param min_length_of_prefix: The minimum length of prefix for it green_mode to consider as a valid prefix. :param min_files_for_prefix: The minimum amount of files a prefix to match against for green_mode to consider it as a valid prefix. :return: Update contents value with the results found out from the file/directory structure in .project_data.yaml. """ file_names_list = get_files_list(contents['dir_structure']) file_names_list = [ os.path.splitext(os.path.basename(x))[0] for x in file_names_list ] file_names_list_reverse = [ os.path.splitext(x)[0][::-1] for x in file_names_list ] trie = Trie() for file in file_names_list: trie.insert(file) prefixes = trie.get_prefixes(min_length_of_prefix, min_files_for_prefix) trie_reverse = Trie() for file in file_names_list_reverse: trie_reverse.insert(file) suffixes = trie_reverse.get_prefixes(min_length_of_prefix, min_files_for_prefix) if len(suffixes) == 0: suffixes = [('', 0)] if len(prefixes) == 0: prefixes = [('', 0)] prefix_list, suffix_list = [], [] for prefix, freq in prefixes: prefix_list.append(prefix) for suffix, freq in suffixes: suffix_list.append(suffix[::-1]) contents = append_to_contents(contents, 'filename_prefix', prefix_list, settings_key) contents = append_to_contents(contents, 'filename_suffix', suffix_list, settings_key) return contents
def check_filename_prefix_postfix(contents, min_length_of_prefix=6, min_files_for_prefix=5): """ Checks whether the project has some files with filenames having certain prefix or postfix. :param contents: The python object containing the file and directory structure written to '.project_data.yaml'. :param min_length_of_prefix: The minimum length of prefix for it green_mode to consider as a valid prefix. :param min_files_for_prefix: The minimum amount of files a prefix to match against for green_mode to consider it as a valid prefix. :return: Update contents value with the results found out from the file/directory structure in .project_data.yaml. """ file_names_list = get_files_list(contents['dir_structure']) file_names_list = [os.path.splitext(os.path.basename(x))[ 0] for x in file_names_list] file_names_list_reverse = [os.path.splitext( x)[0][::-1] for x in file_names_list] trie = Trie() for file in file_names_list: trie.insert(file) prefixes = trie.get_prefixes(min_length_of_prefix, min_files_for_prefix) trie_reverse = Trie() for file in file_names_list_reverse: trie_reverse.insert(file) suffixes = trie_reverse.get_prefixes( min_length_of_prefix, min_files_for_prefix) if len(suffixes) == 0: suffixes = [('', 0)] if len(prefixes) == 0: prefixes = [('', 0)] prefix_list, suffix_list = [], [] for prefix, freq in prefixes: prefix_list.append(prefix) for suffix, freq in suffixes: suffix_list.append(suffix[::-1]) contents = append_to_contents(contents, 'filename_prefix', prefix_list, settings_key) contents = append_to_contents(contents, 'filename_suffix', suffix_list, settings_key) return contents
def test_append_to_contents_4(self): ret_contents = append_to_contents({settings_key: [{'key': [3]}, {'some_other_key': [True]}, 'some_other_entry_in_list']}, 'key', [1, 2], settings_key) self.assertEqual(ret_contents, {settings_key: [{'key': [3, 1, 2]}, {'some_other_key': [True]}, 'some_other_entry_in_list']})
def test_append_to_contents_4(self): ret_contents = append_to_contents( { settings_key: [{ 'key': [3] }, { 'some_other_key': [True] }, 'some_other_entry_in_list'] }, 'key', [1, 2], settings_key) self.assertEqual( ret_contents, { settings_key: [{ 'key': [3, 1, 2] }, { 'some_other_key': [True] }, 'some_other_entry_in_list'] })
def test_append_to_contents_3(self): ret_contents = append_to_contents({settings_key: [{'key': [3]}]}, 'key', [1, 2], settings_key) self.assertEqual(ret_contents, {settings_key: [{'key': [3, 1, 2]}]})
def test_append_to_contents_3(self): ret_contents = append_to_contents({settings_key: [{ 'key': [3] }]}, 'key', [1, 2], settings_key) self.assertEqual(ret_contents, {settings_key: [{'key': [3, 1, 2]}]})
def test_check_filename_prefix_postfix(self): # Files having a common prefix but no suffix. file_path = Path(__file__).parent / 'example_.project_data.yaml' with file_path.open() as stream: contents = yaml.load(stream) ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 3, 3) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', ['example_file_'], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', [''], settings_key) self.assertEqual(test_contents, ret_val_contents) # Works well when min files and min length of prefix are sufficiently # low. contents = {'dir_structure': ['py_some_name.xyz', 'py_some_other.c', 'py_yet_another_file.yaml']} ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 2, 2) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', ['py_'], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', [''], settings_key) self.assertEqual(test_contents, ret_val_contents) # Works when min length of prefix and min files is exactly equal # to the paramters passed. ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 3, 3) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', ['py_'], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', [''], settings_key) self.assertEqual(test_contents, ret_val_contents) # Doesn't work when min length of prefix exceeds the prefix value. ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 4, 3) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', [''], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', [''], settings_key) self.assertEqual(test_contents, ret_val_contents) # Doesn't work when min files exceed the files for a given prefix. ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 3, 4) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', [''], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', [''], settings_key) self.assertEqual(test_contents, ret_val_contents) # Files having a prefix and a suffix contents = {'dir_structure': ['py_some_name.xyz', 'py_some_other_name.c', 'py_yet_another_name.yaml']} ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 3, 3) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', ['py_'], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', ['_name'], settings_key) self.assertEqual(test_contents, ret_val_contents) # Both prefix and suffix don't work if the number of files exceed. ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 3, 4) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', [''], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', [''], settings_key) self.assertEqual(test_contents, ret_val_contents) # Files having 2 prefix and 1 suffix. contents = {'dir_structure': ['some_file.xyz', 'another_one.c', 'my_aweesome_file.yaml', 'some_made_up_thing', 'READEME.md', 'some_other_file', 'some_random_string', 'another_file_just_for_prefix', 'another_random_string']} ret_val_contents = check_filename_prefix_postfix(deepcopy(contents), 3, 3) test_contents = deepcopy(append_to_contents(deepcopy(contents), 'filename_prefix', ['some_', 'another_'], settings_key)) test_contents = append_to_contents(deepcopy(test_contents), 'filename_suffix', ['ing'], settings_key) self.assertEqual(test_contents, ret_val_contents)