from typing import Dict
import numpy as np import pandas as pd
class DF(pd.DataFrame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for c in self.required:
if c not in self.columns:
self[c] = pd.Series()
types = {c: self.dtype[c] for c in self.columns if c in self.dtype}
typed_columns = list(types.keys())
self[typed_columns] = self.astype(types, copy=False)[typed_columns]
self.attrs['name'] = self.name
class SymbolSourceDF(DF):
name: str = 'symbolsource'
required = frozenset(['symbol', 'address', 'cu'])
dtype = {
'symbol': 'string',
'address': np.int64,
'cu': 'string',
'line': np.int64,
}
class SegmentDF(DF):
name: str = 'segment'
required = frozenset(['type', 'vaddress', 'paddress', 'size'])
dtype = {
'type': 'string',
'vaddress': np.int64,
'paddress': np.int64,
'size': np.int64,
'flags': np.int32
}
class SectionDF(DF):
name: str = 'section'
required = frozenset(['section', 'type', 'address', 'size'])
dtype = {
'section': 'string',
'type': 'string',
'address': np.int64,
'size': np.int64,
'flags': np.int32,
'segment': np.int32,
}
class SymbolDF(DF):
name: str = 'symbol'
required = frozenset(['symbol', 'type', 'address', 'size'])
dtype = {
'symbol': 'string',
'type': 'string',
'address': np.int64,
'size': np.int64,
'shndx': 'string'
}
class ExtentDF(DF):
name: str = 'gap'
required = frozenset(['address', 'size', 'section'])
dtype = {
'address': np.int64,
'size': np.int64,
'section': 'string'
}
class StackDF(DF):
name: str = 'stack'
required = frozenset(['symbol', 'type', 'size'])
dtype = {
'symbol': 'string',
'type': 'string',
'size': np.int64,
'file': 'string',
'line': np.int64,
}
def find_class(df: pd.DataFrame):
if isinstance(df, DF):
return type(df)
for c in [SymbolDF, SectionDF, SegmentDF]:
if c.required.issubset(df.columns):
return c
return None
DFs = Dict[str, DF]