Package Ganga :: Package GPIDev :: Package Base :: Module VPrinter
[hide private]
[frames] | no frames]

Source Code for Module Ganga.GPIDev.Base.VPrinter

  1  ################################################################################ 
  2  # Ganga Project. http://cern.ch/ganga 
  3  # 
  4  # $Id: VPrinter.py,v 1.1 2008-07-17 16:40:52 moscicki Exp $ 
  5  ################################################################################ 
  6  from Ganga.GPIDev.Base.Objects import GangaObject 
  7  from Ganga.GPIDev.Base.Proxy import isProxy, isType, runProxyMethod 
  8   
9 -def quoteValue(value, selection):
10 """A quoting function. Used to get consistent formatting""" 11 #print "Quoting",repr(value),selection 12 if type(value) == type(''): 13 if selection == 'copyable': 14 15 value = value.replace('"',R'\"') 16 value = value.replace("'",R"\'") 17 18 #DISABLED 19 ## valueList = list( value ) 20 ## for i in range( len( value ) ): 21 ## c = value[ i ] 22 ## if c in [ "'", '"' ]: 23 ## valueList[ i ] = "\\" + c 24 ## value = "".join( valueList ) 25 if 1 + value.find( "\n" ): 26 #print 'Quote result',"'''" + value + "'''" 27 return "'''" + value + "'''" 28 #print 'Quote result',"'"+value+"'" 29 return "'"+value+"'" 30 #print 'Quote result',value 31 return value
32
33 -def indent(level):
34 return ' '*(level-1)*3
35 36 # A visitor to print the object tree.
37 -class VPrinter(object):
38 # Arguments: 39 # out: file-like output stream where to print, default sys.stdout 40 # selection: string specifying properties to print (default ''): 41 # 'all' - print all properties 42 # 'copyable' - print only copyable properties 43 # any other string - print unhidden properties
44 - def __init__(self,out=None,selection=''):
45 self.level = 0 46 self.nocomma = 1 47 self.selection = selection 48 if out: 49 self.out = out 50 else: 51 import sys 52 self.out = sys.stdout 53 54 self.empty_body = 0 # detect whether the body is empty to handle the comma correctly in this case too
55
56 - def indent(self):
57 return indent(self.level)
58
59 - def comma(self,force=0):
60 if not self.nocomma or force: 61 print >> self.out, "," 62 63 self.nocomma = 0
64
65 - def nodeBegin(self,node):
66 self.level += 1 67 print >> self.out,node._schema.name,'(' 68 self.nocomma = 1 69 self.empty_body = 1
70
71 - def nodeEnd(self,node):
72 if self.empty_body: 73 print >> self.out, self.indent(), ')', 74 self.nocomma = 0 75 else: 76 if self.nocomma: 77 #print >> self.out, 'NOCOMMA', 78 print >> self.out, ')', 79 else: 80 #print >> self.out, 'COMMA', 81 print >> self.out,'\n',self.indent(),')', 82 83 self.level -= 1 84 if self.level == 0: print >> self.out,'\n'
85
86 - def showAttribute( self, node, name ):
87 visible = False 88 if self.selection == 'all': 89 visible = True 90 elif self.selection == 'copyable': 91 if node._schema.getItem(name)['copyable']: 92 if not node._schema.getItem(name)['hidden']: 93 visible = True 94 else: 95 if not node._schema.getItem(name)['hidden']: 96 visible = True 97 return visible
98
99 - def simpleAttribute(self,node,name, value,sequence):
100 if self.showAttribute( node, name ): 101 self.empty_body = 0 102 self.comma() 103 # DISABLED 104 #print '*'*20,name 105 #if sequence: 106 # print 'transformation:',repr(value) 107 # value = value.toString() 108 # print 'into',repr(value) 109 #else: 110 # print 'no transformation' 111 print >> self.out, self.indent(), name, '=', self.quote(value),
112
113 - def acceptOptional(self,s):
114 if s is None: 115 print >> self.out, None, 116 else: 117 runProxyMethod(s,'accept',self)
118
119 - def componentAttribute(self,node,name,subnode,sequence):
120 if self.showAttribute( node, name ): 121 self.empty_body = 0 122 self.comma() 123 print >> self.out, self.indent(), name, '=', 124 if sequence: 125 print >> self.out, '[', 126 for s in subnode: 127 self.acceptOptional(s) 128 print >> self.out,',', 129 print >> self.out, ']', 130 else: 131 self.acceptOptional(subnode)
132
133 - def quote(self,x):
134 return quoteValue(x, self.selection)
135
136 -class VSummaryPrinter(VPrinter):
137 """A class for printing summeries of object properties in a customisable way.""" 138
139 - def __init__(self,level, verbosity_level, whitespace_marker, out=None,selection=''):
140 super(VSummaryPrinter,self).__init__(out,selection) 141 self.level = level 142 self.verbosity_level = verbosity_level 143 self.whitespace_marker = whitespace_marker
144
145 - def _CallSummaryPrintMember(self, node, name, value):
146 """Checks to see whether there is a summary_print function pointer 147 available in the schema object. If so it uses it and returns True 148 otherwise it returns False. 149 """ 150 function_pointer_available = False 151 152 #check whether a print_summary function has been defined 153 print_summary = node._schema.getItem(name)['summary_print'] 154 if print_summary != None: 155 fp = getattr(node,print_summary) 156 str_val = fp(value,self.verbosity_level) 157 self.empty_body = 0 158 self.comma() 159 print >> self.out, self.indent(), name, '=', self.quote(str_val), 160 function_pointer_available = True 161 return function_pointer_available
162
163 - def _CallPrintSummaryTree(self, obj):
164 import StringIO 165 sio = StringIO.StringIO() 166 runProxyMethod(obj, 'printSummaryTree',self.level, self.verbosity_level, self.indent(), sio, self.selection) 167 result = sio.getvalue() 168 if result.endswith('\n'): 169 result = result[0:-1] 170 print >>self.out, result,
171
172 - def simpleAttribute(self, node, name, value, sequence):
173 """Overrides the baseclass method. Tries to print a summary of the attribute.""" 174 if not self.showAttribute( node, name ): 175 return 176 if self._CallSummaryPrintMember(node,name,getattr(node,name)): 177 return 178 179 if sequence: 180 self.empty_body = 0 181 self.comma() 182 print >> self.out, self.indent(), name, '=', 183 self._CallPrintSummaryTree(value) 184 return 185 186 #just go back to default behaviour 187 super(VSummaryPrinter,self).simpleAttribute(node, name, value, sequence)
188
189 - def componentAttribute(self,node,name,subnode,sequence):
190 if not self.showAttribute( node, name ): 191 return 192 if self._CallSummaryPrintMember(node,name,subnode): 193 return 194 from Objects import GangaObject 195 if isType(subnode,GangaObject): 196 self.empty_body = 0 197 self.comma() 198 print >> self.out, self.indent(), name, '=', 199 self._CallPrintSummaryTree(subnode) 200 return 201 202 #just go back to default behaviour 203 super(VSummaryPrinter,self).componentAttribute(node, name, subnode, sequence)
204
205 -def full_print(obj, out = None):
206 """Print the full contents of a GPI object without abbreviation.""" 207 import sys 208 if out == None: 209 out = sys.stdout 210 211 from Ganga.GPIDev.Lib.GangaList import GangaList 212 if isType(obj,GangaList): 213 obj_len = len(obj) 214 if obj_len == 0: 215 print >>out, '[]', 216 else: 217 import StringIO 218 outString = '[' 219 count = 0 220 for x in obj: 221 if isinstance(x, GangaObject): 222 sio = StringIO.StringIO() 223 x.printTree(sio) 224 result = sio.getvalue() 225 #remove trailing whitespace and newlines 226 outString += result.rstrip() 227 else: 228 result = str(x) 229 #remove trailing whitespace and newlines 230 outString += result.rstrip() 231 count += 1 232 if count != obj_len: outString += ', ' 233 outString += ']' 234 print >>out, outString, 235 return 236 237 if isProxy(obj): 238 import StringIO 239 sio = StringIO.StringIO() 240 runProxyMethod(obj,'printTree',sio) 241 print >>out, sio.getvalue(), 242 else: 243 print >>out, str(obj),
244