示例#1
0
    def __load_from_file(self):
        '''
        Loads content from a file.
        '''

        try:
            f = open(self.__filename, "r")
        except IOError:
            return

        for line in f:
            line = line.strip()
            attributes = line.split(self.__separator)

            if len(attributes) != 4:
                continue

            id = int(attributes[0].strip())
            name = attributes[1].strip()
            phone = attributes[2].strip()
            address = attributes[3].strip()

            PersonRepository.add(self, Person(id, name, phone, address))

        f.close()
示例#2
0
    def __init__(self, filename):
        '''
        Constructor for the class PersonFileRepository.
        '''

        PersonRepository.__init__(self)
        self.__filename = filename
        self.__separator = "|"
        self.__load_from_file()
 def setUp(self):
     self.L = PersonRepository()
     self.p = {}
     self.p[1] = Person(1, "John", "1", "A")
     self.p[2] = Person(2, "Mary", "1", "B")
     self.p[5] = Person(5, "Mike", "3", "B")
     self.p[7] = Person(7, "Mike", "4", "C")
     self.L.add(self.p[1])
     self.L.add(self.p[2])
     self.L.add(self.p[5])
     self.L.add(self.p[7])
    def setUp(self):
        self.L = PeopleController(PersonRepository())
        self.p = Person(1, "John", "1", "A")
        self.q = Person(2, "Mary", "1", "B")

        self.L.add_person(self.p)
        self.L.add_person(self.q)
示例#5
0
def main():
    in_file = get_storage_preference()

    # Set repositories.
    if in_file:
        personRepo = PersonFileRepository("storage/people.txt")
        activityRepo = ActivityFileRepository(personRepo,
                                              "storage/activities.txt")
    else:
        personRepo = PersonRepository()
        activityRepo = ActivityRepository()

    # Set controllers.
    peopleCtrl = PeopleController(personRepo)
    activitiesCtrl = ActivitiesController(personRepo, activityRepo)
    statsCtrl = StatsController(personRepo, activityRepo)
    undoRedoCtrl = UndoRedoController(peopleCtrl, activitiesCtrl)

    # Set console.
    ui = Console(peopleCtrl, activitiesCtrl, statsCtrl, undoRedoCtrl)

    # Run everything.
    ui.run()

    # Store to files
    if in_file:
        personRepo.store()
        activityRepo.store()
    def setUp(self):
        pR = PersonRepository()
        self.L = ActivitiesController(pR, ActivityRepository())
        self.p = Person(1, "John", "1", "A")
        self.q = Person(2, "Mary", "1", "B")
        self.r = Person(3, "Carl", "1", "B")
        self.a1 = Activity(self.p, "2015.12.20", "12:12", "Swimming")
        self.a2 = Activity(self.p, "2016.01.20", "12:12", "Swimming")
        self.a3 = Activity(self.q, "2015.12.21", "12:12", "Swimming")
        self.a4 = Activity(self.q, "2015.12.20", "10:12", "Reading")

        pR.add(self.p)
        pR.add(self.q)
        self.L.add_activity(self.a1)
        self.L.add_activity(self.a2)
        self.L.add_activity(self.a3)
        self.L.add_activity(self.a4)
示例#7
0
    def setUp(self):
        pR = PersonRepository()
        aR = ActivityRepository()
        self.L = StatsController(pR, aR)
        self.p = Person(1, "John", "1", "A")
        self.q = Person(2, "Mary", "1", "B")
        self.a1 = Activity(self.p, "2015.12.20", "12:12", "Swimming")
        self.a2 = Activity(self.p, "2016.01.20", "12:12", "Mapping")
        self.a3 = Activity(self.q, "2015.12.21", "12:12", "Swimming")
        self.a4 = Activity(self.q, "2015.12.20", "10:12", "Reading")

        pR.add(self.p)
        pR.add(self.q)
        aR.add(self.a1)
        aR.add(self.a2)
        aR.add(self.a3)
        aR.add(self.a4)
    def test_add_activity(self):
        pR = PersonRepository()
        L = ActivitiesController(pR, ActivityRepository())
        p = self.p
        q = self.q
        r = self.r
        pR.add(p)
        pR.add(q)

        L.add_activity(self.a1)
        self.assertEqual(L.number_of_activities(), 1)
        self.assertEqual(L.find_activity_by_position(0), self.a1)

        L.add_activity(self.a3)
        self.assertEqual(L.number_of_activities(), 2)
        self.assertEqual(L.find_activity_by_position(1), self.a3)

        self.assertRaises(NABException, L.add_activity,
                          Activity(r, "2000.01.01", "12:12", "A"))
    def test_add_person(self):
        L = PeopleController(PersonRepository())

        L.add_person(self.p)
        self.assertEqual(L.number_of_people(), 1)
        self.assertEqual(L.find_person_by_id(1), self.p)

        self.assertRaises(NABException, L.add_person, self.p)
        self.assertEqual(L.number_of_people(), 1)

        L.add_person(self.q)
        self.assertEqual(L.number_of_people(), 2)
        self.assertEqual(L.find_person_by_id(2), self.q)
 def get(self):
     repository = PersonRepository('data/persons.csv')
     self.render('../view/html/persons.html',
                 title='Persons',
                 persons=repository.findAll())
示例#11
0
 def get(self, email):
     repository = PersonRepository('data/persons.csv')
     self.render('../view/html/person.html',
                 title='Person',
                 person=repository.findOneByEmail(email))
