From 9e4e6b7727df521bd187ca209e2a0ebc10f4a382 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 16 Apr 2009 10:42:24 +0200 Subject: first trys --- .gitignore | 1 + item.py | 29 +++++++++++++++++++++++++++++ itemList.py | 28 ++++++++++++++++++++++++++++ storage/__init__.py | 0 storage/factory.py | 5 +++++ storage/sqlite.py | 17 +++++++++++++++++ storage/storageBase.py | 11 +++++++++++ todolist.py | 27 +++++++++++++++++++++++++++ 8 files changed, 118 insertions(+) create mode 100644 .gitignore create mode 100644 item.py create mode 100644 itemList.py create mode 100644 storage/__init__.py create mode 100644 storage/factory.py create mode 100644 storage/sqlite.py create mode 100644 storage/storageBase.py create mode 100755 todolist.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/item.py b/item.py new file mode 100644 index 0000000..c2accaa --- /dev/null +++ b/item.py @@ -0,0 +1,29 @@ +class item: + readOnly = ['id'] + + def __init__(self, title, created, priority): + self.id = -1 + self.title = title + self.created = created + self.priority = priority + + def setId(self, id): + if self.id == -1: + self.id = id + + def getTitle(self): + return self.title + + def getCreatedAt(self): + return self.created + + def getPriority(self): + return self.priority + + def __setattr__(self, name, value): + if name not in item.readOnly: + if name not in self.__dict__ or self.__dict__[name] != value: + self.__dict__[name] = value + + if 'observer' in self.__dict__: + self.observer.notifyChange(self) diff --git a/itemList.py b/itemList.py new file mode 100644 index 0000000..527c837 --- /dev/null +++ b/itemList.py @@ -0,0 +1,28 @@ +class itemList: + def __init__(self, observer): + self.data = [] + self.observer = observer + + def __len__(self): + return len(self.data) + + def __getitem__(self, idx): + return self.data[idx] + + def __setitem__(self, idx, item): + self.data[idx] = item + + def append(self, item): + item.observer = self + self.data.append(item) + self.observer.notifyChange(item) + + def notifyChange(self, sender): + self.observer.notifyChange(sender) + + def __add__(self, item): + self.append(item) + return self + + def __iter__(self): + return self.data.__iter__() diff --git a/storage/__init__.py b/storage/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/storage/factory.py b/storage/factory.py new file mode 100644 index 0000000..c1fa049 --- /dev/null +++ b/storage/factory.py @@ -0,0 +1,5 @@ +from sqlite import * + +def getStorage(): + x = sqlite() + return x diff --git a/storage/sqlite.py b/storage/sqlite.py new file mode 100644 index 0000000..8a24858 --- /dev/null +++ b/storage/sqlite.py @@ -0,0 +1,17 @@ +from storageBase import storageBase +from itemList import itemList +from pysqlite2 import dbapi2 as sqliteBackend + +class sqlite(storageBase): + def __init__(self): + self.con = sqliteBackend.connect(self.getConfigDir() + '/data.sqlite') + + def __del__(self): + self.con.close() + + def load(self): + return itemList(self) + + def notifyChange(self, sender): + print '%s %s' % ('Save changes:', sender.getCreatedAt()) + return diff --git a/storage/storageBase.py b/storage/storageBase.py new file mode 100644 index 0000000..24abd2c --- /dev/null +++ b/storage/storageBase.py @@ -0,0 +1,11 @@ +import os + +class storageBase: + def notifyChange(self, sender): abstract + + def getConfigDir(self): + dir = os.path.expanduser('~/.todolist/blub') + if not os.path.exists(dir): + os.makedirs(dir) + + return dir diff --git a/todolist.py b/todolist.py new file mode 100755 index 0000000..141f5d1 --- /dev/null +++ b/todolist.py @@ -0,0 +1,27 @@ +#!/usr/bin/python + +import storage.factory +import item + +from time import time + +def main(): + store = storage.factory.getStorage() + items = store.load() + items += item.item("Test1", time(), 0) + items += item.item("Test1", time(), 0) + items += item.item("Test1", time(), 0) + + for i in items: + print i.getTitle() + print i.getCreatedAt() + print i.getPriority() + print '========================================' + + items[len(items)-1].id = 'new Title' + items[len(items)-1].id = 'new Title' + + return + +if __name__ == '__main__': + main() -- cgit v1.2.3