Package Peach :: Package Engine :: Module common
[hide private]

Source Code for Module Peach.Engine.common

  1  ''' 
  2  Common functions and classes. 
  3   
  4  @author: Michael Eddington 
  5  @version: $Id$ 
  6  ''' 
  7   
  8  # 
  9  # Copyright (c) 2008 Michael Eddington 
 10  # 
 11  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 12  # of this software and associated documentation files (the "Software"), to deal 
 13  # in the Software without restriction, including without limitation the rights  
 14  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 15  # copies of the Software, and to permit persons to whom the Software is  
 16  # furnished to do so, subject to the following conditions: 
 17  # 
 18  # The above copyright notice and this permission notice shall be included in     
 19  # all copies or substantial portions of the Software. 
 20  # 
 21  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 22  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 23  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 24  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 25  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 26  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 27  # SOFTWARE. 
 28  # 
 29   
 30  # Authors: 
 31  #   Michael Eddington (mike@phed.org) 
 32   
 33  # $Id$ 
 34   
35 -class Holder:
36 ''' 37 Holds static stuff 38 ''' 39 40 globals = None 41 locals = None
42
43 -class SoftException(Exception):
44 ''' 45 Soft exceptions should end the current test 46 iteration, but not the run. They are "recoverable" 47 or "try again" errors. 48 ''' 49 pass
50
51 -class HardException(Exception):
52 ''' 53 Hard exceptions are non-recoverable and should 54 end the fuzzing run. 55 ''' 56 pass
57
58 -class RedoTestException(SoftException):
59 ''' 60 Indicate we should re-run the current test. A recoverable error 61 occured. The main enging loop should only retry the test case 3 62 times before turning this into a hard exception. 63 ''' 64 pass
65
66 -class PeachException(HardException):
67 ''' 68 Peach exceptions are specialized hard exceptions. The 69 message contained in a PeachException is presentable to the 70 user w/o any stack trace, etc. 71 72 Examples would be: 73 74 "Error: The DataModel element requires a name attribute." 75 76 ''' 77
78 - def __init__(self, msg, module = "Unknown"):
79 Exception.__init__(self, msg) 80 self.module = module 81 self.msg = msg
82
83 -def peachEval(code, environment = False):
84 ''' 85 Eval using the Peach namespace stuffs 86 ''' 87 88 return eval(code, Holder.globals, Holder.locals)
89 90
91 -def evalEvent(code, environment):
92 ''' 93 Eval python code returning result. 94 95 code - String 96 environment - Dictionary, keys are variables to place in local scope 97 ''' 98 99 globalScope = {} 100 localScope = {} 101 102 if Holder.globals != None: 103 for k in Holder.globals: 104 globalScope[k] = Holder.globals[k] 105 106 if Holder.locals != None: 107 for k in Holder.locals: 108 localScope[k] = Holder.locals[k] 109 110 for k in environment.keys(): 111 globalScope[k] = environment[k] 112 localScope[k] = environment[k] 113 114 ret = eval(code, globalScope, localScope) 115 116 return ret
117
118 -class _Getch:
119 """Gets a single character from standard input. Does not echo to the 120 screen."""
121 - def __init__(self):
122 try: 123 self.impl = _GetchWindows() 124 except ImportError: 125 self.impl = _GetchUnix()
126
127 - def __call__(self): return self.impl()
128 129
130 -class _GetchUnix:
131 - def __init__(self):
132 import tty, sys
133
134 - def __call__(self):
135 import sys, tty, termios 136 fd = sys.stdin.fileno() 137 old_settings = termios.tcgetattr(fd) 138 try: 139 tty.setraw(sys.stdin.fileno()) 140 ch = sys.stdin.read(1) 141 finally: 142 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 143 return ch
144 145
146 -class _GetchWindows:
147 - def __init__(self):
148 import msvcrt
149
150 - def __call__(self):
151 import msvcrt 152 return msvcrt.getch()
153 154 155 getch = _Getch() 156 157 # end 158