def _change_value(self, snbt: SNBTType, snbt_text: wx.StaticText): try: nbt = amulet_nbt.from_snbt(snbt) except: snbt_text.SetLabel("Invalid SNBT") snbt_text.SetBackgroundColour((255, 200, 200)) else: if isinstance(nbt, PropertyDataTypes): snbt_text.SetLabel(nbt.to_snbt()) snbt_text.SetBackgroundColour(wx.NullColour) else: snbt_text.SetLabel(f"{nbt.__class__.__name__} not valid") snbt_text.SetBackgroundColour((255, 200, 200)) self.Layout()
class TipsFrame(Dialog): """ Represents a tips frame, a frame for displaying tips. :version: $Revision: 1.9 $ :author: C.Dutoit :contact: [email protected] """ def __init__(self, parent): """ """ # Check import sys if sys.platform == "win32": return # Initialize the dialog box super().__init__( parent, -1, _("Tips"), DefaultPosition, Size(DEFAULT_WIDTH, DEFAULT_HEIGHT), RESIZE_BORDER | SYSTEM_MENU | CAPTION | FRAME_FLOAT_ON_PARENT | STAY_ON_TOP) # Normalize tips from org.pyut.general.LineSplitter import LineSplitter ls = LineSplitter() dc = ClientDC(self) for i in range(len(Tips)): tip = ls.split(Tips[i], dc, int(DEFAULT_WIDTH * 0.8)) Tips[i] = "" for line in tip: Tips[i] += line + "\n" # tip = "" # for line in Tips[i].split("\n"): # newLine = "" # for word in line.split(" "): # if len(newLine) + len(word) > 59: # tip += newLine + "\n" # newLine = "" # newLine += word + " " # tip += newLine # Tips[i] = tip # Set current tips self._prefs = PyutPreferences() self._currentTip = self._prefs[PyutPreferences.CURRENT_TIP] if self._currentTip is None: self._currentTip = 0 else: self._currentTip = int(self._currentTip) # Add icon # fileName = resource_filename(IMG_PKG, 'TipsLogo.bmp') # icon = Icon(fileName, BITMAP_TYPE_BMP) # self.SetIcon(icon) # self.Center(BOTH) # Center on the screen self.Center(dir=VERTICAL) self.AcceptsFocus() # Create controls # bmp: Bitmap = org.pyut.resources.img.ImgTipsFrameTipsLogo.embeddedImage.GetBitmap() bmp: Bitmap = TipsLogo.GetBitmap() self._picture = StaticBitmap(self, -1, bmp) tip = Tips[self._currentTip] self._label = StaticText(self, -1, tip, size=Size(DEFAULT_WIDTH * 0.8, DEFAULT_HEIGHT * 0.8), style=ST_NO_AUTORESIZE) nextTipButton = Button(self, ID_SET_NEXT_TIP, _("&Next tip")) previousTipButton = Button(self, ID_SET_PREVIOUS_TIP, _("&Previous tip")) self._chkShowTips = CheckBox(self, ID_CHK_SHOW_TIPS, _("&Show tips at startup")) showTips: bool = self._prefs.showTipsOnStartup() self._chkShowTips.SetValue(showTips) # Upper sizer upSizer = BoxSizer(HORIZONTAL) upSizer.Add(self._picture, 0, ALL | ALIGN_CENTER, 5) upSizer.Add(self._label, 1, ALL | ALIGN_CENTER, 5) # Lower sizer loSizer = BoxSizer(HORIZONTAL) loSizer.Add(previousTipButton, 0, ALL | ALIGN_CENTER, 5) loSizer.Add(nextTipButton, 0, ALL | ALIGN_CENTER, 5) loSizer.Add(Button(self, ID_OK, "&Ok"), 0, ALL | ALIGN_CENTER, 5) # Main sizer self.SetAutoLayout(True) mainSizer = BoxSizer(VERTICAL) mainSizer.Add(upSizer, 0, ALL | ALIGN_CENTER, 5) mainSizer.Add(self._chkShowTips, 0, ALL | ALIGN_CENTER, 5) mainSizer.Add(loSizer, 0, ALL | ALIGN_CENTER, 5) self.SetSizer(mainSizer) mainSizer.Fit(self) # Events self.Bind(EVT_BUTTON, self._onOk, id=ID_OK) self.Bind(EVT_CLOSE, self._onClose) self.Bind(EVT_BUTTON, self._onNextTip, id=ID_SET_NEXT_TIP) self.Bind(EVT_BUTTON, self._onPreviousTip, id=ID_SET_PREVIOUS_TIP) # noinspection PyUnusedLocal def _onOk(self, event): """ _onOk : Handle user click on the OK button @author C.Dutoit """ # Exit modal mode self.Close() # noinspection PyUnusedLocal def _onNextTip(self, event): """ Select and display next tip @author C.Dutoit """ self._currentTip = (self._currentTip + 1) % len(Tips) self._label.SetLabel(Tips[self._currentTip]) # noinspection PyUnusedLocal def _onPreviousTip(self, event): """ Select and display previous tip @author C.Dutoit """ self._currentTip = (self._currentTip - 1) % len(Tips) self._label.SetLabel(Tips[self._currentTip]) def _onClose(self, event): """ Save state """ # Save state rationalTipNumber: int = (self._currentTip + 1) % len(Tips) currentTipNumber: str = f'{str(rationalTipNumber)}' self._prefs[PyutPreferences.CURRENT_TIP] = currentTipNumber self._prefs[PyutPreferences. SHOW_TIPS_ON_STARTUP] = self._chkShowTips.GetValue() event.Skip()
class DlgEditLink(Dialog): """ Dialog for the link (between classes) editting. to use it : dlg=DlgEditLink(...) dlg.setValues(...) dlg.ShowModal() to see if the user clicked on Ok or Cancel : x=dlg.getReturnAction() to get new values : x=dlg.getCardinality() x=dlg.getRoles() x=dlg.getRelationship() don't forget : dlg.Destroy() :version: $Revision: 1.9 $ :author: C.Dutoit :contact: [email protected] """ def __init__(self, parent, ID, pyutLink: PyutLink): """ """ super().__init__(parent, ID, _("Link Edit"), style=RESIZE_BORDER | CAPTION) self.logger: Logger = getLogger(__name__) # Associated PyutLink self._pyutLink: PyutLink = pyutLink self._relationship = self._pyutLink.getName() self._aRoleInB = "" self._bRoleInA = "" # self._cardinalityA = self._pyutLink.getSourceCardinality() # self._cardinalityB = self._pyutLink.getDestinationCardinality() self._cardinalityA = self._pyutLink.sourceCardinality self._cardinalityB = self._pyutLink.destinationCardinality self._returnAction = CANCEL # #describe how user exited dialog box # labels lblCardA = StaticText(self, -1, _("Cardinality"), style=ALIGN_LEFT) lblRela = StaticText(self, -1, _("Relationship"), style=ALIGN_CENTRE) lblCardB = StaticText(self, -1, _("Cardinality"), style=ALIGN_RIGHT) lblA = StaticText(self, -1, "A", style=ALIGN_LEFT) self._lblArrow = StaticText(self, -1, "", style=ALIGN_CENTRE) self.updateLblArrow() lblB = StaticText(self, -1, "B", style=ALIGN_RIGHT) lblAinB = StaticText(self, -1, _("A's role in B"), style=ALIGN_LEFT) lblBinA = StaticText(self, -1, _("B's role in A"), style=ALIGN_RIGHT) # text self._txtCardinalityA = TextCtrl(self, TXT_CARDINALITY_A, "", size=Size(50, 20)) self._txtRelationship = TextCtrl(self, TXT_RELATIONSHIP, "", size=Size(100, 20)) self._txtCardinalityB = TextCtrl(self, TXT_CARDINALITY_B, "", size=Size(50, 20)) self._txtARoleB = TextCtrl(self, A_ROLE_IN_B, "") self._txtBRoleA = TextCtrl(self, B_ROLE_IN_A, "") self.setValues(self._relationship, self._aRoleInB, self._bRoleInA, self._cardinalityA, self._cardinalityB) self._txtARoleB.Enable(False) self._txtBRoleA.Enable(False) # text events self.Bind(EVT_TEXT, self._onTxtCardinalityAChange, id=TXT_CARDINALITY_A) self.Bind(EVT_TEXT, self._onTxtCardinalityBChange, id=TXT_CARDINALITY_B) self.Bind(EVT_TEXT, self._onTxtRelationshipChange, id=TXT_RELATIONSHIP) self.Bind(EVT_TEXT, self._onTxtARoleBChange, id=A_ROLE_IN_B) self.Bind(EVT_TEXT, self._onTxtBRoleAChange, id=B_ROLE_IN_A) # Ok/Cancel btnOk = Button(self, OK, _("&Ok")) btnCancel = Button(self, CANCEL, _("&Cancel")) btnRemove = Button(self, BTN_REMOVE, _("&Remove")) btnOk.SetDefault() # button events self.Bind(EVT_BUTTON, self._onCmdOk, id=OK) self.Bind(EVT_BUTTON, self._onCmdCancel, id=CANCEL) self.Bind(EVT_BUTTON, self._onRemove, id=BTN_REMOVE) szr1 = FlexGridSizer(cols=3, hgap=30, vgap=5) szr1.AddMany([(lblCardA, 0, ALIGN_LEFT), (lblRela, 0, ALIGN_CENTER_HORIZONTAL), (lblCardB, 0, ALIGN_RIGHT), (self._txtCardinalityA, 0, ALIGN_LEFT), (self._txtRelationship, 0, ALIGN_CENTER_HORIZONTAL), (self._txtCardinalityB, 0, ALIGN_RIGHT)]) szr1.AddGrowableCol(0) szr1.AddGrowableCol(1) szr1.AddGrowableCol(2) szr2 = BoxSizer(HORIZONTAL) szr2.Add(lblA, 1, GROW | RIGHT, 10) szr2.Add(self._lblArrow, 1, GROW, 10) szr2.Add(lblB, 1, GROW) # szr3 : # lblAinB, lblBinA # self._txtARoleB, self._txtBRoleA szr3 = FlexGridSizer(cols=2, hgap=30, vgap=5) szr3.AddMany([(lblAinB, 0), (lblBinA, 0, ALIGN_RIGHT), (self._txtARoleB, 0), (self._txtBRoleA, 0, ALIGN_RIGHT | BOTTOM, 20)]) szr3.AddGrowableCol(0) szr3.AddGrowableCol(1) # szr4 : # btnRemove, btnOk, btnCancel szr4 = BoxSizer(HORIZONTAL) szr4.Add(btnRemove, 0, RIGHT, 10) szr4.Add(btnOk, 0, RIGHT, 10) szr4.Add(btnCancel, 0) # szr5 : # szr1 # szr2 # szr3 # szr4 szr5 = BoxSizer(VERTICAL) szr5.Add(szr1, 0, GROW | ALL, 10) szr5.Add(szr2, 0, GROW | ALL, 10) szr5.Add(szr3, 0, GROW | ALL, 10) szr5.Add(szr4, 0, ALIGN_RIGHT | ALL, 10) self.SetSizer(szr5) self.SetAutoLayout(True) szr5.Fit(self) def updateLblArrow(self): """ Draw an example arrow in lblArrow @since 1.1.1.2 @author C.Dutoit <*****@*****.**> """ dic = {PyutLink: "Pyut Link"} self.logger.info( f"DlgEditLink-updateLblArrow: {self._pyutLink.__class__}") if self._pyutLink.__class__ in dic.keys(): self._lblArrow.SetLabel(dic[self._pyutLink.__class__]) else: # self._lblArrow.SetLabel("Unknown link class : " + self._pyutLink.__class__) self._lblArrow.SetLabel( f'Unknown link class : {self._pyutLink.__class__}') def _copyLink(self): """ Copy the datas from _pyutLink to _pyutLinkCopy. @since 1.4 @author P. Waelti <*****@*****.**> """ self._pyutLinkCopy = deepcopy(self._pyutLink) def _onTxtCardinalityAChange(self, event): """ Event occuring when TXT_CARDINALITY_A change @since 1.2 @author C.Dutoit<*****@*****.**> """ self._cardinalityA = event.GetString() def _onTxtCardinalityBChange(self, event): """ Event occuring when TXT_CARDINALITY_B change @since 1.2 @author C.Dutoit<*****@*****.**> """ self._cardinalityB = event.GetString() def _onTxtRelationshipChange(self, event): """ Event occuring when TXT_RELATIONSHIP change @since 1.2 @author C.Dutoit<*****@*****.**> """ self._relationship = event.GetString() def _onTxtARoleBChange(self, event): """ Event occuring when TXT_A_ROLE_IN_B change @since 1.2 @author C.Dutoit<*****@*****.**> """ self._aRoleInB = event.GetString() def _onTxtBRoleAChange(self, event): """ Event occuring when TXT_B_ROLE_IN_A change @since 1.2 @author C.Dutoit<*****@*****.**> """ self._bRoleInA = event.GetString() # noinspection PyUnusedLocal def _onCmdOk(self, event: CommandEvent): """ Handle click on "Ok" button Args: event: """ self._pyutLink.setName(self._relationship) self._pyutLink.sourceCardinality = self._cardinalityA self._pyutLink.destinationCardinality = self._cardinalityB # Should perhaps take roles, not yet implemented in PyutLink TODO self._returnAction = OK self.Close() # noinspection PyUnusedLocal def _onCmdCancel(self, event): """ Handle click on "Cancel" button @since 1.2 @author C.Dutoit<*****@*****.**> """ self._returnAction = CANCEL self.Close() # noinspection PyUnusedLocal def _onRemove(self, event): """ Handle click on "Remove" button @since 1.2 @author Laurent Burgbacher <*****@*****.**> """ self._returnAction = -1 self.Close() def getCardinality(self): """ return a tuple composed by the two Cardinality (CA, CB) @since 1.2 @author C.Dutoit<*****@*****.**> """ return self._cardinalityA, self._cardinalityB def getRoles(self): """ return a tuple composed by the two Roles (A's role in B, B's role in a) @since 1.2 @author C.Dutoit<*****@*****.**> """ return self._aRoleInB, self._bRoleInA def getRelationship(self): """ return the relationship @since 1.2 @author C.Dutoit<*****@*****.**> """ return self._relationship def setValues(self, relationship, aRoleInB, bRoleInA, cardinalityA, cardinalityB): """ set all link's values @param text relationship : the relationship between the two entities @param text aRoleInB : the role of source entity in target entity @param text bRoleInA : the role of target entity in source entity @param text cardinalityA : the source cardinality @param text cardinalityB : the target cardinality @since 1.2 @author C.Dutoit<*****@*****.**> """ self._txtRelationship.SetValue(relationship) self._txtARoleB.SetValue(aRoleInB) self._txtBRoleA.SetValue(bRoleInA) self._txtCardinalityA.SetValue(cardinalityA) self._txtCardinalityB.SetValue(cardinalityB) def getReturnAction(self): """ Return an info on how the user exited the dialog box @return : Ok = click on Ok button; Cancel = click on Cancel button @since 1.2 @author C.Dutoit<*****@*****.**> """ return self._returnAction
class MainDialog(Dialog): """ Main window. Hit a button to profile the system, then another to scan what was plugged in """ def __init__(self): """Constructor""" Dialog.__init__(self, None, title="Bad Cop", size=Size(500, 100)) ico = Icon('logo.ico', BITMAP_TYPE_ICO) self.SetIcon(ico) self.message = StaticText( self, label="Click Profile, then insert device and click Test") self.profile = Button(self, label="Profile") self.test = Button(self, label="Test") self.test.Disable() self.profile.Bind(EVT_BUTTON, self.profileusb) self.test.Bind(EVT_BUTTON, self.testusb) self.Bind(EVT_CLOSE, self.onclose) main_sizer = BoxSizer(VERTICAL) t_sizer = BoxSizer(HORIZONTAL) p_sizer = BoxSizer(HORIZONTAL) t_sizer.Add(self.message, 0, ALL | CENTER, 5) p_sizer.Add(self.profile, 0, ALL | CENTER, 5) p_sizer.Add(self.test, 0, ALL | CENTER, 5) main_sizer.Add(p_sizer, 0, ALL | CENTER, 5) main_sizer.Add(t_sizer, 0, ALL | EXPAND | CENTER, 5) self.SetSizer(main_sizer) def profileusb(self, other): del other dev = find(find_all=True) devices = [] for cfg in dev: devclass = str(cfg.bDeviceClass) product = str(cfg.iProduct) vid = hex(cfg.idVendor) pid = hex(cfg.idProduct) for line in Configuration(find(idVendor=cfg.idVendor)): line = str(line) linestrip = line.strip() linesplit = linestrip.split('\n') linesplit = [x.split(':') for x in linesplit] lines = [] for w in linesplit: lines.append([y.strip(' ') for y in w]) for e in lines: if 'bInterfaceClass' in e[0]: intclass = e[1] devices.append([devclass, product, vid, pid, intclass]) with open('devices.pkl', 'wb') as f: dump(devices, f) self.profile.SetLabel("Done!") self.profile.Disable() self.test.Enable() def testusb(self, other): del other with open('devices.pkl', 'rb') as f: benchmark = load(f) dev = find(find_all=True) devices = [] for cfg in dev: devclass = str(cfg.bDeviceClass) product = str(cfg.iProduct) vid = hex(cfg.idVendor) pid = hex(cfg.idProduct) for line in Configuration(find(idVendor=cfg.idVendor)): line = str(line) linestrip = line.strip() linesplit = linestrip.split('\n') linesplit = [x.split(':') for x in linesplit] lines = [] for w in linesplit: lines.append([y.strip(' ') for y in w]) for e in lines: if 'bInterfaceClass' in e[0]: intclass = e[1] devices.append([devclass, product, vid, pid, intclass]) first_tuple_list = [tuple(lst) for lst in benchmark] secnd_tuple_list = [tuple(lst) for lst in devices] first_set = set(first_tuple_list) secnd_set = set(secnd_tuple_list) output = "" height = 100 first = 0 for devclass, product, vid, pid, usbtype in first_set.symmetric_difference( secnd_set): if usbtype == "0xa CDC Data": devicedesc = "Virtual Data Port (Network)" elif usbtype == "0xe0 Wireless Controller": devicedesc = "Wireless Internet Or Bluetooth" elif usbtype == "0x8 Mass Storage": devicedesc = "Data Storage Device" elif usbtype == "0x9 Hub": devicedesc = "USB Hub" elif usbtype == "0x3 Human Interface Device": devicedesc = "Keyboard, Mouse, or Other Input Device" elif usbtype == "0x2 CDC Communication": devicedesc = "Vitual Communications Port (Network)" else: devicedesc = usbtype + " device " if first == 0: output += "This appears to be a: \n " + devicedesc + " \n" else: output += " " + devicedesc + " \n" height = height + 30 first = 1 self.SetSize((500, height)) self.message.SetLabel(output) def onclose(self, event): del event try: remove('devices.pkl') except: print('file missing') self.Destroy() exit("Application Exited")