1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 __docformat__ = "epytext en"
57
58 """
59 orm's debug module was re-written in one of my brigher moments:
60
61 First of all it contains a class called _logstream which implements a
62 subset of Python's file interface. This class is instantiated three
63 times and the objects are provided as global variables: log, debug and
64 sql. Each of these have a verbose attribute which determines, it log,
65 debug or sql information are written to stderr.
66
67 Furthermore, the _logstream class contains a mechanism to
68 automatically add options to a Python optparse.option_parser
69 automatically. Example:
70
71 >>> parser = optparse.OptionParser(doc, version=__version__)
72 >>> log.add_option(parser)
73 >>> debug.add_option(parser)
74
75 resulting in these options:
76
77 -v, --verbose Be verbose (to stderr)
78 -d, --debug Print debug messages (to stderr)
79
80 sql only adds a long --show-sql option if used in this manner. If
81 you'd like to use other switches you'll have to copy the lines below
82 over to your program.
83
84 """
85
86 import sys
87 from string import *
88
89
91 """
92 Implement a subset of the file interface to be used for status
93 messages. Depending on its verbose flag, the write() method will
94 pass its argument to sys.stderr.write() or discard it.
95 """
97 self.verbose = False
98
100 if self.verbose:
101 sys.stderr.write(s)
102
104 if self.verbose:
105 sys.stderr.flush()
106
107 - def __call__(self, option, opt, value, parser):
108 """
109 Called by the Option Parser when the command line option
110 added by the add_option method() implemented by the two child
111 classes is present.
112 """
113 self.verbose = True
114
116 """
117 Return true if logging is ON, otherwise return False. This is ment
118 to be used in if clauses::
119
120 if debug:
121 print 'Debug!'
122 """
123 if self.verbose:
124 return True
125 else:
126 return False
127
128
129 -class _log(_logstream):
131 option_parser.add_option("-v", "--verbose", action="callback",
132 callback=self, help="Be verbose (to stderr)")
135 option_parser.add_option("-d", "--debug", action="callback",
136 callback=self,
137 help="Print debug messages (to stderr)")
138
139 -class _sql(_logstream):
140 """
141 The _sql class has a logging mechanism that is controlled through
142 the buffer_size attribute. If buffer_size is greater than 0, buffer_size
143 sql statements will be retained in a list attribute called queries. This
144 is used by unit tests to see if queries are generated as expected.
145 """
146
151
153 _logstream.write(self, s)
154
155 s = strip(s)
156 if s == "": return
157
158 if self.buffer_size > 0:
159 self.queries.append(s)
160 if len(self.queries) > self.buffer_size:
161 del self.queries[0]
162
164 self.queries = []
165
167 option_parser.add_option("--show-sql", action="callback",
168 callback=self,
169 help="Log SQL queries and commands (to stderr)")
170
171 log = _log()
172 debug = _debug()
173 sqllog = _sql()
174