Skip to content

Commit

Permalink
Implement data getters for GDBs and groups
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Sep 10, 2023
1 parent 2a497c5 commit 8aa384c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
48 changes: 32 additions & 16 deletions tivars/types/gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 10 additions & 5 deletions tivars/types/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8aa384c

Please sign in to comment.