# Path: Helpers/Switch.py
# class switch(object):
#     """
#     This class provides the functionality we want. You only need to look at
#     this if you want to know how this works. It only needs to be defined
#     once, no need to muck around with its internals.
#     """
#     def __init__(self, value):
#         self.value = value
#         self.fall = False
# 
#     def __iter__(self):
#         """Return the match method once, then stop"""
#         yield self.match
#         raise StopIteration
# 
#     def match(self, *args):
#         """Indicate whether or not to enter a case suite"""
#         if self.fall or not args:
#             return True
#         elif self.value in args:  # changed for v1.5, see below
#             self.fall = True
#             return True
#         else:
#             return False


from urllib import urlretrieve
from Helpers.Switch import switch

    def __init__(self, stock_tickers=None, start_date=None, end_date=None):
        self.stock_tickers = stock_tickers
        self.start_date = self.get_date(start_date)
        self.end_date = self.get_date(end_date)

    def download_csv(self):
        for ticker in self.stock_tickers:
            link = "http://finance.google.com/finance/historical?q=JSE:" + ticker
            if self.start_date is not None:
                link += "&startdate=" + self.start_date
            if self.end_date is not None:
                link += "&enddate=" + self.end_date
            link += "&output=csv"
            print "Download link: " + link
            # TODO: Check this method when I get home
            try:
                urlretrieve(link, "Download.csv")
            except:
                link.replace("q=JSE:", "q=")
                urlretrieve(link, "Download.csv")

    @staticmethod
    def get_date(date):
        if date is None:
            return None

        assert isinstance(date, str)
        string_values = date.split('-')


# Path: Helpers/Switch.py
# class switch(object):
#     """
#     This class provides the functionality we want. You only need to look at
#     this if you want to know how this works. It only needs to be defined
#     once, no need to muck around with its internals.
#     """
#     def __init__(self, value):
#         self.value = value
#         self.fall = False
# 
#     def __iter__(self):
#         """Return the match method once, then stop"""
#         yield self.match
#         raise StopIteration
# 
#     def match(self, *args):
#         """Indicate whether or not to enter a case suite"""
#         if self.fall or not args:
#             return True
#         elif self.value in args:  # changed for v1.5, see below
#             self.fall = True
#             return True
#         else:
#             return False


# Path: Data/GoogleDownloader.py
from urllib import urlretrieve
from Helpers.Switch import switch


    def __init__(self, stock_tickers=None, start_date=None, end_date=None):
        self.stock_tickers = stock_tickers
        self.start_date = self.get_date(start_date)
        self.end_date = self.get_date(end_date)

    def download_csv(self):
        for ticker in self.stock_tickers:
            link = "http://finance.google.com/finance/historical?q=JSE:" + ticker
            if self.start_date is not None:
                link += "&startdate=" + self.start_date
            if self.end_date is not None:
                link += "&enddate=" + self.end_date
            link += "&output=csv"
            print "Download link: " + link
            # TODO: Check this method when I get home
            try:
                urlretrieve(link, "Download.csv")
            except:
                link.replace("q=JSE:", "q=")
                urlretrieve(link, "Download.csv")

    @staticmethod
    def get_date(date):
        if date is None:
            return None

        assert isinstance(date, str)
        string_values = date.split('-')


        for case in switch(string_values[1]):