from __future__ import with_statement
import os
import csv
import datetime


class VacalcError(RuntimeError): pass


class EmployeeStore(object):

    def __init__(self, db_file):
        self._db_file = db_file
        if self._db_file and os.path.isfile(self._db_file):
            self._employees = self._read_employees(self._db_file)
        else:
            self._employees = {}

    def _read_employees(self, path):
        employees = {}
        with open(path) as db:
            for row in csv.reader(db):
                employee = Employee(row[0], row[1])
                employees[employee.name] = employee
        return employees

    def get_employee(self, name):
        try:
            return self._employees[name]
        except KeyError:
            raise VacalcError("Employee '%s' not found" % name)

    def get_all_employees(self):
        return self._employees.values()

    def add_employee(self, name, startdate):
        if name in self._employees:
            raise VacalcError("Employee '%s' already exists in the system" %
                              name)
        employee = Employee(name, startdate)
        self._employees[employee.name] = employee
        self._serialize(employee)

    def _serialize(self, employee):
        if not self._db_file:
            return
        with open(self._db_file, 'a') as db:
            writer = csv.writer(db, lineterminator='\n')
            writer.writerow([employee.name, employee.startdate])


class Employee(object):

    def __init__(self, name, startdate):
        self.name = name
        self.startdate = self._parse_date(startdate)

    def _parse_date(self, datestring):
        year, month, day = datestring.split('-')
        return datetime.date(int(year), int(month), int(day))

@@ -557,15 +557,33 @@
 0%5D, 
+self._parse_date(
 row%5B1%5D)
+)
 %0A   
@@ -1133,24 +1133,41 @@
 loyee(name, 
+self._parse_date(
 startdate)%0A 
@@ -1164,16 +1164,17 @@
 artdate)
+)
 %0A       
@@ -1508,154 +1508,8 @@
 %5D)%0A%0A
-%0Aclass Employee(object):%0A%0A    def __init__(self, name, startdate):%0A        self.name = name%0A        self.startdate = self._parse_date(startdate)%0A%0A
     
@@ -1543,16 +1543,33 @@
 tring):%0A
+        try:%0A    
         
@@ -1609,16 +1609,20 @@
 it('-')%0A
+    
         
@@ -1675,8 +1675,247 @@
 t(day))%0A
+        except (TypeError, ValueError):%0A            raise VacalcError(%22Invalid time string '%25s'%22 %25 datestring)%0A%0A%0Aclass Employee(object):%0A%0A    def __init__(self, name, startdate):%0A        self.name = name%0A        self.startdate = startdate%0A

raise exception also from invalid date string