def shallow_xml_repr(value, host = None, memo = None): if memo is None: memo = list() cycle = value in memo if value not in memo: memo.append(value) if any(value is x for x in (None, True, False)): return tags.span(value) if hasattr(value, '__class__') and value.__class__ in (int, bool, float, object): return tags.span(value) if hasattr(value, '__class__') and value.__class__ in (str, unicode): return shallow_str_xml_repr(value) if cycle: return name_repr(value, memo) if hasattr(value, '__class__') and hasattr(value.__class__, '__rich_shallow_xml_repr__'): return value.__rich_shallow_xml_repr__(host, memo) if hasattr(value, '__class__') and hasattr(value.__class__, '__shallow_xml_repr__'): return value.__shallow_xml_repr__() if hasattr(value, '__class__') and hasattr(value.__class__, '__xml_repr__'): return getattr(value, '__xml_repr__')() if isinstance(value, dict): return shallow_dict_xml_repr(value, host, memo) if isinstance(value, tuple): return shallow_tuple_xml_repr(value, host, memo) if isinstance(value, list): return shallow_list_xml_repr(value, host, memo) return name_repr(value, memo)
def name_repr(value, memo): if value in memo: color = disonance_spectrum[memo.index(value)] else: color = '000000' return tags.span( object.__repr__(value), style = 'color: #%s' % str(color) )
def shallow_str_xml_repr(value): lines = value.split("\n") return tags.span( tags.code( lines[0], style = 'white-space: pre;' ), len(lines) > 1 and tags.b('...') or '', )
def shallow_list_xml_repr(value, host = None, memo = None): return tags.span( name_repr(value, memo), tags.b(' ['), iter_join( tags.b(', '), ( shallow_xml_repr(item, host, memo) for item in value ) ), tags.b(']'), )
def xml_repr(value, host = None, depth = None, memo = None): if depth is None: depth = default_depth if depth == 0: return shallow_xml_repr(value, host, memo) if memo is None: memo = list() cycle = value in memo if value in (None, True, False): return tags.span(value) if hasattr(value, '__class__') and value.__class__ in (int, bool, float, object): return tags.span(value) if hasattr(value, '__class__') and value.__class__ in (str, unicode): return str_xml_repr(value) if value not in memo: memo.append(value) if cycle: return name_repr(value, memo) if hasattr(value, '__class__') and value.__class__ in (list, tuple): # todo: consider a more comprehensive way # of finding iterables return iterable_xml_repr(value, host, depth, memo) if hasattr(value, '__class__') and hasattr(value.__class__, '__rich_xml_repr__'): return value.__rich_xml_repr__(host, depth, memo) if hasattr(value, '__class__') and hasattr(value.__class__, '__xml_repr__'): return value.__xml_repr__() if isinstance(value, dict): return dict_xml_repr(value, host, depth, memo) if isinstance(value, Exception): return exception_xml_repr(value, host, depth, memo) return object_xml_repr(value, host, depth, memo)
def shallow_dict_xml_repr(value, host = None, memo = None): return tags.span( name_repr(value, memo), tags.b(' {'), iter_join( tags.b(', '), ( ( shallow_xml_repr(key, host, memo), tags.b(': '), repr(inner) ) for key, inner in value.items() ) ), tags.b('}'), )