Files
connpy/connpy/cli/validators.py
T
fluzzi32 bf5bddda5a perf(core): implement lazy loading for heavy services, Google SDK and CLI forms
- Defer instantiation of heavy services (SyncService, UserService,
  ImportExportService, SystemService, ExecutionService) using cached
  properties in ServiceProvider and module __getattr__ in services.
- Defer Google Drive SDK imports in sync_service.py using module-level
  __getattr__ and _get_google_libs() helper while retaining mock patchability.
- Lazy load inquirer, Forms, Validators and Blessed theme adapters in
  connpy/cli/* to prevent loading CLI form dependencies at startup.
- Clean up unused top-level service imports in connapp.py.
2026-08-11 17:06:40 -03:00

143 lines
5.6 KiB
Python

import re
import ast
def _raise_val_err(reason):
import inquirer
raise inquirer.errors.ValidationError("", reason=reason)
class Validators:
def __init__(self, app):
self.app = app
def host_validation(self, answers, current, regex = "^.+$"):
if not re.match(regex, current):
_raise_val_err("Host cannot be empty")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
return True
def profile_protocol_validation(self, answers, current, regex = "(^ssh$|^telnet$|^kubectl$|^docker$|^ssm$|^$)"):
if not re.match(regex, current):
_raise_val_err("Pick between ssh, telnet, kubectl, docker, ssm or leave empty")
return True
def protocol_validation(self, answers, current, regex = "(^ssh$|^telnet$|^kubectl$|^docker$|^ssm$|^$|^@.+$)"):
if not re.match(regex, current):
_raise_val_err("Pick between ssh, telnet, kubectl, docker, ssm, leave empty or @profile")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
return True
def profile_port_validation(self, answers, current, regex = "(^[0-9]*$)"):
if not re.match(regex, current):
_raise_val_err("Pick a port between 1-65535, @profile o leave empty")
try:
port = int(current)
except ValueError:
port = 0
if current != "" and not 1 <= int(port) <= 65535:
_raise_val_err("Pick a port between 1-65535 or leave empty")
return True
def port_validation(self, answers, current, regex = "(^[0-9]*$|^@.+$)"):
if not re.match(regex, current):
_raise_val_err("Pick a port between 1-65535, @profile or leave empty")
try:
port = int(current)
except ValueError:
port = 0
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
elif current != "" and not 1 <= int(port) <= 65535:
_raise_val_err("Pick a port between 1-65535, @profile o leave empty")
return True
def pass_validation(self, answers, current, regex = "(^@.+$)"):
profiles = current.split(",")
for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(i))
return True
def tags_validation(self, answers, current):
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
elif current != "":
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err("Tags should be a python dictionary.".format(current))
return True
def profile_tags_validation(self, answers, current):
if current != "":
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err("Tags should be a python dictionary.".format(current))
return True
def jumphost_validation(self, answers, current):
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
elif current != "":
if current not in self.app.nodes_list:
_raise_val_err("Node {} don't exist.".format(current))
return True
def profile_jumphost_validation(self, answers, current):
if current != "":
if current not in self.app.nodes_list:
_raise_val_err("Node {} don't exist.".format(current))
return True
def default_validation(self, answers, current):
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
return True
def bulk_node_validation(self, answers, current, regex = "^[0-9a-zA-Z_.,$#-]+$"):
if not re.match(regex, current):
_raise_val_err("Host cannot be empty")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
return True
def bulk_folder_validation(self, answers, current):
if not self.app.case:
current = current.lower()
candidate = current
if "/" in current:
candidate = current.split("/")[0]
matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != "" and len(matches) == 0:
_raise_val_err("Location {} don't exist".format(current))
return True
def bulk_host_validation(self, answers, current, regex = "^.+$"):
if not re.match(regex, current):
_raise_val_err("Host cannot be empty")
if current.startswith("@"):
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
hosts = current.split(",")
nodes = answers["ids"].split(",")
if len(hosts) > 1 and len(hosts) != len(nodes):
_raise_val_err("Hosts list should be the same length of nodes list")
return True