From 31ddc8aeb47a19ff038678fb55992338aad0b6b1 Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 10 Oct 2018 01:34:56 +0200 Subject: new async logic --- common/misc.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 common/misc.py (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py new file mode 100755 index 0000000..3961b8d --- /dev/null +++ b/common/misc.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +import validators +from common.strings import StaticAnswers + + +def deduplicate(reply): + """ + list deduplication method + :param list reply: list containing non unique items + :return: list containing unique items + """ + reply_dedup = list() + for item in reply: + if item not in reply_dedup: + reply_dedup.append(item) + + return reply_dedup + + +def validate(wordlist, index): + """ + validation method to reduce malformed querys and unnecessary connection attempts + :param wordlist: words separated by " " from the message + :param index: keyword index inside the message + :return: true if valid + """ + # keyword inside the message + argument = wordlist[index] + + # check if argument is in the argument list + if argument in StaticAnswers().keys(arg='list'): + # if argument uses a domain check for occurrence in list and check domain + if argument in StaticAnswers().keys(arg='list', keyword='domain_keywords'): + try: + target = wordlist[index + 1] + if validators.domain(target): + return True + elif validators.email(target): + return True + + except IndexError: + # except an IndexError if a keywords is the last word in the message + return False + + # check if number keyword is used if true check if target is assignable + elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'): + try: + if wordlist[index + 1]: + return True + except IndexError: + # except an IndexError if target is not assignable + return False + # check if argument is inside no_arg list + elif argument in StaticAnswers().keys(arg='list', keyword="no_arg_keywords"): + return True + else: + return False + else: + return False -- cgit v1.2.3-18-g5258 From 0c313565f2b649366f7382dc1b3f28a3e80f4ffc Mon Sep 17 00:00:00 2001 From: nico Date: Tue, 6 Nov 2018 23:43:11 +0100 Subject: simplification and major rework * updated gitignore file * partly reworked servercontact implementation * complete rework of uptime, version * part rework of xep requests + added more comments to xep requests + added opt_arg to version, xep and contact * complete rework of validate function * updated HandleError function * part rework of StaticStrings function + implemented data dictionary to hold all data in main bot + added message_ids * complete rework of queue building and deduplication --- common/misc.py | 76 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 33 deletions(-) (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py index 3961b8d..bbf9c55 100755 --- a/common/misc.py +++ b/common/misc.py @@ -17,43 +17,53 @@ def deduplicate(reply): return reply_dedup -def validate(wordlist, index): +def validate(keyword, target): """ validation method to reduce malformed querys and unnecessary connection attempts - :param wordlist: words separated by " " from the message - :param index: keyword index inside the message + :param keyword: used keyword + :param target: provided target :return: true if valid """ - # keyword inside the message - argument = wordlist[index] - - # check if argument is in the argument list - if argument in StaticAnswers().keys(arg='list'): - # if argument uses a domain check for occurrence in list and check domain - if argument in StaticAnswers().keys(arg='list', keyword='domain_keywords'): - try: - target = wordlist[index + 1] - if validators.domain(target): - return True - elif validators.email(target): - return True - - except IndexError: - # except an IndexError if a keywords is the last word in the message - return False - - # check if number keyword is used if true check if target is assignable - elif argument in StaticAnswers().keys(arg='list', keyword='number_keywords'): - try: - if wordlist[index + 1]: - return True - except IndexError: - # except an IndexError if target is not assignable - return False - # check if argument is inside no_arg list - elif argument in StaticAnswers().keys(arg='list', keyword="no_arg_keywords"): + # check if keyword is in the argument list + if keyword in StaticAnswers().keys(): + + # if keyword in domain_keywords list + if keyword in StaticAnswers().keys('domain_keywords'): + # if target is a domain / email return True + if validators.domain(target): + return True + elif validators.email(target): + return True + + # check if keyword is in number_keyword list + elif keyword in StaticAnswers().keys('number_keywords'): + # if target only consists of digits return True + if target.isdigit(): + return True + + # if keyword is in no_arg_keywords list return True + elif keyword in StaticAnswers().keys("no_arg_keywords"): return True - else: - return False + + # if the target could not be validated until this return False else: return False + + +# +class HandleError: + """ + simple XMPP error / exception class formating the error condition + """ + def __init__(self, error, key, target): + # init all necessary variables + self.error = error + self.key = key + self.target = target + + def report(self): + # return the formatted result string to the user + condition = self.error.condition + text = "There was an error requesting %s's %s : %s" % (self.target, self.key, condition) + + return text -- cgit v1.2.3-18-g5258 From d7fc664d3be4634a693a24fa98ff3f15d2c97c41 Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 7 Nov 2018 00:37:24 +0100 Subject: * corrected CamelCase * corrected logging.INFO to .info * small changes to HandleError class --- common/misc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py index bbf9c55..1350a2a 100755 --- a/common/misc.py +++ b/common/misc.py @@ -57,13 +57,13 @@ class HandleError: """ def __init__(self, error, key, target): # init all necessary variables - self.error = error + self.text = error.text + self.condition = error.condition self.key = key self.target = target def report(self): # return the formatted result string to the user - condition = self.error.condition - text = "There was an error requesting %s's %s : %s" % (self.target, self.key, condition) + text = "%s, %s resulted in: %s" % (self.text, self.key, self.condition) return text -- cgit v1.2.3-18-g5258 From 146a4efc7f9bb97caaa97ddd02f1ce244cb4deb0 Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 7 Nov 2018 00:49:32 +0100 Subject: small fixup + added timestamp to logging output * finished up HandleError output --- common/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py index 1350a2a..e0df882 100755 --- a/common/misc.py +++ b/common/misc.py @@ -64,6 +64,6 @@ class HandleError: def report(self): # return the formatted result string to the user - text = "%s, %s resulted in: %s" % (self.text, self.key, self.condition) + text = "%s. %s %s resulted in: %s" % (self.text, self.key, self.target, self.condition) return text -- cgit v1.2.3-18-g5258 From cd1442e216abf564daceaef5fe45555587eef69b Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 19:42:21 +0100 Subject: code quality improvements - remove unused variable * better iteration of xdata nodes - removed unnecessary else --- common/misc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py index e0df882..abcc05e 100755 --- a/common/misc.py +++ b/common/misc.py @@ -46,8 +46,7 @@ def validate(keyword, target): return True # if the target could not be validated until this return False - else: - return False + return False # -- cgit v1.2.3-18-g5258 From 86d058237b336516d2e5009072a4365b5bf7380b Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 9 Nov 2018 19:56:09 +0100 Subject: elif amount reduction * reduced amount of unnecessary elif after return * some formatting --- common/misc.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'common/misc.py') diff --git a/common/misc.py b/common/misc.py index abcc05e..86798b8 100755 --- a/common/misc.py +++ b/common/misc.py @@ -24,26 +24,20 @@ def validate(keyword, target): :param target: provided target :return: true if valid """ - # check if keyword is in the argument list - if keyword in StaticAnswers().keys(): - - # if keyword in domain_keywords list - if keyword in StaticAnswers().keys('domain_keywords'): - # if target is a domain / email return True - if validators.domain(target): - return True - elif validators.email(target): - return True + # if keyword in domain_keywords list + if keyword in StaticAnswers().keys('domain_keywords'): + # 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'): - # if target only consists of digits return True - if target.isdigit(): - return True + # check if keyword is in number_keyword list + elif keyword in StaticAnswers().keys('number_keywords'): + # if target only consists of digits return True + return target.isdigit() - # if keyword is in no_arg_keywords list return True - elif keyword in StaticAnswers().keys("no_arg_keywords"): - return True + # if keyword is in no_arg_keywords list return True + elif keyword in StaticAnswers().keys("no_arg_keywords"): + return True # if the target could not be validated until this return False return False -- cgit v1.2.3-18-g5258