Package orm2 :: Module debug
[hide private]
[frames] | no frames]

Source Code for Module orm2.debug

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  ##  This file is part of orm, The Object Relational Membrane Version 2. 
  5  ## 
  6  ##  Copyright 2002-2006 by Diedrich Vorberg <diedrich@tux4web.de> 
  7  ## 
  8  ##  All Rights Reserved 
  9  ## 
 10  ##  For more Information on orm see the README file. 
 11  ## 
 12  ##  This program is free software; you can redistribute it and/or modify 
 13  ##  it under the terms of the GNU General Public License as published by 
 14  ##  the Free Software Foundation; either version 2 of the License, or 
 15  ##  (at your option) any later version. 
 16  ## 
 17  ##  This program is distributed in the hope that it will be useful, 
 18  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ##  GNU General Public License for more details. 
 21  ## 
 22  ##  You should have received a copy of the GNU General Public License 
 23  ##  along with this program; if not, write to the Free Software 
 24  ##  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  ## 
 26  ##  I have added a copy of the GPL in the file COPYING 
 27   
 28  # Changelog 
 29  # --------- 
 30  # 
 31  # $Log: debug.py,v $ 
 32  # Revision 1.6  2006/05/13 17:23:42  diedrich 
 33  # Massive docstring update. 
 34  # 
 35  # Revision 1.5  2006/04/28 09:49:27  diedrich 
 36  # Docstring updates for epydoc 
 37  # 
 38  # Revision 1.4  2006/04/15 23:19:32  diedrich 
 39  # Added reset() method to sql log class. 
 40  # 
 41  # Revision 1.3  2005/12/31 18:28:05  diedrich 
 42  # - Updated year in copyright header ;) 
 43  # 
 44  # Revision 1.2  2005/12/18 22:35:46  diedrich 
 45  # - Inheritance 
 46  # - pgsql adapter 
 47  # - first unit tests 
 48  # - some more comments 
 49  # 
 50  # Revision 1.1.1.1  2005/11/20 14:55:46  diedrich 
 51  # Initial import 
 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   
90 -class _logstream:
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 """
96 - def __init__(self):
97 self.verbose = False
98
99 - def write(self, s):
100 if self.verbose: 101 sys.stderr.write(s)
102
103 - def flush(self):
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
115 - def __nonzero__(self):
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):
130 - def add_option(self, option_parser):
131 option_parser.add_option("-v", "--verbose", action="callback", 132 callback=self, help="Be verbose (to stderr)")
133 -class _debug(_logstream):
134 - def add_option(self, option_parser):
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
147 - def __init__(self):
148 _logstream.__init__(self) 149 self.buffer_size = 0 150 self.queries = []
151
152 - def write(self, s):
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
163 - def reset(self):
164 self.queries = []
165
166 - def add_option(self, option_parser):
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