Merge branch 'master' of git.covalent.cz:overwatch/over
This commit is contained in:
commit
76ab4c1d32
2 changed files with 60 additions and 9 deletions
34
over/misc.py
34
over/misc.py
|
@ -130,8 +130,10 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
|
||||||
|
|
||||||
output_io = io.StringIO() if not output else output
|
output_io = io.StringIO() if not output else output
|
||||||
|
|
||||||
if type(data) is not bytes:
|
try:
|
||||||
raise ValueError("data must be bytes")
|
data = bytes(data)
|
||||||
|
except:
|
||||||
|
raise ValueError("data must be bytes or similar")
|
||||||
|
|
||||||
offset_figures = math.ceil(math.log2(len(data)) / 8) * 2 if data else 2
|
offset_figures = math.ceil(math.log2(len(data)) / 8) * 2 if data else 2
|
||||||
format_str = "%%0%dx " %(offset_figures)
|
format_str = "%%0%dx " %(offset_figures)
|
||||||
|
@ -189,3 +191,31 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show
|
||||||
if not output:
|
if not output:
|
||||||
output_io.seek(0)
|
output_io.seek(0)
|
||||||
return output_io.read()
|
return output_io.read()
|
||||||
|
|
||||||
|
# --------------------------------------------------
|
||||||
|
|
||||||
|
def raw_to_hex(raw, spaces=True):
|
||||||
|
"""
|
||||||
|
Converts a bytearray (or bytes) into its textual hexadecimal representation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
output = []
|
||||||
|
|
||||||
|
for o in raw:
|
||||||
|
output.append(hex(o)[2:].zfill(2))
|
||||||
|
|
||||||
|
return (" " if spaces else "").join(output)
|
||||||
|
|
||||||
|
def hex_to_raw(text):
|
||||||
|
"""
|
||||||
|
Converts a hexadecimal representation of a byte array into a bytearray.
|
||||||
|
"""
|
||||||
|
|
||||||
|
output = []
|
||||||
|
|
||||||
|
text = text.replace(" ", "")
|
||||||
|
|
||||||
|
for i in range(len(text)//2):
|
||||||
|
output.append(int(text[2*i:2*i+2], 16))
|
||||||
|
|
||||||
|
return bytearray(output)
|
||||||
|
|
|
@ -5,9 +5,9 @@ from collections import OrderedDict
|
||||||
|
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
|
|
||||||
class ndict(OrderedDict):
|
class ndict:
|
||||||
"""
|
"""
|
||||||
An OrderedDict subclass whose keys are exposed as attributes.
|
An OrderedDict wrapper whose keys are exposed as attributes.
|
||||||
|
|
||||||
>>> d = ndict()
|
>>> d = ndict()
|
||||||
>>> d.alpha = 1
|
>>> d.alpha = 1
|
||||||
|
@ -20,17 +20,38 @@ class ndict(OrderedDict):
|
||||||
{"alpha": 1, "beta": 42}
|
{"alpha": 1, "beta": 42}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
__methods__ = ["values", "items"]
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
object.__setattr__(self, "d", OrderedDict(*args, **kwargs))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "|" + repr(self.d)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self.d.__iter__()
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
"""
|
"""
|
||||||
@while looking up an attribute
|
@while looking up an attribute
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if name in self:
|
safe_name = name.replace("_", "-")
|
||||||
return self[name]
|
|
||||||
elif name.replace("_", "-") in self:
|
if name in self.__methods__:
|
||||||
return self[name.replace("_", "-")]
|
return getattr(self.d, name)
|
||||||
|
if name in self.d:
|
||||||
|
return self.d[name]
|
||||||
|
elif safe_name in self.d:
|
||||||
|
return self.d[safe_name]
|
||||||
else:
|
else:
|
||||||
raise AttributeError('"ndict" object has no attribute "%s"' %(name))
|
raise AttributeError('"ndict" object has no attribute "%s"' %(name))
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
self[name] = value
|
self.d[name] = value
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return self.d[key]
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self.d[key] = value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue