Kata Birthday Greeting

mail@pastecode.io avatar
unknown
python
3 years ago
1.7 kB
1
Indexable
Never
import os
import csv
from datetime import date
from optparse import OptionParser

class BirthdayCSVRepository:
    _path = ''

    def __init__(self, path):
        self._path = path

    def _get_file_handle(self):
        return open(self._path, newline='')

    def all(self):
        return csv.reader(self._get_file_handle(), delimiter=',', quotechar='|')

class Notifier:

    _template = "Happy birthday, dear {first_name}!"

    def notify(self, friends):
        for friend in friends:
            print(self._template.format(first_name = friend[1]))

class ProcessFriends:

    def execute(self, current_date, friends):
        notify_friends = []
        for friend in friends:
            if self.is_birthday(date.fromisoformat(friend[2]), current_date) :
                notify_friends.append( friend )
        return notify_friends

    def is_birthday(self, friend_date, current_date):
        if friend_date.month == current_date.month and friend_date.day == current_date.day:
            return True
        else:
            return False

def get_current_date():
    parser = OptionParser()
    parser.add_option(
        '-d', '--date',
        default=date.today().isoformat(),
        dest='current_date',
        help='Current date'
    )
    (options, args) = parser.parse_args()
    return date.fromisoformat(options.current_date)

if __name__ == "__main__":

    CSV_FRIENDS_PATH = os.path.dirname(__file__) + '/../friends.csv'

    notify_friends = ProcessFriends().execute(
        get_current_date(),
        BirthdayCSVRepository(CSV_FRIENDS_PATH).all()
    )

    Notifier().notify(notify_friends)