From 5ea9dccf02eb26d146dbc1fdb3106135612820ae Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 8 Nov 2020 20:36:17 -0700 Subject: fdt: Use an Enum for the data type Use an Enum instead of the current ad-hoc constants, so that there is a data type associated with each 'type' value. Signed-off-by: Simon Glass --- tools/dtoc/fdt.py | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'tools/dtoc/fdt.py') diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index 03b86773d5..cb365ca094 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -5,6 +5,7 @@ # Written by Simon Glass # +from enum import IntEnum import struct import sys @@ -22,7 +23,25 @@ from patman import tools # so it is fairly efficient. # A list of types we support -(TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL, TYPE_INT64) = range(5) +class Type(IntEnum): + (BYTE, INT, STRING, BOOL, INT64) = range(5) + + def is_wider_than(self, other): + """Check if another type is 'wider' than this one + + A wider type is one that holds more information than an earlier one, + similar to the concept of type-widening in C. + + This uses a simple arithmetic comparison, since type values are in order + from narrowest (BYTE) to widest (INT64). + + Args: + other: Other type to compare against + + Return: + True if the other type is wider + """ + return self.value > other.value def CheckErr(errnum, msg): if errnum: @@ -41,9 +60,9 @@ def BytesToValue(data): Type of data Data, either a single element or a list of elements. Each element is one of: - TYPE_STRING: str/bytes value from the property - TYPE_INT: a byte-swapped integer stored as a 4-byte str/bytes - TYPE_BYTE: a byte stored as a single-byte str/bytes + Type.STRING: str/bytes value from the property + Type.INT: a byte-swapped integer stored as a 4-byte str/bytes + Type.BYTE: a byte stored as a single-byte str/bytes """ data = bytes(data) size = len(data) @@ -63,21 +82,21 @@ def BytesToValue(data): is_string = False if is_string: if count == 1: - return TYPE_STRING, strings[0].decode() + return Type.STRING, strings[0].decode() else: - return TYPE_STRING, [s.decode() for s in strings[:-1]] + return Type.STRING, [s.decode() for s in strings[:-1]] if size % 4: if size == 1: - return TYPE_BYTE, tools.ToChar(data[0]) + return Type.BYTE, tools.ToChar(data[0]) else: - return TYPE_BYTE, [tools.ToChar(ch) for ch in list(data)] + return Type.BYTE, [tools.ToChar(ch) for ch in list(data)] val = [] for i in range(0, size, 4): val.append(data[i:i + 4]) if size == 4: - return TYPE_INT, val[0] + return Type.INT, val[0] else: - return TYPE_INT, val + return Type.INT, val class Prop: @@ -97,7 +116,7 @@ class Prop: self.bytes = bytes(data) self.dirty = False if not data: - self.type = TYPE_BOOL + self.type = Type.BOOL self.value = True return self.type, self.value = BytesToValue(bytes(data)) @@ -128,9 +147,8 @@ class Prop: update the current property to be like the second, since it is less specific. """ - if newprop.type < self.type: - # Special handling to convert an int into bytes - if self.type == TYPE_INT and newprop.type == TYPE_BYTE: + if self.type.is_wider_than(newprop.type): + if self.type == Type.INT and newprop.type == Type.BYTE: if type(self.value) == list: new_value = [] for val in self.value: @@ -155,11 +173,11 @@ class Prop: Returns: A single value of the given type """ - if type == TYPE_BYTE: + if type == Type.BYTE: return chr(0) - elif type == TYPE_INT: + elif type == Type.INT: return struct.pack('>I', 0); - elif type == TYPE_STRING: + elif type == Type.STRING: return '' else: return True @@ -184,7 +202,7 @@ class Prop: """ self.bytes = struct.pack('>I', val); self.value = self.bytes - self.type = TYPE_INT + self.type = Type.INT self.dirty = True def SetData(self, bytes): -- cgit v1.2.3 From 479dd30f4ac3a8ef417a9aafed24c0cad8a866be Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 8 Nov 2020 20:36:20 -0700 Subject: patman: Drop tools.ToChar() and ToChars() This is useful anymore, since we always want to call chr() in Python 3. Drop it and adjust callers to use chr(). Also drop ToChars() which is no-longer used. Signed-off-by: Simon Glass --- tools/dtoc/fdt.py | 8 ++++---- tools/dtoc/test_fdt.py | 2 +- tools/patman/tools.py | 23 ----------------------- 3 files changed, 5 insertions(+), 28 deletions(-) (limited to 'tools/dtoc/fdt.py') diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index cb365ca094..4a78c73725 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -87,9 +87,9 @@ def BytesToValue(data): return Type.STRING, [s.decode() for s in strings[:-1]] if size % 4: if size == 1: - return Type.BYTE, tools.ToChar(data[0]) + return Type.BYTE, chr(data[0]) else: - return Type.BYTE, [tools.ToChar(ch) for ch in list(data)] + return Type.BYTE, [chr(ch) for ch in list(data)] val = [] for i in range(0, size, 4): val.append(data[i:i + 4]) @@ -152,9 +152,9 @@ class Prop: if type(self.value) == list: new_value = [] for val in self.value: - new_value += [tools.ToChar(by) for by in val] + new_value += [chr(by) for by in val] else: - new_value = [tools.ToChar(by) for by in self.value] + new_value = [chr(by) for by in self.value] self.value = new_value self.type = newprop.type diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py index 10e7f33a59..dc6943f733 100755 --- a/tools/dtoc/test_fdt.py +++ b/tools/dtoc/test_fdt.py @@ -46,7 +46,7 @@ def _GetPropertyValue(dtb, node, prop_name): # Add 12, which is sizeof(struct fdt_property), to get to start of data offset = prop.GetOffset() + 12 data = dtb.GetContents()[offset:offset + len(prop.value)] - return prop, [tools.ToChar(x) for x in data] + return prop, [chr(x) for x in data] class TestFdt(unittest.TestCase): diff --git a/tools/patman/tools.py b/tools/patman/tools.py index 7cd58031e7..00c7013924 100644 --- a/tools/patman/tools.py +++ b/tools/patman/tools.py @@ -423,29 +423,6 @@ def GetBytes(byte, size): """ return bytes([byte]) * size -def ToChar(byte): - """Convert a byte to a character - - This is useful because in Python 2 bytes is an alias for str, but in - Python 3 they are separate types. This function converts an ASCII value to - a value with the appropriate type in either case. - - Args: - byte: A byte or str value - """ - return chr(byte) if type(byte) != str else byte - -def ToChars(byte_list): - """Convert a list of bytes to a str/bytes type - - Args: - byte_list: List of ASCII values representing the string - - Returns: - string made by concatenating all the ASCII values - """ - return ''.join([chr(byte) for byte in byte_list]) - def ToBytes(string): """Convert a str type into a bytes type -- cgit v1.2.3