From 6a42748191f07e8bd3611d564641d3b06fa17125 Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Tue, 10 Dec 2019 16:50:28 -0800 Subject: [PATCH] controlnet validation --- coretk/coretk/validation.py | 24 ++++++++++++++++++------ coretk/coretk/widgets.py | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/coretk/coretk/validation.py b/coretk/coretk/validation.py index 403e073d..68fc79c3 100644 --- a/coretk/coretk/validation.py +++ b/coretk/coretk/validation.py @@ -1,7 +1,7 @@ """ input validation """ -import logging +import re import tkinter as tk import netaddr @@ -14,12 +14,14 @@ class InputValidation: self.positive_int = None self.positive_float = None self.name = None + self.ip4 = None self.register() def register(self): self.positive_int = self.master.register(self.check_positive_int) self.positive_float = self.master.register(self.check_positive_float) self.name = self.master.register(self.check_node_name) + self.ip4 = self.master.register(self.check_ip4) def ip_focus_out(self, event): value = event.widget.get() @@ -35,7 +37,6 @@ class InputValidation: event.widget.insert(tk.END, default) def check_positive_int(self, s): - logging.debug("int validation...") if len(s) == 0: return True try: @@ -47,7 +48,6 @@ class InputValidation: return False def check_positive_float(self, s): - logging.debug("float validation...") if len(s) == 0: return True try: @@ -59,7 +59,6 @@ class InputValidation: return False def check_node_name(self, s): - logging.debug("node name validation...") if len(s) < 0: return False if len(s) == 0: @@ -70,7 +69,6 @@ class InputValidation: return True def check_canvas_int(sefl, s): - logging.debug("int validation...") if len(s) == 0: return True try: @@ -82,7 +80,6 @@ class InputValidation: return False def check_canvas_float(self, s): - logging.debug("canvas float validation") if not s: return True try: @@ -92,3 +89,18 @@ class InputValidation: return False except ValueError: return False + + def check_ip4(self, s): + if not s: + return True + pat = re.compile("^([0-9]+[.])*[0-9]*$") + if pat.match(s) is not None: + _32bits = s.split(".") + if len(_32bits) > 4: + return False + for _8bits in _32bits: + if (_8bits and int(_8bits) > 255) or len(_8bits) > 3: + return False + return True + else: + return False diff --git a/coretk/coretk/widgets.py b/coretk/coretk/widgets.py index 68983210..fb5582a1 100644 --- a/coretk/coretk/widgets.py +++ b/coretk/coretk/widgets.py @@ -117,8 +117,18 @@ class ConfigFrame(FrameScroll): button = ttk.Button(file_frame, text="...", command=func) button.grid(row=0, column=1) else: - entry = ttk.Entry(frame, textvariable=value) - entry.grid(row=index, column=1, sticky="ew", pady=pady) + if "controlnet" in option.name and "script" not in option.name: + entry = ttk.Entry( + frame, + textvariable=value, + validate="key", + validatecommand=(self.app.validation.ip4, "%P"), + ) + entry.grid(row=index, column=1, sticky="ew", pady=pady) + else: + entry = ttk.Entry(frame, textvariable=value) + entry.grid(row=index, column=1, sticky="ew", pady=pady) + elif option.type in INT_TYPES: value.set(option.value) entry = ttk.Entry(