Exemplo n.º 1
0
def test_one_glob_case_sensitive_handling(tmpdir):
    """Ensure that creating a non-matching globbed path does not return paths."""
    hello_file_one = tmpdir.join("hello1.txt")
    hello_file_one.write("content")
    hello_file_two = tmpdir.join("hello2.txt")
    hello_file_two.write("content")
    system_name = platform.system()
    if system_name is not WINDOWS:
        assert len(tmpdir.listdir()) == 2
        created_paths = list(
            files.create_paths(tmpdir.basename,
                               file="*.TXT",
                               home=tmpdir.dirname))
        assert len(created_paths) == 0
    elif system_name is WINDOWS:
        assert len(tmpdir.listdir()) == 2
        created_paths = list(
            files.create_paths(tmpdir.basename,
                               file="*.TXT",
                               home=tmpdir.dirname))
        assert len(created_paths) == 2
    if system_name is not WINDOWS:
        for created_path in files.create_paths(tmpdir.basename,
                                               file="HELLO*",
                                               home=tmpdir.dirname):
            assert ".txt" in str(created_path)
        assert len(created_paths) == 0
    elif system_name is WINDOWS:
        for created_path in files.create_paths(tmpdir.basename,
                                               file="HELLO*",
                                               home=tmpdir.dirname):
            assert ".txt" in str(created_path)
        assert len(created_paths) == 2
Exemplo n.º 2
0
def specified_tag_greater_than_count(
    chosen_tag,
    checking_function,
    expected_count,
    given_file,
    containing_directory,
    exact=False,
):
    """Determine if the tag count is greater than expected in given file(s)."""
    # Use these two variables to keep track of tag counts for multiple files.
    # The idea is that file_tags_count_dictionary will store (key, value) pairs
    # where the key is the file and the count is the number of entities in that file.
    file_tags_count = 0
    file_tags_count_dictionary = {}
    # Create a Path object to the chosen file in the containing directory, accounting
    # for the fact that a wildcard like "*.md" will create multiple paths. Note that
    # the create_paths function can only return valid paths, regardless of input.
    for file_for_checking in files.create_paths(
        file=given_file, home=containing_directory
    ):
        file_tag_count = 0
        # since the specified file must be valid and thus suitable for checking,
        # read the contents of the file and then check for the chosen tag
        file_contents = file_for_checking.read_text()
        file_tag_count = checking_function(file_contents, chosen_tag)
        file_tags_count_dictionary[file_for_checking.name] = file_tag_count
    # return the minimum value and the entire dictionary of counts
    minimum_pair = util.get_first_minimum_value(file_tags_count_dictionary)
    file_tags_count = minimum_pair[1]
    # check the condition and also return file_tags_count
    return (
        util.greater_than_equal_exacted(file_tags_count, expected_count, exact),
        file_tags_count_dictionary,
    )
Exemplo n.º 3
0
def test_create_non_glob_path_with_none_middle(tmpdir):
    """Ensure that creating a non-globbed path with get_paths works correctly."""
    hello_file_one = tmpdir.join("hello1.txt")
    hello_file_one.write("content")
    hello_file_two = tmpdir.join("hello2.txt")
    hello_file_two.write("content")
    assert len(tmpdir.listdir()) == 2
    created_paths = list(
        files.create_paths(tmpdir.basename,
                           file="hello1.txt",
                           home=tmpdir.dirname))
    assert len(created_paths) == 1
    created_paths = list(
        files.create_paths(tmpdir.basename,
                           file="hello2.txt",
                           home=tmpdir.dirname))
    assert len(created_paths) == 1
Exemplo n.º 4
0
def test_garbage_glob_file_returns_no_matching_paths_more_garbage_markdown_names(
    tmpdir, ):
    """Ensure that creating a garbage globbed path returns no matches."""
    hello_file_one = tmpdir.join("hello1.txt")
    hello_file_one.write("content")
    hello_file_two = tmpdir.join("hello2.txt")
    hello_file_two.write("content")
    assert len(tmpdir.listdir()) == 2
    created_paths = list(
        files.create_paths(tmpdir.basename,
                           file="%*#@@--(*.md)Hello.md",
                           home=tmpdir.dirname))
    assert len(created_paths) == 0
    created_paths = list(
        files.create_paths(tmpdir.basename,
                           file="Hello.md%*#@@--(*.md)",
                           home=tmpdir.dirname))
    assert len(created_paths) == 0
