| 1 |
#! /usr/bin/env python |
| 2 |
from gobject import GObject |
| 3 |
from tinymail import List, Iterator |
| 4 |
from UserList import UserList |
| 5 |
|
| 6 |
import tinymail |
| 7 |
import gobject |
| 8 |
|
| 9 |
class PyTnyList(GObject, UserList, List): |
| 10 |
""" |
| 11 |
An implementation of the Tinymail List interface |
| 12 |
based on a python list. |
| 13 |
""" |
| 14 |
def __init__(self, sequence=[]): |
| 15 |
GObject.__init__(self) |
| 16 |
UserList.__init__(self, sequence) |
| 17 |
|
| 18 |
def do_get_length(self): |
| 19 |
return len(self) |
| 20 |
|
| 21 |
def do_prepend(self, obj): |
| 22 |
self.insert(0, obj) |
| 23 |
|
| 24 |
def do_append(self, obj): |
| 25 |
self.append(obj) |
| 26 |
|
| 27 |
def do_foreach(self, func, data): |
| 28 |
for item in self: |
| 29 |
func(item, data) |
| 30 |
|
| 31 |
def do_create_iterator(self): |
| 32 |
return self.__ListIterator(tuple(self)) |
| 33 |
|
| 34 |
def do_copy(self): |
| 35 |
return PyTnyList(self) |
| 36 |
|
| 37 |
class __ListIterator(GObject, Iterator): |
| 38 |
""" |
| 39 |
An implementation of the Tinymail iterator interface |
| 40 |
""" |
| 41 |
def __init__(self, sequence): |
| 42 |
self.sequence = sequence |
| 43 |
self.position = 0 |
| 44 |
|
| 45 |
def do_next(self): |
| 46 |
self.position += 1 |
| 47 |
|
| 48 |
def do_prev(self): |
| 49 |
self.position -= 1 |
| 50 |
|
| 51 |
def do_first(self): |
| 52 |
self.position = 0 |
| 53 |
|
| 54 |
def do_nth(self, pos): |
| 55 |
self.position = pos |
| 56 |
|
| 57 |
def do_get_current(self): |
| 58 |
return self.sequence[self.position] |
| 59 |
|
| 60 |
def do_is_done(self): |
| 61 |
return not self.position < len(sequence) |
| 62 |
|
| 63 |
def do_get_list(self): |
| 64 |
return PyTnyList(self.sequence) |
| 65 |
|
| 66 |
gobject.type_register(PyTnyList) |
| 67 |
|
| 68 |
def print_pair(pair): |
| 69 |
print pair.get_name() |
| 70 |
print pair.get_value() |
| 71 |
|
| 72 |
if __name__ == '__main__': |
| 73 |
a = PyTnyList() |
| 74 |
for i in range(10): |
| 75 |
List.append(a, tinymail.Pair(str(i), str(i))) |
| 76 |
print a.get_length() |
| 77 |
for i in range(10): |
| 78 |
a.prepend(tinymail.Pair(str(i), str(i))) |
| 79 |
print a.get_length() |
| 80 |
a.foreach(print_pair) |
| 81 |
b = a.copy() |
| 82 |
b.foreach(print_pair) |