def EvaluateCondition(self, Cond): Item = None ReplaceList = [] Parts = re.findall("\$(\w+)(\.\w+)?", Cond) for Part in Parts: if len(Part) > 1 and len(Part[1]) > 0: Name = Part[0] + '_' + Part[1][1:] else: Name = Part[0] Value = None Item = self.GetConfigDataItemFromName(Name, False) if Item: try: Value = '0x%x' % Bytes2Val( self.CfgDataObj.ValueToByteArray( Item['value'], Item['length'])) except: Value = None ReplaceList.append(('$' + ''.join(Part), Value)) OrgCond = Cond Result = True ReplaceList.sort(key=lambda x: len(x[0]), reverse=True) for Each in ReplaceList: if Each[1] is None: break Cond = Cond.replace(Each[0], Each[1]) try: Result = True if eval(Cond) else False except: print("WARNING: Condition '%s' is invalid!" % OrgCond) return Result
def NormalizeValueStr(self, Item, ValueStr): IsArray = True if Item['value'].startswith('{') else False if IsArray and not ValueStr.startswith('{'): ValueStr = "{ %s }" % ValueStr try: OldBytes = self.CfgDataObj.ValueToByteArray( Item['value'], Item['length']) NewBytes = self.CfgDataObj.ValueToByteArray( ValueStr, Item['length']) if OldBytes == NewBytes: NewValue = Item['value'] else: if IsArray: NewValue = Bytes2Str(NewBytes) else: if Item['value'].startswith('0x'): HexLen = Item['length'] * 2 if len(Item['value']) == HexLen + 2: Fmt = '0x%%0%dX' % HexLen else: Fmt = '0x%X' else: Fmt = '%d' NewValue = Fmt % Bytes2Val(NewBytes) except: NewValue = Item['value'] return NewValue
def GenerateDeltaFile(self, DeltaFile, Full=False): NewData = self.CfgDataObj.GenerateBinaryArray() Lines = [] TagName = '' Level = 0 PlatformId = None DefPlatformId = 0 for Item in self.CfgDataObj._CfgItemList: if Level == 0 and Item['embed'].endswith(':START'): TagName = Item['embed'].split(':')[0] Level += 1 Start = Item['offset'] End = Start + Item['length'] FullName = '%s.%s' % (TagName, Item['cname']) if 'PLATFORMID_CFG_DATA.PlatformId' == FullName: DefPlatformId = Bytes2Val(self.OrgCfgDataBin[Start:End]) if NewData[Start:End] != self.OrgCfgDataBin[Start:End] or ( Full and Item['name'] and (Item['cname'] != 'Dummy')): if not Item['subreg']: Text = '%-40s | %s' % (FullName, Item['value']) if 'PLATFORMID_CFG_DATA.PlatformId' == FullName: PlatformId = Array2Val(Item['value']) else: Lines.append(Text) else: OldArray = self.OrgCfgDataBin[Start:End] NewArray = NewData[Start:End] for SubItem in Item['subreg']: NewBitValue = self.CfgDataObj.GetBsfBitFields( SubItem, NewArray) OldBitValue = self.CfgDataObj.GetBsfBitFields( SubItem, OldArray) if OldBitValue != NewBitValue or ( Full and Item['name'] and (Item['cname'] != 'Dummy')): if SubItem['cname'].startswith(Item['cname']): Offset = len(Item['cname']) + 1 FieldName = '%s.%s' % ( FullName, SubItem['cname'][Offset:]) Text = '%-40s | %s' % (FieldName, SubItem['value']) Lines.append(Text) if Item['embed'].endswith(':END'): EndTagName = Item['embed'].split(':')[0] if EndTagName == TagName: Level -= 1 if PlatformId is None or DefPlatformId == PlatformId: PlatformId = DefPlatformId print( "WARNING: 'PlatformId' configuration is same as default %d!" % PlatformId) Lines.insert( 0, '%-40s | %s\n\n' % ('PLATFORMID_CFG_DATA.PlatformId', '0x%04X' % PlatformId)) self.CfgDataObj.WriteDeltaFile(DeltaFile, PlatformId, Lines)
def __init__(self, Parent, ColHdr, Bins): Cols = len(ColHdr) ColByteLen = [] for Col in range(Cols): #Columns ColByteLen.append(int(ColHdr[Col].split(':')[1])) ByteLen = sum(ColByteLen) Rows = (len(Bins) + ByteLen - 1) // ByteLen self.Rows = Rows self.Cols = Cols self.ColByteLen = ColByteLen self.ColHdr = ColHdr self.Size = len(Bins) self.LastDir = '' style = ttk.Style() style.configure("Custom.Treeview.Heading", font=('Calibri', 10, 'bold'), foreground="blue") ttk.Treeview.__init__(self, Parent, height=Rows, columns=[''] + ColHdr, show='headings', style="Custom.Treeview", selectmode='none') self.bind("<Button-1>", self.Click) self.bind("<FocusOut>", self.FocusOut) self.entry = ValidatingEntry(self, width=4, justify=CENTER) self.heading(0, text='LOAD') self.column(0, width=60, stretch=0, anchor=CENTER) for Col in range(Cols): #Columns Text = ColHdr[Col].split(':')[0] ByteLen = int(ColHdr[Col].split(':')[1]) self.heading(Col + 1, text=Text) self.column(Col + 1, width=self.ToCellWidth(ByteLen), stretch=0, anchor=CENTER) Idx = 0 for Row in range(Rows): #Rows Text = '%04X' % (Row * len(ColHdr)) Vals = ['%04X:' % (Cols * Row)] for Col in range(Cols): #Columns if Idx >= len(Bins): break ByteLen = int(ColHdr[Col].split(':')[1]) Value = Bytes2Val(Bins[Idx:Idx + ByteLen]) Hex = ("%%0%dX" % (ByteLen * 2)) % Value Vals.append(Hex) Idx += ByteLen self.insert('', 'end', values=tuple(Vals)) if Idx >= len(Bins): break
def __init__(self, Parent, ColHdr, Bins): Frame.__init__(self, Parent) Idx = 0 Cols = len(ColHdr) if Cols > 0: Rows = (len(Bins) + Cols - 1) // Cols self.Row = Rows self.Col = Cols ColAdj = 2 RowAdj = 1 for Col in range(Cols): #Columns Text = ColHdr[Col].split(':')[0] Header = Label(self, text=Text) Header.grid(row=0, column=Col + ColAdj) for Row in range(Rows): #Rows Text = '%04X' % (Row * len(ColHdr)) Header = Label(self, text=Text) Header.grid(row=Row + RowAdj, column=0, columnspan=1, sticky='ewsn') for Col in range(Cols): #Columns if Idx >= len(Bins): break ByteLen = int(ColHdr[Col].split(':')[1]) Value = Bytes2Val(Bins[Idx:Idx + ByteLen]) Hex = ("%%0%dX" % (ByteLen * 2)) % Value ValidatingEntry(self, width=ValidatingEntry.ToCellWidth(ByteLen), justify=CENTER, value=Hex).grid(row=Row + RowAdj, column=Col + ColAdj) Idx += ByteLen if Idx >= len(Bins): break
def RefreshBin(self, Bins): if not Bins: return # Reload binary into widget BinLen = len(Bins) for Row in range(self.Rows): Iid = self.get_children()[Row] for Col in range(self.Cols): Idx = Row * sum(self.ColByteLen) + sum(self.ColByteLen[:Col]) ByteLen = self.ColByteLen[Col] if Idx + ByteLen <= self.Size: ByteLen = int(self.ColHdr[Col].split(':')[1]) if Idx + ByteLen > BinLen: Val = 0 else: Val = Bytes2Val(Bins[Idx:Idx + ByteLen]) HexVal = ("%%0%dX" % (ByteLen * 2)) % Val self.set(Iid, Col + 1, HexVal)
def AddConfigItem(self, Item, Row): Parent = self.RightGrid Name = Label(Parent, text=Item['name'], anchor="w") Parts = Item['type'].split(',') Type = Parts[0] Widget = None if Type == "Combo": # Build if Item['option'] in self.CfgDataObj._BuidinOption: OptList = [('0', 'Disable'), ('1', 'Enable')] else: OptList = self.CfgDataObj.GetItemOptionList(Item) CurrentValue = self.CfgDataObj.ValueToByteArray( Item['value'], Item['length']) OptionList = [] Current = None for Idx, Option in enumerate(OptList): OptionValue = self.CfgDataObj.ValueToByteArray( Option[0], Item['length']) if OptionValue == CurrentValue: Current = Idx OptionList.append(Option[1]) Widget = ttk.Combobox(Parent, value=OptionList, state="readonly") Widget.bind("<<ComboboxSelected>>", self.ComboSelectChanged) if Current is None: print('WARNING: Value "%s" is an invalid option for "%s" !' % (Bytes2Val(CurrentValue), Item['cname'])) else: Widget.current(Current) elif Type in ["EditNum", "EditText"]: TxtVal = StringVar() Widget = Entry(Parent, textvariable=TxtVal) Value = Item['value'].strip("'") if Type in ["EditText"]: TxtVal.trace( 'w', lambda *args: self.LimitEntrySize(TxtVal, Item['length'])) elif Type in ["EditNum"]: Value = Item['value'].strip("{").strip("}").strip() Widget.bind("<FocusOut>", self.EditNumFinished) TxtVal.set(Value) elif Type in ["Table"]: Bins = self.CfgDataObj.ValueToByteArray(Item['value'], Item['length']) ColHdr = Item['option'].split(',') Widget = CustomTable(Parent, ColHdr, Bins) if Widget: Ttp = CreateToolTip(Widget, Item['help']) self.SetObjectName(Name, 'LABEL_' + Item['cname']) self.SetObjectName(Widget, Item['cname']) Name.grid(row=Row, column=0, padx=10, pady=5, sticky="nsew") Widget.grid(row=Row + 1, column=0, padx=10, pady=5, sticky="nsew")