示例#12
0
    f = open("settings.properties", "r")
    s = f.read()
    lines = s.split("\n")
    set = {}
    l = 0
    for line in lines:
        tokens = line.split("=")
        set[tokens[0].strip()] = tokens[1].strip()
        settings.append(set)
        l += 1
    return settings


settings = readSettings()
if settings[0]["file_type"] == "memory":
    persRepo = PersonRepository()
    initListPers(persRepo)
if settings[0]["file_type"] == "text_file":
    persRepo = PersonTextFileRepo()
if settings[0]["file_type"] == "binary_file":
    persRepo = PersonBinaryRepo()

if settings[1]["file_type"] == "memory":
    actRepo = ActivityRepository()
    initListActs(actRepo)
if settings[1]["file_type"] == "text_file":
    actRepo = ActivityTextFileRepo()
if settings[1]["file_type"] == "binary_file":
    actRepo = ActivityBinaryRepo()

dayRepo = DayRepository()
class PersonRepositoryTestCase(unittest.TestCase):
    def setUp(self):
        self.L = PersonRepository()
        self.p = {}
        self.p[1] = Person(1, "John", "1", "A")
        self.p[2] = Person(2, "Mary", "1", "B")
        self.p[5] = Person(5, "Mike", "3", "B")
        self.p[7] = Person(7, "Mike", "4", "C")
        self.L.add(self.p[1])
        self.L.add(self.p[2])
        self.L.add(self.p[5])
        self.L.add(self.p[7])

    def test_add(self):
        L = PersonRepository()

        self.assertEqual(len(L), 0)
        L.add(self.p[1])
        self.assertEqual(len(L), 1)
        self.assertEqual(L.find_by_id(1), self.p[1])
        L.add(self.p[2])
        self.assertEqual(len(L), 2)
        self.assertEqual(L.find_by_id(2), self.p[2])
        L.add(self.p[5])
        self.assertEqual(len(L), 3)
        self.assertEqual(L.find_by_id(5), self.p[5])
        self.assertRaises(NABException, L.find_by_id, self.p[1])

    def test_find(self):
        L = self.L

        self.assertEqual(L.find(1), 0)
        self.assertEqual(L.find(3), -1)
        self.assertEqual(L.find(2), 1)
        self.assertEqual(L.find(5), 2)
        self.assertEqual(L.find(7), 3)
        self.assertRaises(NABException, L.find, -1)

    def test_find_by_id(self):
        L = self.L

        self.assertEqual(L.find_by_id(1), self.p[1])
        self.assertEqual(L.find_by_id(5), self.p[5])
        self.assertRaises(NABException, L.find_by_id, 3)
        self.assertRaises(NABException, L.find_by_id, -1)

    def test_find_by_name(self):
        L = self.L

        P = L.find_by_name("Mike")
        self.assertEqual(len(P), 2)
        self.assertNotEqual(P.find(5), -1)
        self.assertNotEqual(P.find(7), -1)

        P = L.find_by_name("Mary")
        self.assertEqual(len(P), 1)
        self.assertNotEqual(P.find(2), -1)

        P = L.find_by_name("Sophie")
        self.assertEqual(len(P), 0)

    def test_find_by_phone(self):
        L = self.L

        P = L.find_by_phone("1")
        self.assertEqual(len(P), 2)
        self.assertNotEqual(P.find(1), -1)
        self.assertNotEqual(P.find(2), -1)

        P = L.find_by_phone("3")
        self.assertEqual(len(P), 1)
        self.assertNotEqual(P.find(5), -1)

        P = L.find_by_phone("9")
        self.assertEqual(len(P), 0)

    def test_find_by_address(self):
        L = self.L

        P = L.find_by_address("B")
        self.assertEqual(len(P), 2)
        self.assertNotEqual(P.find(2), -1)
        self.assertNotEqual(P.find(5), -1)

        P = L.find_by_address("A")
        self.assertEqual(len(P), 1)
        self.assertNotEqual(P.find(1), -1)

        P = L.find_by_address("D")
        self.assertEqual(len(P), 0)

    def test_remove(self):
        L = self.L

        self.assertEqual(L.remove(2), self.p[5])
        self.assertEqual(len(L), 3)
        self.assertEqual(L.find(5), -1)

        self.assertEqual(L.remove(0), self.p[1])
        self.assertEqual(len(L), 2)
        self.assertEqual(L.find(1), -1)

        self.assertRaises(NABException, L.remove, 3)

    def test_remove_by_id(self):
        L = self.L

        L.remove_by_id(5)
        self.assertEqual(len(L), 3)
        self.assertEqual(L.find(5), -1)

        L.remove_by_id(1)
        self.assertEqual(len(L), 2)
        self.assertEqual(L.find(1), -1)

        self.assertRaises(NABException, L.remove_by_id, 3)
    def test_add(self):
        L = PersonRepository()

        self.assertEqual(len(L), 0)
        L.add(self.p[1])
        self.assertEqual(len(L), 1)
        self.assertEqual(L.find_by_id(1), self.p[1])
        L.add(self.p[2])
        self.assertEqual(len(L), 2)
        self.assertEqual(L.find_by_id(2), self.p[2])
        L.add(self.p[5])
        self.assertEqual(len(L), 3)
        self.assertEqual(L.find_by_id(5), self.p[5])
        self.assertRaises(NABException, L.find_by_id, self.p[1])