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 +++++++++++++++++++------ common/strings.json | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ common/strings.py | 69 ++++++++++++--------------------- 3 files changed, 173 insertions(+), 56 deletions(-) create mode 100644 common/strings.json (limited to 'common') 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 diff --git a/common/strings.json b/common/strings.json new file mode 100644 index 0000000..eefd73e --- /dev/null +++ b/common/strings.json @@ -0,0 +1,108 @@ +{ + "help": { + "help_basic": { + "contact": "!contact domain.tld -- receive XMPP server contact address info", + "help": "!help -- display all basic help information", + "uptime": "!uptime domain.tld -- receive XMPP server uptime", + "version": "!version domain.tld -- receive XMPP server version", + "xep": "!xep $xepnumber -- receive xep info abstract" + }, + "help_advanced": { + "contact": { + "info": "XEP-0157 contact addresses for XMPP Services request", + "optional_args": { + "info": "it is not required to type the whole optional argument, it is only necessary to type at least the two starting characters.", + "possible_vars": [ + "abuse-addresses", + "admin-addresses", + "feedback-addresses", + "sales-addresses", + "security-addresses", + "support-addresses" + ] + }, + "targets": "valid target: domain.tld", + "command": "!contact $target $optional_argument" + }, + "uptime": { + "info": "XEP-0012 last activity request", + "targets": "valid targets: domain.tld", + "command": "!uptime $target $optional_argument" + }, + "version": { + "info": "XEP-0072 version query request", + "optional_args": { + "name": "receive the name of the software used", + "os": "receive only the operating system version", + "version": "receive only the software version used" + }, + "targets": "valid targets are: domain.tld and jid/resource", + "command": "!version $target $optional_argument" + }, + "xep": { + "info": "receive abstract of referenced XEP", + "optional_args": { + "last_revision_tags": [ + "date", + "version", + "initials", + "remark" + ], + "tags": [ + "number", + "title", + "abstract", + "type", + "status", + "approver", + "shortname", + "sig", + "lastcall", + "date", + "version", + "initials", + "remark" + ] + }, + "targets": "any valid integer", + "command": "!xep $xepnumber $optional_argument" + }, + "info": { + "info": "pooled command to accumulate dataset composed of uptime version and contact", + "targets": "valid target is: domain.tld", + "command": "!info $target" + } + } + }, + "functions": { + "keywords": { + "all": [ + "!help", + "!uptime", + "!version", + "!contact", + "!xep", + "!info" + ], + "domain": [ + "!uptime", + "!version", + "!contact" + ], + "expand": { + "!info": ["!uptime", "!version", "!contact"] + }, + "noarg": [ + "!help" + ], + "number": [ + "!xep" + ] + }, + "answers": [ + "I heard that %s", + "%s I am sorry for that.", + "%s did you try turning it off and on again?" + ] + } +} \ No newline at end of file diff --git a/common/strings.py b/common/strings.py index 6f1b629..6941a6b 100644 --- a/common/strings.py +++ b/common/strings.py @@ -1,55 +1,36 @@ # -*- coding: utf-8 -*- -from random import randint +from random import choice +import json 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, key=""): + def __init__(self): + with open("./common/strings.json") as basefile: + self.strings = json.load(basefile) + + self.helpfile = self.strings["help"] + self.keywords = self.strings["functions"]["keywords"] + self.replys = self.strings["functions"]["answers"] + + def keys(self, key=None): # if specific keyword in referenced return that - if key in self.keywords.keys(): + if key in self.keywords: return self.keywords[key] # in any other case return the whole dict - return self.keywords["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 + return self.keywords["all"] + + def help_doc(self, key=None): + # if specific key is referenced return only that key + if key is not None: + return self.helpfile["help_advanced"][key] + + # in any other case return basic dict + return self.helpfile["help_basic"] + + def answers(self, nick=None): + # return pseudo random answer + return choice(self.replys) % nick -- cgit v1.2.3-54-g00ecf