369 |
ira |
1 |
#!/usr/bin/env python
|
|
|
2 |
|
|
|
3 |
#
|
|
|
4 |
# Copyright 2006, Ira W. Snyder (devel@irasnyder.com)
|
|
|
5 |
# License: GNU GPL v2 (or, at your option, any later version)
|
|
|
6 |
#
|
|
|
7 |
|
|
|
8 |
# Print structure description:
|
|
|
9 |
# ===============================================
|
|
|
10 |
# desc = { TYPE1 : func1, TYPE2 : func2, ... }
|
|
|
11 |
#
|
|
|
12 |
# Each type will be a unique key (enforced by the dict)
|
|
|
13 |
# Each func will be a pointer to a function.
|
|
|
14 |
|
|
|
15 |
import errno
|
|
|
16 |
|
|
|
17 |
class PrintSystem (object):
|
|
|
18 |
"""A very generic printing system. This class allows you to specify a
|
|
|
19 |
structure that describes your printing system, and it will handle
|
|
|
20 |
any further messages sent to it."""
|
|
|
21 |
|
|
|
22 |
def __init__ (self, description):
|
|
|
23 |
self.__description = description
|
|
|
24 |
|
|
|
25 |
def __default_action (self, mytype, mydata):
|
|
|
26 |
print 'ERROR: no action for: type=%s data=%s' % (str(mytype), str(mydata))
|
|
|
27 |
|
|
|
28 |
def mprint (self, message):
|
|
|
29 |
"""Print a message according to our description.
|
|
|
30 |
|
|
|
31 |
It should be in the form (TYPE, ...)"""
|
|
|
32 |
|
|
|
33 |
# If there is no type in the message, it must be invalid
|
|
|
34 |
if len(message) < 1:
|
|
|
35 |
return -errno.EINVAL
|
|
|
36 |
|
|
|
37 |
mytype = message[0] # the first element is the type
|
|
|
38 |
mydata = message[1:] # the rest is the data
|
|
|
39 |
|
|
|
40 |
# If we don't have a valid function to use, this message
|
|
|
41 |
# must be invalid
|
|
|
42 |
if mytype not in self.__description.keys():
|
|
|
43 |
self.__default_action (mytype, mydata)
|
|
|
44 |
return -errno.EINVAL
|
|
|
45 |
|
|
|
46 |
# Looks like we're good, call the correct function
|
|
|
47 |
# with the data given
|
|
|
48 |
try:
|
|
|
49 |
self.__description[mytype](mydata)
|
|
|
50 |
except:
|
|
|
51 |
print 'Had an exception while trying to print what appeared to'
|
|
|
52 |
print 'be a valid message. The message was:'
|
|
|
53 |
self.__default_action (mytype, mydata)
|
|
|
54 |
return -errno.EINVAL
|
|
|
55 |
|
|
|
56 |
# We got here, so we completed successfully!
|
|
|
57 |
return 0
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
################################################################################
|
|
|
61 |
# Test material is below
|
|
|
62 |
################################################################################
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
def f1 (data):
|
|
|
66 |
(a, b, c) = data
|
|
|
67 |
print 'in f1 a=%d b=%d c=%d' % (a, b, c)
|
|
|
68 |
|
|
|
69 |
def f2 (data):
|
|
|
70 |
(a, b) = data
|
|
|
71 |
print 'in f2 a=%s b=%s' % (a, b)
|
|
|
72 |
|
|
|
73 |
def f3 (data):
|
|
|
74 |
print 'no data for f3'
|
|
|
75 |
|
|
|
76 |
def main():
|
|
|
77 |
( TYPE1, TYPE2, TYPE3 ) = range(3)
|
|
|
78 |
mystruct = { TYPE1 : f1, TYPE2 : f2, TYPE3 : f3 }
|
|
|
79 |
|
|
|
80 |
ps = PrintSystem (mystruct)
|
|
|
81 |
|
|
|
82 |
print 'All here should succeed:'
|
|
|
83 |
ps.mprint ((TYPE1, 1, 2, 3))
|
|
|
84 |
ps.mprint ((TYPE2, 'hello', 'world'))
|
|
|
85 |
ps.mprint ((TYPE3,))
|
|
|
86 |
ps.mprint ((TYPE1, 2, 3, 4))
|
|
|
87 |
|
|
|
88 |
print '\nShould fail here:'
|
|
|
89 |
ps.mprint ((20, 'a', 'b', 'c'))
|
|
|
90 |
|
|
|
91 |
if __name__ == '__main__':
|
|
|
92 |
# Run test code
|
|
|
93 |
main ()
|
|
|
94 |
|