def _prep_text(i, entry): number = tui.TextWidget("%2d)" % i) title = tui.TextWidget(_(entry.title)) value = getdeepattr(self.args, entry.attribute) value = tui.TextWidget(value) return tui.ColumnWidget([(3, [number]), (None, [title, value])], 1)
def _prep_check(i, entry): number = tui.TextWidget("%2d)" % i) value = getdeepattr(self.args, entry.attribute) ch = tui.CheckboxWidget(title=_(entry.title), completed=bool(value)) return tui.ColumnWidget([(3, [number]), (None, [ch])], 1)
def set_deep_attr_test(self): """Test setdeepattr.""" # pylint: disable=attribute-defined-outside-init # pylint: disable=no-member class O(object): pass a = O() a.b = O() a.b1 = 1 a.b.c = O() a.b.c1 = "ř" # set to a new attribute iutil.setdeepattr(a, "b.c.d", True) self.assertEqual(a.b.c.d, True) # override existing attribute iutil.setdeepattr(a, "b.c", 1234) self.assertEqual(a.b.c, 1234) # "" is actually a valid attribute name # that can be only accessed by getattr iutil.setdeepattr(a, "", 1234) self.assertEqual(getattr(a, ""), 1234) iutil.setdeepattr(a, "b.", 123) self.assertEqual(iutil.getdeepattr(a, "b."), 123) # error should raise AttributeError with self.assertRaises(AttributeError): iutil.setdeepattr(a, "b.c.d.e.f.g.h", 1234)
def _prep_password(i, entry): number = tui.TextWidget("%2d)" % i) title = tui.TextWidget(_(entry.title)) value = getdeepattr(self.args, entry.attribute) value = tui.TextWidget("".join(["*"] * len(value))) return tui.ColumnWidget([(3, [number]), (None, [title, value])], 1)
def _prep_check(i, entry): number = tui.TextWidget("%2d)" % i) value = getdeepattr(self.args, entry.attribute) ch = tui.CheckboxWidget( title=_(entry.title), completed=bool(value)) return tui.ColumnWidget([(3, [number]), (None, [ch])], 1)
def input(self, args, key): try: idx = int(key) - 1 if idx >= 0 and idx < len(self.visible_fields): if self.visible_fields[idx].aux == self.CHECK: setdeepattr( self.args, self.visible_fields[idx].attribute, not getdeepattr( self.args, self.visible_fields[idx][1])) self.redraw() self.apply() else: ScreenHandler.push_screen_modal(self.dialog, self.visible_fields[idx]) self.redraw() if self.dialog.value is not None: setdeepattr(self.args, self.visible_fields[idx].attribute, self.dialog.value) self.apply() return InputState.PROCESSED except ValueError: pass return NormalTUISpoke.input(self, args, key)
def _prep_password(i, entry): number = tui.TextWidget("%2d)" % i) title = tui.TextWidget(_(entry.title)) value = "" if len(getdeepattr(self.args, entry.attribute)) > 0: value = _("Password set.") value = tui.TextWidget(value) return tui.ColumnWidget([(3, [number]), (None, [title, value])], 1)
def input(self, args, key): try: idx = int(key) - 1 if idx >= 0 and idx < len(self.visible_fields): if self.visible_fields[idx].aux == self.CHECK: setdeepattr(self.args, self.visible_fields[idx].attribute, not getdeepattr(self.args, self.visible_fields[idx][1])) self.app.redraw() self.apply() else: self.app.switch_screen_modal(self.dialog, self.visible_fields[idx]) if self.dialog.value is not None: setdeepattr(self.args, self.visible_fields[idx].attribute, self.dialog.value) self.apply() return True except ValueError: pass return NormalTUISpoke.input(self, args, key)
def input(self, args, key): try: idx = int(key) - 1 if idx >= 0 and idx < len(self.edit_fields): if self.edit_fields[idx].aux == self.CHECK: setdeepattr(self.args, self.edit_fields[idx].attribute, not getdeepattr(self.args, self.edit_fields[idx][1])) self.app.redraw() self.apply() else: self.app.switch_screen_modal(self.dialog, self.edit_fields[idx]) if self.dialog.value is not None: setdeepattr(self.args, self.edit_fields[idx].attribute, self.dialog.value) self.apply() return True except ValueError: pass return NormalTUISpoke.input(self, args, key)
def get_deep_attr_test(self): """Test getdeepattr.""" # pylint: disable=attribute-defined-outside-init class O(object): pass a = O() a.b = O() a.b1 = 1 a.b.c = 2 a.b.c1 = "ř" self.assertEqual(iutil.getdeepattr(a, "b1"), 1) self.assertEqual(iutil.getdeepattr(a, "b.c"), 2) self.assertEqual(iutil.getdeepattr(a, "b.c1"), "ř") # be consistent with getattr and throw # AttributeError if non-existent attribute is requested with self.assertRaises(AttributeError): iutil.getdeepattr(a, "") with self.assertRaises(AttributeError): iutil.getdeepattr(a, "b.c.d")