# Path: ratechecker/dataset.py
# class Dataset(object):
#     loaders = {
#         "adjustment": AdjustmentLoader,
#         "product": ProductLoader,
#         "rate": RateLoader,
#         "region": RegionLoader,
#     }
# 
#     def __init__(self, f):
#         self.zf = ZipFile(f)
# 
#     @cached_property
#     def cover_sheet(self):
#         with self.zf.open("CoverSheet.xml") as f:
#             return CoverSheet(f)
# 
#     @cached_property
#     def timestamp(self):
#         ts = datetime.combine(self.cover_sheet.date, time.min)
#         return timezone.make_aware(ts, timezone.get_current_timezone())
# 
#     @cached_property
#     def filename_prefix(self):
#         return self.cover_sheet.date.strftime("%Y%m%d")
# 
#     def load(self):
#         # Sort the list of loaders so that Region loads last, as a bellwether
#         for key, loader_cls in sorted(self.loaders.items()):
#             try:
#                 f = self.datafile(key)
#             except KeyError:
#                 # The fees data is expected to be temporarily unavailable,
#                 # so if the fees file is not found, we skip it and
#                 # continue loading the other data types.
#                 if key == "fee":
#                     continue
#                 raise
# 
#             # The zip file may be opened as binary, but we want to process the
#             # files that it contains as text.
#             f_text = io.TextIOWrapper(f)
#             loader = loader_cls(f_text, data_timestamp=self.timestamp)
#             loader.load()
# 
#     def datafile(self, name):
#         filename = "{}_{}.txt".format(self.filename_prefix, name)
#         return self.zf.open(filename)


from datetime import date
from io import BytesIO
from zipfile import ZipFile
from ratechecker.dataset import Dataset
def get_sample_dataset_zipfile(day=None, datasets={}):
    day = day or date.today()

    f = BytesIO()
    zf = ZipFile(f, "w")
    zf.writestr("CoverSheet.xml", get_sample_cover_sheet(day=day))

    if not datasets:
        datestr = day.strftime("%Y%m%d")
        datasets = {
            "{}_{}.txt".format(datestr, k): "dummy"
            for k in Dataset.loaders.keys()  # noqa
        }

    for filename, contents in datasets.items():
        zf.writestr(filename, contents)

    zf.close()

    f.seek(0)
    return f


def write_sample_dataset(filename):
    content = get_sample_dataset_zipfile().read()
    with open(filename, "wb") as f:
        f.write(content)


def get_sample_dataset(day=None, datasets={}):

# Path: ratechecker/dataset.py
# class Dataset(object):
#     loaders = {
#         "adjustment": AdjustmentLoader,
#         "product": ProductLoader,
#         "rate": RateLoader,
#         "region": RegionLoader,
#     }
# 
#     def __init__(self, f):
#         self.zf = ZipFile(f)
# 
#     @cached_property
#     def cover_sheet(self):
#         with self.zf.open("CoverSheet.xml") as f:
#             return CoverSheet(f)
# 
#     @cached_property
#     def timestamp(self):
#         ts = datetime.combine(self.cover_sheet.date, time.min)
#         return timezone.make_aware(ts, timezone.get_current_timezone())
# 
#     @cached_property
#     def filename_prefix(self):
#         return self.cover_sheet.date.strftime("%Y%m%d")
# 
#     def load(self):
#         # Sort the list of loaders so that Region loads last, as a bellwether
#         for key, loader_cls in sorted(self.loaders.items()):
#             try:
#                 f = self.datafile(key)
#             except KeyError:
#                 # The fees data is expected to be temporarily unavailable,
#                 # so if the fees file is not found, we skip it and
#                 # continue loading the other data types.
#                 if key == "fee":
#                     continue
#                 raise
# 
#             # The zip file may be opened as binary, but we want to process the
#             # files that it contains as text.
#             f_text = io.TextIOWrapper(f)
#             loader = loader_cls(f_text, data_timestamp=self.timestamp)
#             loader.load()
# 
#     def datafile(self, name):
#         filename = "{}_{}.txt".format(self.filename_prefix, name)
#         return self.zf.open(filename)


# Path: ratechecker/tests/helpers.py
from datetime import date
from io import BytesIO
from zipfile import ZipFile
from ratechecker.dataset import Dataset

def get_sample_dataset_zipfile(day=None, datasets={}):
    day = day or date.today()

    f = BytesIO()
    zf = ZipFile(f, "w")
    zf.writestr("CoverSheet.xml", get_sample_cover_sheet(day=day))

    if not datasets:
        datestr = day.strftime("%Y%m%d")
        datasets = {
            "{}_{}.txt".format(datestr, k): "dummy"
            for k in Dataset.loaders.keys()  # noqa
        }

    for filename, contents in datasets.items():
        zf.writestr(filename, contents)

    zf.close()

    f.seek(0)
    return f


def write_sample_dataset(filename):
    content = get_sample_dataset_zipfile().read()
    with open(filename, "wb") as f:
        f.write(content)


def get_sample_dataset(day=None, datasets={}):

    return Dataset(get_sample_dataset_zipfile(day=day, datasets=datasets))