Exemplo n.º 5
0
def test_garbage_glob_ext_returns_no_matching_paths(tmpdir):
    """Ensure that creating a garbage globbed path returns no matches."""
    hello_file_one = tmpdir.join("hello1.txt")
    hello_file_one.write("content")
    hello_file_two = tmpdir.join("hello2.txt")
    hello_file_two.write("content")
    assert len(tmpdir.listdir()) == 2
    created_paths = list(
        files.create_paths(tmpdir.basename, file="*.FAKE",
                           home=tmpdir.dirname))
    assert len(created_paths) == 0
Exemplo n.º 6
0
def count_entities(given_file, containing_directory, checking_function):
    """Count the number of entities for the file(s) in the directory."""
    # create an empty dictionary of filenames and an internal dictionary
    file_counts_dictionary = {}
    # if the file is not found in the directory, the count is zero
    file_contents_count = 0
    # create a path for the given file and its containing directory
    # note that this call does not specify any *args and thus there
    # are no directories between the home directory and the file
    for file_for_checking in files.create_paths(
        file=given_file, home=containing_directory
    ):
        # start the count of the number of entities at zero, assuming none found yet
        file_contents_count = 0
        # create an empty dictionary of the counts
        file_contents_count_dictionary = {}
        # a valid file exists and thus it is acceptable to perform the checking
        # extract the text from the file_for_checking
        file_contents = file_for_checking.read_text()
        # use the provided checking_function to check the contents of the file
        # note this works since Python supports passing a function to a function
        file_contents_count, file_contents_count_dictionary = checking_function(
            file_contents
        )
        # the checking_function returned a dictionary of form {entity: count}
        # so we should store this dictionary insider the containing dictionary
        # this case would occur for checks like number of words in paragraphs
        if file_contents_count_dictionary:
            # associate these file counts with the filename in a dictionary
            file_counts_dictionary[
                file_for_checking.name
            ] = file_contents_count_dictionary
        # the checking_function did not return a dictionary because that was
        # not sensible for the type of check (e.g., counting paragraphs)
        # so we should make a "dummy" dictionary containing the entity count
        else:
            file_contents_count_dictionary = {1: file_contents_count}
            file_counts_dictionary[
                file_for_checking.name
            ] = file_contents_count_dictionary
    # find the minimum count for all paragraphs across all of the files
    # assume that nothing was found and the count is zero and prove otherwise
    file_contents_count_overall = file_contents_count
    # there is a dictionary of counts for files, so deeply find the minimum
    # as long as the count is not of the total words in a file
    if (
        file_counts_dictionary
        and checking_function.__name__ != constants.functions.Count_Total_Words
    ):
        file_contents_count_overall = util.get_first_minimum_value_deep(
            file_counts_dictionary
        )[1][1]
    # return the overall requested count and the nested file count dictionary
    return file_contents_count_overall, file_counts_dictionary
Exemplo n.º 7
0
def test_create_one_glob_path_with_none_middle(tmpdir):
    """Ensure that creating a globbed path works correctly."""
    hello_file_one = tmpdir.join("hello1.txt")
    hello_file_one.write("content")
    hello_file_two = tmpdir.join("hello2.txt")
    hello_file_two.write("content")
    assert len(tmpdir.listdir()) == 2
    # *.txt
    created_paths = list(
        files.create_paths(tmpdir.basename, file="*.txt", home=tmpdir.dirname))
    assert len(created_paths) == 2
    # *.*
    created_paths = list(
        files.create_paths(tmpdir.basename, file="*.*", home=tmpdir.dirname))
    # *
    created_paths = list(
        files.create_paths(tmpdir.basename, file="*", home=tmpdir.dirname))
    assert len(created_paths) == 2
    for created_path in files.create_paths(tmpdir.basename,
                                           file="*.txt",
                                           home=tmpdir.dirname):
        assert ".txt" in str(created_path)
    created_paths = list(
        files.create_paths(tmpdir.basename, file="hello*",
                           home=tmpdir.dirname))
    assert len(created_paths) == 2
    for created_path in files.create_paths(tmpdir.basename,
                                           file="hello*",
                                           home=tmpdir.dirname):
        assert ".txt" in str(created_path)
