diff --git a/kaitaistruct.py b/kaitaistruct.py index 73c1e12..6d265ad 100644 --- a/kaitaistruct.py +++ b/kaitaistruct.py @@ -15,6 +15,18 @@ # __version__ = '0.8' +from RecursionSafe import RecursionSafe + +recSafe=RecursionSafe() + +@recSafe.wrap +def repr_generator_for_all_props(self): + """Generator to use in own __repr__ functions.""" + return ( + "".join(( str(k), "=", repr(getattr(self, k)) )) + for k in dir(self) + if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type) + ) class KaitaiStruct(object): def __init__(self, stream): @@ -26,6 +38,16 @@ def __enter__(self): def __exit__(self, *args, **kwargs): self.close() + def __repr__(self): + return "".join( + ( + self.__class__.__name__, + "(", + ", ".join( repr_generator_for_all_props(self) ), + ")" + ) + ) + def close(self): self._io.close() diff --git a/setup.py b/setup.py index e6ea7af..4674684 100755 --- a/setup.py +++ b/setup.py @@ -44,4 +44,6 @@ ], keywords='kaitai,struct,construct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development', py_modules=["kaitaistruct"], + requires=["RecursionSafe"], + dependency_links=["git+https://github.com/KOLANICH/RecursionSafe.py.git"], )