From 8aa384c7789da41135ef5c76c29e49111aa30d92 Mon Sep 17 00:00:00 2001 From: KG Date: Sun, 10 Sep 2023 13:34:44 -0400 Subject: [PATCH] Implement data getters for GDBs and groups --- tivars/types/gdb.py | 48 ++++++++++++++++++++++++++++--------------- tivars/types/group.py | 15 +++++++++----- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/tivars/types/gdb.py b/tivars/types/gdb.py index 04b699b..ba3507f 100644 --- a/tivars/types/gdb.py +++ b/tivars/types/gdb.py @@ -147,7 +147,7 @@ class IndexedEquationConverter(Converter): _T = TIGraphedEquation @classmethod - def get(cls, data: bytes, *, instance: 'TIMonoGDB' = None, **kwargs) -> _T: + def get(cls, data: bytes, *, instance=None, **kwargs) -> _T: """ Converts ``bytes`` -> `TIGraphedEquation` by finding the equation at ``index`` within a GDB @@ -159,7 +159,7 @@ def get(cls, data: bytes, *, instance: 'TIMonoGDB' = None, **kwargs) -> _T: return instance.equations[index] @classmethod - def set(cls, value: _T, *, instance: 'TIMonoGDB' = None, **kwargs) -> bytes: + def set(cls, value: _T, *, instance=None, **kwargs) -> bytes: """ Converts ``bytes`` -> `TIGraphedEquation` by modifying the equation at ``index`` within a GDB @@ -181,7 +181,7 @@ def set(cls, value: _T, *, instance: 'TIMonoGDB' = None, **kwargs) -> bytes: data += b''.join(equation.raw.calc_data for equation in equations) # Set colors (if they exist) - if color := color_data(instance): + if color := instance.get_color_data(): data += b'84C' for i in range(0, instance.num_equations, instance.num_equations // instance.num_styles): data += equations[i].color @@ -298,15 +298,6 @@ def load_string(self, string: str, *, model: TIModel = None): self.load_equation(equation) -def color_data(gdb: 'TIMonoGDB') -> bytes: - data = io.BytesIO(gdb.calc_data[gdb.offset + gdb.num_styles:]) - temp = TIGraphedEquation() - for i in range(gdb.num_equations): - temp.load_data_section(data) - - return data.read() - - class TIMonoGDB(SizedEntry, register=True): """ Base class for all GDB entries @@ -466,7 +457,32 @@ def equations(self) -> tuple[TIGraphedEquation, ...]: :return: This GDB's stored equations """ - data = io.BytesIO(self.calc_data[self.offset:]) + return self.get_equations() + + def get_color_data(self, data: bytes = None) -> bytes: + """ + Finds the color portion of a GDB if it exists + + :param data: The data to find the color portion of (defaults to this GDB's data) + :return: The color portion of ``data``, which may be empty + """ + + data = io.BytesIO(data or self.calc_data[self.offset + self.num_styles:]) + temp = TIGraphedEquation() + for i in range(self.num_equations): + temp.load_data_section(data) + + return data.read() + + def get_equations(self, data: bytes = None) -> tuple[TIGraphedEquation, ...]: + """ + Extracts the equations stored in a GDB into a ``tuple`` + + :param data: The data to extract equations from (defaults to this GDB's data) + :return: A ``tuple`` of equations stored in ``data`` + """ + + data = io.BytesIO(data or self.calc_data[self.offset:]) equations = tuple(TIGraphedEquation(name=name) for name in self.equation_names) # Load styles @@ -492,10 +508,10 @@ def equations(self) -> tuple[TIGraphedEquation, ...]: return equations def get_min_os(self, data: bytes = None) -> OsVersion: - return max(map(TIGraphedEquation.get_min_os, self.equations), default=OsVersions.INITIAL) + return max([TIGraphedEquation.get_min_os(eq) for eq in self.get_equations(data)], default=OsVersions.INITIAL) def get_version(self, data: bytes = None) -> int: - return max(map(TIGraphedEquation.get_version, self.equations), default=0x00) + return max([TIGraphedEquation.get_version(eq) for eq in self.get_equations(data)], default=0x00) @Loader[dict] def load_dict(self, dct: dict): @@ -606,7 +622,7 @@ def string(self) -> str: return json.dumps(self.dict()) def coerce(self): - if color_data(self): + if self.get_color_data(): self.__class__ = TIGDB self.coerce() else: diff --git a/tivars/types/group.py b/tivars/types/group.py index bdc31cf..87be4f5 100644 --- a/tivars/types/group.py +++ b/tivars/types/group.py @@ -85,17 +85,22 @@ def group(entries: list[TIEntry], *, name: str = "GROUP") -> 'TIGroup': return group def get_min_os(self, data: bytes = None) -> OsVersion: - return max([entry.get_min_os() for entry in self.ungroup()], default=OsVersions.INITIAL) + return max([entry.get_min_os() for entry in self.ungroup(data)], default=OsVersions.INITIAL) def get_version(self, data: bytes = None) -> int: - return max([entry.get_version() for entry in self.ungroup()], default=0x00) + return max([entry.get_version() for entry in self.ungroup(data)], default=0x00) - def ungroup(self) -> list[TIEntry]: + def ungroup(self, data: bytes = None) -> list[TIEntry]: """ - :return: A ``list`` of entries stored in this group + Ungroups a group object into a ``list`` of its entries + + All VAT data is ignored. + + :param data: The data to ungroup (defaults to this group's data) + :return: A ``list`` of entries stored in ``data`` """ - data = io.BytesIO(self.data[:]) + data = io.BytesIO(data or self.data[:]) entries = [] index = 1