class TestDefaultTextValidator(TestCase): def setUp(self) -> None: self.min_length = 16 self.max_length = 256 self.validator = TextValidator() def test_valid(self): num_chars = randint(self.min_length, self.max_length) valid_text = gen_random_text(num_chars) self.assertTrue(self.validator.is_valid(valid_text)) def test_invalid_min(self): num_chars = randint(-999, 15) valid_text = gen_random_text(num_chars) self.assertFalse(self.validator.is_valid(valid_text)) def test_invalid_max(self): num_chars = randint(257, 999) valid_text = gen_random_text(num_chars) self.assertFalse(self.validator.is_valid(valid_text)) def test_absolute_min_valid(self): valid_text = gen_random_text(self.min_length) self.assertTrue(self.validator.is_valid(valid_text)) def test_absolute_max_valid(self): valid_text = gen_random_text(self.max_length) self.assertTrue(self.validator.is_valid(valid_text))
def _create_additional_controls(self): """ Create custom controls """ self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "verif"), size=(150, 26)), "Regular expression") self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "default"), size=(150, 26)), "Default value")
def _createControls(self): """ Create GUI controls """ # Add controls to the layout self.addToLayout( wx.TextCtrl(self, validator=TextValidator(self, "portname"), size=(200, 26)), "Serial port") self.addToLayout( wx.TextCtrl(self, validator=TextValidator(self, "portspeed")), "Serial baud rate (bps)") self.addOkCancelButtons()
def __init__(self, validators=None): self.default_validators = [ TextValidator(min_length=1001, max_length=3000) ] self.all_validators = self.default_validators if isinstance(validators, list) and validators: self.all_validators = self.default_validators + validators
def _createControls(self): """ Create GUI controls """ # Add controls to the layout self.addToLayout(wx.CheckBox(self, -1, "Anti-playback", validator=BoolValidator(self, "playbk"))) self.addToLayout(wx.CheckBox(self, -1, "Smart encryption", validator=BoolValidator(self, "smartencrypt"))) self.addToLayout(wx.TextCtrl(self, validator=TextValidator(self, "password", regex="^[A-Fa-f0-9-]{24}$"), size=(150, 26)), "Password (12 bytes, hex)") self.addOkCancelButtons()
def create_controls(self): """ Create GUI controls """ # Add controls to the layout self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "paramname", regex="^[a-zA-Z0-9_-]+$"), size=(150, 26)), "Name (blank spaces not allowed)") self.rb_type_num = wx.RadioButton(self, -1, 'Numeric value', (10, 10), style=wx.RB_GROUP) self.rb_type_bin = wx.RadioButton(self, -1, 'Binary value', (10, 30)) self.rb_type_str = wx.RadioButton(self, -1, 'String value', (10, 50)) self.Bind(wx.EVT_RADIOBUTTON, self.set_type, id=self.rb_type_num.GetId()) self.Bind(wx.EVT_RADIOBUTTON, self.set_type, id=self.rb_type_bin.GetId()) self.Bind(wx.EVT_RADIOBUTTON, self.set_type, id=self.rb_type_str.GetId()) self.add_to_layout(self.rb_type_num, "Data type:") self.add_to_layout(self.rb_type_bin) self.add_to_layout(self.rb_type_str) self.add_to_layout( wx.TextCtrl( self, validator=TextValidator( self, "bitsize", regex= "^([1-9]|[1-9][0-9]|[1-3][0-9][0-9]|[4][0-3][0-9]|[440])$" ), size=(70, 26)), "Size in bits")
def _createControls(self): """ Create GUI controls """ # Add controls to the layout self.addToLayout( wx.TextCtrl(self, validator=TextValidator(self, "freq_channel")), "Frequency channel") self.addToLayout( wx.TextCtrl(self, validator=TextValidator(self, "netid", regex="^[A-Fa-f0-9-]{4}$")), "Network ID") self.addToLayout( wx.TextCtrl(self, validator=TextValidator(self, "devaddress")), "Device address") self.addToLayout( wx.TextCtrl(self, validator=TextValidator(self, "interval")), "Periodic Tx interval") self.addOkCancelButtons()
def _addWidget(self, index): """ Add widget to the layout 'index' Index of hte parameter within self.parameters """ param = self.parameters[index] if param.type == SwapType.BINARY: self.addToLayout(wx.CheckBox(self, -1, param.name, validator=BoolValidator(self, "parameters", index))) elif param.type in [SwapType.NUMBER, SwapType.STRING]: addTextCtrl = False choices = [] if param.verif is not None: if param.verif == "MONTH": choices = ["January", "February", "March", "April", "June", "May", "July", "August", "September", "October", "November", "December"] elif param.verif == "YEAR": for i in range(2011, 2060): choices.append(str(i)) elif param.verif == "MDAY": for i in range(1, 32): choices.append(str(i)) elif param.verif == "WDAY": choices = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] elif param.verif in ["MINUTE", "SECOND"]: for i in range(0, 10): choices.append("0" + str(i)) for i in range(10, 60): choices.append(str(i)) elif param.verif == "HOUR": for i in range(0, 10): choices.append("0" + str(i)) for i in range(10, 24): choices.append(str(i)) elif len(param.verif) > 6: if param.verif[:6] == "RANGE(": comma = param.verif.find(",") if comma > -1: startRange = int(param.verif[6:comma]) endRange = int(param.verif[comma+1:-1]) else: startRange = 0 endRange = int(param.verif[6:-1]) + 1 for i in range(startRange, endRange): choices.append(str(i)) else: addTextCtrl = True else: addTextCtrl = True if addTextCtrl: self.addToLayout(wx.TextCtrl(self, -1, validator=TextValidator(self, "parameters", index, param.verif)), param.name) else: self.addToLayout(wx.ComboBox(self, validator=ListValidator(self, "parameters", index), choices=choices, style=wx.CB_READONLY), param.name)
def create_controls(self): """ Create GUI controls """ # Add controls to the layout self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "unit"), size=(70, 26)), "Unit label") self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "factor", regex="[-+]?[0-9]*\.?[0-9]+"), size=(70, 26)), "Factor modifier") self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "offset", regex="[-+]?[0-9]*\.?[0-9]+"), size=(70, 26)), "Offset modifier") self.add_to_layout( wx.StaticText(self, label="Final value = value x factor + offset")) self.add_okcancel_buttons()
def _create_controls(self): """ Create GUI controls """ # Add controls to the layout self.add_to_layout( wx.TextCtrl(self, validator=TextValidator(self, "regname", regex="\S"), size=(150, 26)), "Register name") self.add_to_layout( wx.TextCtrl( self, validator=TextValidator( self, "regid", regex= "^([1][1-9]|[2-9][0-9]|[1][0-9][0-9]|[2][0-1][0-9]|[22][0-5])$" ), size=(70, 26)), "Register ID (10-255)") # Create list box self.list_parameters = wx.ListCtrl(self, -1, style=wx.LC_REPORT, size=(275, 150)) self.list_parameters.InsertColumn(0, "Parameter name") self.list_parameters.InsertColumn(1, "type") self.list_parameters.InsertColumn(2, "dir") self.list_parameters.SetColumnWidth(0, 140) self.list_parameters.SetColumnWidth(1, 50) self.list_parameters.SetColumnWidth(1, 50) self.add_to_layout(self.list_parameters, "List of parameters") self.add_list_buttons() self.add_okcancel_buttons()
def __init__(self, parent, developer_id=None, developer_name=None, product_id=None, product_name=None, product_descr=None, pwrdownmode=False): """ Constructor @param parent: parent wizard @param developer_id: developer ID @param developer_nanme: developer name @param product_id: product ID @param product_nanme: product name @param product_descr: product description @param pwrdownmode: power-down mode """ SwapWizardPage.__init__(self, parent, title="Product information") # Developer name self.developer_name = developer_name if self.developer_name is None: self.developer_name = "" # SWAP develoepr id self.developer_id = developer_id if self.developer_id is None: self.developer_id = "" # Product name self.product_name = product_name if self.product_name is None: self.product_name = "" # Product description self.product_descr = product_descr if self.product_descr is None: self.product_descr = "" # SWAP peoduct id self.product_id = product_id if self.product_id is None: self.product_id = "" # Battery-powered device self.low_power = pwrdownmode # Add widgets to the page self.add_to_layout( wx.TextCtrl(self, -1, validator=TextValidator(self, "developer_name"), size=(150, 26)), "Developer name") self.add_to_layout( wx.TextCtrl(self, -1, validator=TextValidator(self, "developer_id", regex="^[0-9]*$"), size=(70, 26)), "Developer ID number (DEC)") self.add_to_layout( wx.TextCtrl(self, -1, validator=TextValidator(self, "product_name", regex="^[a-zA-Z0-9]*$"), size=(150, 26)), "Product name (single word)") self.add_to_layout( wx.TextCtrl(self, -1, validator=TextValidator(self, "product_descr"), size=(250, 26)), "Product description") self.add_to_layout( wx.TextCtrl(self, -1, validator=TextValidator(self, "product_id", regex="^[0-9]*$"), size=(70, 26)), "Product ID number (DEC)") self.add_to_layout( wx.CheckBox(self, -1, "Battery-powered device?", validator=BoolValidator(self, "low_power"))) self.Bind(wx.wizard.EVT_WIZARD_PAGE_CHANGING, self.onpage_changing)
def is_valid(self, value): return all(TextValidator.is_valid(value) for TextValidator in self.all_validators)
def __init__(self, validators=None): self.default_validator = [TextValidator(1001, 3000)] self.all_validators = self.default_validator if isinstance(validators, list) and validators: self.all_validators += validators super().__init__()
def __init__(self, validators=None): self.default_validators = [TextValidator(0, 999)] self.all_validators = self.default_validators if isinstance(validators, list) and validators: self.all_validators += validators
def setUp(self) -> None: self.min_length = 16 self.max_length = 256 self.validator = TextValidator()
def setUp(self) -> None: minimal = randint(0, 128) maximal = randint(128, 256) self.min_length = minimal self.max_length = maximal self.validator = TextValidator(min_length=minimal, max_length=maximal)
def __init__(self, validators=None): self.default_validator = [TextValidator(min_length=0, max_length=999)] self.all_validators = self.default_validator if isinstance(validators, list) and validators: self.all_validators += validators
def __init__(self, validators=None, *args, **kwargs): super().__init__(*args, **kwargs) self.default = [TextValidator(min_length=1001, max_length=3000)] self.all_validators = self.default if isinstance(validators, list) and validators: self.all_validators += validators