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 --- classes/__init__.py | 5 +++++ classes/help.py | 29 +++++++++++++++++++++++++++++ classes/servercontact.py | 26 +++++--------------------- classes/version.py | 11 ++++++----- classes/xep.py | 2 +- 5 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 classes/__init__.py create mode 100644 classes/help.py (limited to 'classes') diff --git a/classes/__init__.py b/classes/__init__.py new file mode 100644 index 0000000..838c8c2 --- /dev/null +++ b/classes/__init__.py @@ -0,0 +1,5 @@ +from classes.version import Version +from classes.servercontact import ServerContact +from classes.uptime import LastActivity +from classes.xep import XEPRequest +from classes.help import Helper diff --git a/classes/help.py b/classes/help.py new file mode 100644 index 0000000..c268520 --- /dev/null +++ b/classes/help.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from common.misc import arg_abbr +from common.strings import StaticAnswers +import json + + +class Helper: + def __init__(self): + self.possible_vars = StaticAnswers().helpfile["help_advanced"].keys() + + def receive(self, target): + # optional argument abbreviation + target = arg_abbr(target, self.possible_vars) + + if target in self.possible_vars: + return StaticAnswers().help_doc(target) + else: + return StaticAnswers().help_doc() + + def format(self, query, target, opt_arg): + doc = self.receive(target) + + if target in self.possible_vars: + answer = json.dumps(StaticAnswers().help_doc(target), indent=4) + + else: + answer = "\n".join(['%s' % value for (_, value) in doc.items()]) + + return answer diff --git a/classes/servercontact.py b/classes/servercontact.py index c2f4ad5..e0e871e 100644 --- a/classes/servercontact.py +++ b/classes/servercontact.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from common.misc import arg_abbr import defusedxml.ElementTree as Et @@ -19,23 +20,6 @@ class ServerContact: self.contact = None self.target, self.opt_arg = None, None - def opt_arg_abbreviation(self): - """ - optional argument abbreviation function - if the provided string > 2 characters the most likely key will be chosen - :return: completes the opt_arg to the most likely one - """ - # if opt_argument is smaller then 2 pass to prohibit multiple answers - if len(self.opt_arg) < 2: - pass - - abbr = str(self.opt_arg) - possible_abbr = ["abuse-addresses", "admin-addresses", "feedback-addresses", "sales-addresses", - "security-addresses", "support-addresses"] - - # searches the best match in the list of possible_abbr and completes the opt_arg to that - self.opt_arg = [s for s in possible_abbr if s.startswith(abbr)][0] - def process(self): # get etree from base xml iq = Et.fromstring(str(self.contact)) @@ -57,11 +41,11 @@ class ServerContact: # iterate over all child elements in node for child in node: - # if one opt_arg is defined return just that one - if self.opt_arg in self.possible_vars: - # check for possible abbreviations to the optional argument - self.opt_arg_abbreviation() + # check for possible abbreviations to the optional argument + self.opt_arg = arg_abbr(self.opt_arg, self.possible_vars) + # if opt_arg is defined and valid return just that one + if self.opt_arg in self.possible_vars: if child.attrib['var'] == self.opt_arg: # add section to result dict and append info result[child.attrib['var']] = list() diff --git a/classes/version.py b/classes/version.py index 1e9ef7e..42b3e61 100644 --- a/classes/version.py +++ b/classes/version.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- - +from common.misc import arg_abbr # XEP-0072: Server Version class Version: @@ -10,17 +10,18 @@ class Version: # init all necessary variables self.software_version = None self.target, self.opt_arg = None, None + self.possible_vars = ["version", "os", "name"] def format_result(self): - # list of all possible opt_arg - possible_opt_args = ["version", "os", "name"] - name = self.software_version['name'] version = self.software_version['version'] os = self.software_version['os'] + # check for possible abbreviations to the optional argument + self.opt_arg = arg_abbr(self.opt_arg, self.possible_vars) + # if opt_arg is given member of possible_opt_args list return that element - if self.opt_arg in possible_opt_args: + if self.opt_arg in self.possible_vars: text = "%s: %s" % (self.opt_arg, self.software_version[self.opt_arg]) # otherwise return full version string diff --git a/classes/xep.py b/classes/xep.py index f5fae61..df401a7 100644 --- a/classes/xep.py +++ b/classes/xep.py @@ -47,10 +47,10 @@ class XEPRequest: r = s.get("https://xmpp.org/extensions/xeplist.xml") r.encoding = 'utf-8' local_etag = head.headers['etag'] + self.xeplist = Et.fromstring(r.content.decode()) with open("./common/xeplist.xml", "w") as file: file.write(r.content.decode()) - self.xeplist = Et.fromstring(r.content.decode()) with open('./common/.etag', 'w') as string: string.write(local_etag) -- cgit v1.2.3-54-g00ecf