Exemplo n.º 8
0
def count_lines(
    given_file=constants.markers.Nothing,
    containing_directory=constants.markers.Nothing,
    contents=constants.markers.Nothing,
):
    """Count lines for the file in the directory, or alternatively, provided contents."""
    # Use these two variables to keep track of line counts for multiple files.
    # The idea is that file_contents_count_dictionary will store (key, value) pairs
    # where the key is the file and the count is the number of entities in that file.
    file_contents_count = 0
    file_contents_count_dictionary = {}
    # the contents are provided and thus there is no file or directory
    # the context for this condition is when the function checks
    # the output from the execution of a specified command
    if contents is not constants.markers.Nothing:
        line_list = get_line_list(contents)
        file_contents_count = len(line_list)
    # file is and directory are available and thus there are no contents
    # the context for this condition is when the function checks
    # the number of lines in a specific file in a specific directory
    elif (given_file is not constants.markers.Nothing
          and containing_directory is not constants.markers.Nothing):
        # Create a Path object to the chosen file in the containing directory, accounting
        # for the fact that a wildcard like "*.md" will create multiple paths. Note that
        # the create_paths function can only return valid paths, regardless of input.
        for file_for_checking in files.create_paths(file=given_file,
                                                    home=containing_directory):
            file_contents_count = 0
            # file is available and the contents are not provided
            # the context for this condition is when the function checks
            # the contents of a specified file that exists on the filesystem
            file_contents = file_for_checking.read_text()
            line_list = get_line_list(file_contents)
            file_contents_count = len(line_list)
            file_contents_count_dictionary[
                file_for_checking.name] = file_contents_count
        # return the minimum value and the entire dictionary of counts
        minimum_pair = util.get_first_minimum_value(
            file_contents_count_dictionary)
        file_contents_count = minimum_pair[1]
    return file_contents_count, file_contents_count_dictionary
Exemplo n.º 9
0
def count_entities(
    chosen_fragment,
    checking_function,
    given_file=constants.markers.Nothing,
    containing_directory=constants.markers.Nothing,
    contents=constants.markers.Nothing,
):
    """Count fragments for the file in the directory (or contents) and a fragment."""
    # Use these two variables to keep track of entity counts for multiple files.
    # The idea is that file_contents_count_dictionary will store (key, value) pairs
    # where the key is the file and the count is the number of entities in that file.
    file_contents_count = 0
    file_contents_count_dictionary = {}
    # file is not available and the contents are provided
    # the context for this condition is when the function checks
    # the output from the execution of a specified command
    if (contents is not constants.markers.Nothing
            and given_file is constants.markers.Nothing):
        # The command ran and produced an error, which means that its output
        # is technically "" or Nothing. So, set it to Nothing so that the
        # checking_function can determine that none of the entity exists in it
        if contents is constants.markers.Command_Error:
            contents = constants.markers.Nothing
        # run the checking_function to look for fragments in the contents
        file_contents_count = checking_function(contents, chosen_fragment)
        return file_contents_count, file_contents_count_dictionary
    for file_for_checking in files.create_paths(file=given_file,
                                                home=containing_directory):
        # an actual file is available and command contents are not provided
        # the context for this condition is when the function checks file contents
        # read the text from the file and then check for the chosen fragment
        file_contents = file_for_checking.read_text()
        file_contents_count = checking_function(file_contents, chosen_fragment)
        file_contents_count_dictionary[
            file_for_checking.name] = file_contents_count
    # return the minimum value and the entire dictionary of counts
    minimum_pair = util.get_first_minimum_value(file_contents_count_dictionary)
    file_contents_count = minimum_pair[1]
    return file_contents_count, file_contents_count_dictionary