dtoc: Move BytesToValue() out of the Prop class

This method does not actually use any members of the Prop class. Move it
out of the class so that it is easier to add unit tests.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2019-05-17 22:00:34 -06:00
parent 194b8d5e71
commit 7e6952df36

View File

@ -29,66 +29,8 @@ def CheckErr(errnum, msg):
raise ValueError('Error %d: %s: %s' %
(errnum, libfdt.fdt_strerror(errnum), msg))
class Prop:
"""A device tree property
Properties:
name: Property name (as per the device tree)
value: Property value as a string of bytes, or a list of strings of
bytes
type: Value type
"""
def __init__(self, node, offset, name, bytes):
self._node = node
self._offset = offset
self.name = name
self.value = None
self.bytes = str(bytes)
self.dirty = False
if not bytes:
self.type = TYPE_BOOL
self.value = True
return
self.type, self.value = self.BytesToValue(bytes)
def RefreshOffset(self, poffset):
self._offset = poffset
def Widen(self, newprop):
"""Figure out which property type is more general
Given a current property and a new property, this function returns the
one that is less specific as to type. The less specific property will
be ble to represent the data in the more specific property. This is
used for things like:
node1 {
compatible = "fred";
value = <1>;
};
node1 {
compatible = "fred";
value = <1 2>;
};
He we want to use an int array for 'value'. The first property
suggests that a single int is enough, but the second one shows that
it is not. Calling this function with these two propertes would
update the current property to be like the second, since it is less
specific.
"""
if newprop.type < self.type:
self.type = newprop.type
if type(newprop.value) == list and type(self.value) != list:
self.value = [self.value]
if type(self.value) == list and len(newprop.value) > len(self.value):
val = self.GetEmpty(self.type)
while len(self.value) < len(newprop.value):
self.value.append(val)
def BytesToValue(self, bytes):
def BytesToValue(bytes):
"""Converts a string of bytes into a type and value
Args:
@ -137,6 +79,66 @@ class Prop:
else:
return TYPE_INT, val
class Prop:
"""A device tree property
Properties:
name: Property name (as per the device tree)
value: Property value as a string of bytes, or a list of strings of
bytes
type: Value type
"""
def __init__(self, node, offset, name, bytes):
self._node = node
self._offset = offset
self.name = name
self.value = None
self.bytes = str(bytes)
self.dirty = False
if not bytes:
self.type = TYPE_BOOL
self.value = True
return
self.type, self.value = BytesToValue(bytes)
def RefreshOffset(self, poffset):
self._offset = poffset
def Widen(self, newprop):
"""Figure out which property type is more general
Given a current property and a new property, this function returns the
one that is less specific as to type. The less specific property will
be ble to represent the data in the more specific property. This is
used for things like:
node1 {
compatible = "fred";
value = <1>;
};
node1 {
compatible = "fred";
value = <1 2>;
};
He we want to use an int array for 'value'. The first property
suggests that a single int is enough, but the second one shows that
it is not. Calling this function with these two propertes would
update the current property to be like the second, since it is less
specific.
"""
if newprop.type < self.type:
self.type = newprop.type
if type(newprop.value) == list and type(self.value) != list:
self.value = [self.value]
if type(self.value) == list and len(newprop.value) > len(self.value):
val = self.GetEmpty(self.type)
while len(self.value) < len(newprop.value):
self.value.append(val)
@classmethod
def GetEmpty(self, type):
"""Get an empty / zero value of the given type
@ -183,7 +185,7 @@ class Prop:
bytes: New property value to set
"""
self.bytes = str(bytes)
self.type, self.value = self.BytesToValue(bytes)
self.type, self.value = BytesToValue(bytes)
self.dirty = True
def Sync(self, auto_resize=False):