From 134a674228b3d2b42cb7fe2a12f215a225e7544b Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 9 Jan 2019 19:38:04 +0100 Subject: initial rework of the implemented functions + added __init__ import file for all functions + added new helper function + added strings.json file * some logic fixups * validator fixup * arg abbreviation moved to misc --- common/misc.py | 52 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py index b91c618..edfe3a0 100755 --- a/common/misc.py +++ b/common/misc.py @@ -24,28 +24,56 @@ def validate(keyword, target): :param target: provided target :return: true if valid """ - # if keyword in domain_keywords list - if keyword in StaticAnswers().keys('domain_keywords'): + # if keyword is in noarg list return True + if keyword in StaticAnswers().keys("noarg"): + return True + + # prevent AttributeError if target is NoneType + if target is None: + return False + + # if keyword in domain list + if keyword in StaticAnswers().keys('domain'): # if target is a domain / email return True if validators.domain(target) or validators.email(target): return True - # check if keyword is in number_keyword list - elif keyword in StaticAnswers().keys('number_keywords'): - # prevent AttributeError if target is NoneType - if target is not None: - # if target only consists of digits return True - return target.isdigit() + # check if keyword is in number list + elif keyword in StaticAnswers().keys('number'): + return target.isdigit() - # if keyword is in no_arg_keywords list return True - elif keyword in StaticAnswers().keys("no_arg_keywords"): + # if keyword in expand list return True + elif keyword in StaticAnswers().keys("expand"): return True # if the target could not be validated until this return False return False -# +def arg_abbr(value, possible_values): + """ + optional argument abbreviation + if the provided string value > 2 characters the most likely value will be chosen + :return: completes the value to the most likely one + """ + # prevent traceback if value is None + if value and possible_values is not None: + # if opt_argument is smaller then 2 pass to prohibit multiple answers + if len(value) < 2: + return value + + abbr = str(value) + + # searches the best match in the list of possible_abbr and completes the opt_arg to that + new_value = [s for s in list(possible_values) if s.startswith(abbr)] + + # prevent index error if abbreviation has not result + if new_value: + value = new_value[0] + + return value + + class HandleError: """ simple XMPP error / exception class formating the error condition @@ -59,6 +87,6 @@ class HandleError: def report(self): # return the formatted result string to the user - text = "%s. %s %s resulted in: %s" % (self.text, self.key, self.target, self.condition) + text = "%s %s resulted in: %s" % (self.key, self.target, self.condition) return text -- cgit v1.2.3-54-g00ecf