def write_donors(self, file_path, msg=None): """ Writes the donors to a file. :self: The Class :file_path: The path of the output file. :msg: The message to tell the user when the save is successful. """ content = "\n".join([str(d) for d in self.donors]) FileHelpers.write_file(file_path, content, msg)
def test_get_user_output_existing_dir(self): with patch("os.path.exists") as pathy: pathy.return_value = True with patch('builtins.input') as handle_input: handle_input.return_value = "./lel/" return_dir = FileHelpers.get_user_output_path() assert return_dir == "./lel/"
def test_get_user_output_nonexisting_dir_invalid_choice(self): with patch("os.path.exists") as pathy: pathy.return_value = False with patch('builtins.input') as handle_input: handle_input.side_effect = ["./lel/", "yee", "no"] return_dir = FileHelpers.get_user_output_path() assert return_dir == tempfile.gettempdir()
def test_get_user_output_nonexisting_dir_create(self): with patch("os.path.exists") as pathy: pathy.return_value = False with patch('builtins.input') as handle_input: handle_input.side_effect = ["./lel/", "yEs"] with patch("os.makedirs"): return_dir = FileHelpers.get_user_output_path() assert return_dir == "./lel/"
def __init__(self, list_donors, email_template): """ Instantiate a Donor Collection :self: The class :list_donors: The list of donors in the collection. :email_template: The path to the email template for donors. """ self.donors = list_donors self.email_template = FileHelpers.open_file(email_template, type(str()))
def send_to_all(): """ Export thank you notes for all donors. """ print("\n\nLets thank everybody!") print( str("\nThis will prepare a letter to send to everyone has donated to " "Studio Starchelle in the past.")) print( str("All letters will be saved as text (.txt) files in the default directory a " "different directory is specified.")) save_dir = FileHelpers.get_user_output_path() if save_dir is None: print("\nCancelling send to all. Returning to main menu...\n") return for donor in donor_list: file_path = path.join(save_dir, f"{donor.name}.txt") FileHelpers.write_file(file_path, donor.get_email(donor_list.email_template)) print(f"Donor letters successfully saved to: {save_dir}")
def from_file(cls, file_path, email_template): """ Instantiate a Donor Collection from the contents of a CSV File. :cls: The class :file_path: The path to the donor list CSV :email_template: The path to the email template for donors. """ if not os.path.isfile(file_path): raise FileNotFoundError(f"No file exists at path: {file_path}") donors = [ Donor.from_string(c) for c in FileHelpers.open_file(file_path, type(Donor)) ] self = cls(donors, email_template) return self
from support import FileHelpers from support import MenuItem from support import MenuDriven from donor_models import Donor from donor_models import Donor_Collection parser = argparse.ArgumentParser( description="Studio Starchelle's Donor Appreciation System") parser.add_argument( "--donors", metavar="path", help="The donor list to import", type=str, default=FileHelpers.default_resource_file_path("donor_list.csv")) parser.add_argument( "--email", metavar="path", help="The email template to import", type=str, default=FileHelpers.default_resource_file_path("email_template.txt")) donor_list = None def main(args): """ The main method of the applicaiton. :args: The arguments parsed from argparse
def test_write_file_list(self): open_mock = mock.mock_open() with patch("builtins.open", open_mock, create=True): FileHelpers.write_file("./test.csv", ["stuff", "in", "a", "box"]) open_mock.assert_any_call("./test.csv", "w") open_mock().writelines.assert_any_call(["stuff", "in", "a", "box"])
def test_write_file_string(self): open_mock = mock.mock_open() with patch("builtins.open", open_mock, create=True): FileHelpers.write_file("./test.txt", "stuff in a box") open_mock.assert_any_call("./test.txt", "w") open_mock().write.assert_any_call("stuff in a box")
def test_resource_file_pathing(self): file_path = os.path.dirname(os.path.realpath(__file__)) file_path = os.path.join(file_path, "resource", "test.txt") assert FileHelpers.default_resource_file_path("test.txt") == file_path