From d305f8adf3fa7c4f154f003bdf16ce42b1895ffd Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 6 Oct 2018 13:16:27 +0200 Subject: small improvements * leading 0 fix * validation function improvements * moved strings.py and misc files to /common/ --- common/strings.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 common/strings.py (limited to 'common/strings.py') diff --git a/common/strings.py b/common/strings.py new file mode 100644 index 0000000..6866a31 --- /dev/null +++ b/common/strings.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +from random import randint + + +class StaticAnswers: + """ + collection of callable static/ semi-static strings + """ + def __init__(self, nick=""): + self.nickname = nick + self.helpfile = { + 'help': '!help -- display this text', + 'version': '!version domain.tld -- receive XMPP server version', + 'uptime': '!uptime domain.tld -- receive XMPP server uptime', + 'contact': '!contact domain.tld -- receive XMPP server contact address info', + 'xep': '!xep XEP Number -- recieve information about the specified XEP'} + self.possible_answers = { + '1': 'I heard that, %s.', + '2': 'I am sorry for that %s.', + '3': '%s did you try turning it off and on again?'} + self.error_messages = { + '1': 'not reachable', + '2': 'not a valid target' + } + self.keywords = { + "keywords": ["!help", "!uptime", "!version", "!contact", "!xep"], + "domain_keywords": ["!uptime", "!version", "!contact"], + "no_arg_keywords": ["!help"], + "number_keywords": ["!xep"] + } + + def keys(self, arg="", keyword='keywords'): + if arg == 'list': + try: + return self.keywords[keyword] + except KeyError: + return self.keywords['keywords'] + else: + return self.keywords + + def gen_help(self): + helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()]) + return helpdoc + + def gen_answer(self): + possible_answers = self.possible_answers + return possible_answers[str(randint(1, possible_answers.__len__()))] % self.nickname + + def error(self,code): + try: + text = self.error_messages[str(code)] + except KeyError: + return 'unknown error' + return text -- 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/strings.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'common/strings.py') diff --git a/common/strings.py b/common/strings.py index 6866a31..faac65c 100644 --- a/common/strings.py +++ b/common/strings.py @@ -29,14 +29,13 @@ class StaticAnswers: "number_keywords": ["!xep"] } - def keys(self, arg="", keyword='keywords'): - if arg == 'list': - try: - return self.keywords[keyword] - except KeyError: - return self.keywords['keywords'] + def keys(self, key=""): + # if specific keyword in referenced return that + if key in self.keywords.keys(): + return self.keywords[key] + # in any other case return the whole dict else: - return self.keywords + return self.keywords["keywords"] def gen_help(self): helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()]) -- 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/strings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'common/strings.py') diff --git a/common/strings.py b/common/strings.py index faac65c..7a10471 100644 --- a/common/strings.py +++ b/common/strings.py @@ -33,9 +33,9 @@ class StaticAnswers: # if specific keyword in referenced return that if key in self.keywords.keys(): return self.keywords[key] + # in any other case return the whole dict - else: - return self.keywords["keywords"] + return self.keywords["keywords"] def gen_help(self): helpdoc = "\n".join(['%s' % value for (_, value) in self.helpfile.items()]) -- 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/strings.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'common/strings.py') diff --git a/common/strings.py b/common/strings.py index 7a10471..6f1b629 100644 --- a/common/strings.py +++ b/common/strings.py @@ -13,11 +13,13 @@ class StaticAnswers: 'version': '!version domain.tld -- receive XMPP server version', 'uptime': '!uptime domain.tld -- receive XMPP server uptime', 'contact': '!contact domain.tld -- receive XMPP server contact address info', - 'xep': '!xep XEP Number -- recieve information about the specified XEP'} + 'xep': '!xep XEP Number -- recieve information about the specified XEP' + } self.possible_answers = { '1': 'I heard that, %s.', '2': 'I am sorry for that %s.', - '3': '%s did you try turning it off and on again?'} + '3': '%s did you try turning it off and on again?' + } self.error_messages = { '1': 'not reachable', '2': 'not a valid target' -- cgit v1.2.3-18-g5258