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
|
||||
|
||||
if type(data) is not bytes:
|
||||
raise ValueError("data must be bytes")
|
||||
try:
|
||||
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
|
||||
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:
|
||||
output_io.seek(0)
|
||||
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.alpha = 1
|
||||
|
@ -20,17 +20,38 @@ class ndict(OrderedDict):
|
|||
{"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):
|
||||
"""
|
||||
@while looking up an attribute
|
||||
"""
|
||||
|
||||
if name in self:
|
||||
return self[name]
|
||||
elif name.replace("_", "-") in self:
|
||||
return self[name.replace("_", "-")]
|
||||
safe_name = name.replace("_", "-")
|
||||
|
||||
if name in self.__methods__:
|
||||
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:
|
||||
raise AttributeError('"ndict" object has no attribute "%s"' %(name))
|
||||
|
||||
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