Pretty printing of a Object object.
| 249 | |
| 250 | |
| 251 | class Printer: |
| 252 | """ |
| 253 | Pretty printing of a Object object. |
| 254 | """ |
| 255 | |
| 256 | @classmethod |
| 257 | def indent(cls, n): |
| 258 | return '%*s' % (n * 3, ' ') |
| 259 | |
| 260 | def tostr(self, object, indent=-2): |
| 261 | """ get s string representation of object """ |
| 262 | history = [] |
| 263 | return self.process(object, history, indent) |
| 264 | |
| 265 | def process(self, object, h, n=0, nl=False): |
| 266 | """ print object using the specified indent (n) and newline (nl). """ |
| 267 | if object is None: |
| 268 | return 'None' |
| 269 | if isinstance(object, Object): |
| 270 | if len(object) == 0: |
| 271 | return '<empty>' |
| 272 | else: |
| 273 | return self.print_object(object, h, n+2, nl) |
| 274 | if isinstance(object, dict): |
| 275 | if len(object) == 0: |
| 276 | return '<empty>' |
| 277 | else: |
| 278 | return self.print_dictionary(object, h, n+2, nl) |
| 279 | if isinstance(object, (list, tuple)): |
| 280 | if len(object) == 0: |
| 281 | return '<empty>' |
| 282 | else: |
| 283 | return self.print_collection(object, h, n+2) |
| 284 | if isinstance(object, basestring): |
| 285 | return '"%s"' % tostr(object) |
| 286 | return '%s' % tostr(object) |
| 287 | |
| 288 | def print_object(self, d, h, n, nl=False): |
| 289 | """ print complex using the specified indent (n) and newline (nl). """ |
| 290 | s = [] |
| 291 | cls = d.__class__ |
| 292 | md = d.__metadata__ |
| 293 | if d in h: |
| 294 | s.append('(') |
| 295 | s.append(cls.__name__) |
| 296 | s.append(')') |
| 297 | s.append('...') |
| 298 | return ''.join(s) |
| 299 | h.append(d) |
| 300 | if nl: |
| 301 | s.append('\n') |
| 302 | s.append(self.indent(n)) |
| 303 | if cls != Object: |
| 304 | s.append('(') |
| 305 | if isinstance(d, Facade): |
| 306 | s.append(md.facade) |
| 307 | else: |
| 308 | s.append(cls.__name__) |