From 7e3d3f95bfc1bd34b6c69cad89c2dba69e58cd33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sun, 28 Oct 2018 21:26:25 +0100 Subject: [PATCH] Use json format for the servers list --- gajim/common/helpers.py | 13 ++++++ gajim/data/other/servers.json | 77 ++++++++++++++++++++++++++++++++ gajim/data/other/servers.xml | 83 ----------------------------------- gajim/gtk/account_wizard.py | 9 ++-- gajim/gtkgui_helpers.py | 26 ----------- 5 files changed, 95 insertions(+), 113 deletions(-) create mode 100644 gajim/data/other/servers.json delete mode 100644 gajim/data/other/servers.xml diff --git a/gajim/common/helpers.py b/gajim/common/helpers.py index 2260a2e2c..0bc10946a 100644 --- a/gajim/common/helpers.py +++ b/gajim/common/helpers.py @@ -39,6 +39,7 @@ import shlex import socket import time import logging +import json from datetime import datetime, timedelta from distutils.version import LooseVersion as V from encodings.punycode import punycode_encode @@ -1492,3 +1493,15 @@ def get_sync_threshold(jid, archive_info): app.logger.set_archive_infos(jid, sync_threshold=threshold) return threshold return archive_info.sync_threshold + +def load_json(path, key=None, default=None): + try: + with open(path, 'r') as file: + json_dict = json.loads(file.read()) + except Exception: + log.exception('Parsing error') + return default + + if key is None: + return json_dict + return json_dict.get(key, default) diff --git a/gajim/data/other/servers.json b/gajim/data/other/servers.json new file mode 100644 index 000000000..fd558ad12 --- /dev/null +++ b/gajim/data/other/servers.json @@ -0,0 +1,77 @@ +{ + "servers":[ + "0nl1ne.at", + "404.city", + "brauchen.info", + "chatme.im", + "climm.org", + "comm.unicate.me", + "creep.im", + "deshalbfrei.org", + "draugr.de", + "einfachjabber.de", + "forumanalogue.fr", + "im.apinc.org", + "im.flosoft.biz", + "jabber-hosting.de", + "jabber.at", + "jabber.ccc.de", + "jabber.chaotic.de", + "jabber.co.nz", + "jabber.cz", + "jabber.earth.li", + "jabber.fourecks.de", + "jabber.hot-chilli.net", + "jabber.i-pobox.net", + "jabber.me", + "jabber.meta.net.nz", + "jabber.no", + "jabber.sk", + "jabber.org", + "jabber.rueckgr.at", + "jabber.scha.de", + "jabber.second-home.de", + "jabber.smash-net.org", + "jabber.sow.as", + "jabber.theforest.us", + "jabberafrica.org", + "jabberes.org", + "jabberpl.org", + "jabbim.com", + "jabbim.cz", + "jabbim.hu", + "jabbim.pl", + "jabbim.sk", + "jabster.pl", + "jaim.at", + "lightwitch.org", + "linuxlovers.at", + "lsd-25.ru", + "njs.netlab.cz", + "palita.net", + "pandion.im", + "richim.org", + "sss.chaoslab.ru", + "sternenschweif.de", + "suchat.org", + "swissjabber.ch", + "swissjabber.de", + "swissjabber.eu", + "swissjabber.li", + "swissjabber.org", + "tcweb.org", + "tekst.me", + "tigase.im", + "ubuntu-jabber.de", + "ubuntu-jabber.net", + "univers-libre.net", + "uprod.biz", + "verdammung.org", + "wtfismyip.com", + "xabber.de", + "xmpp-hosting.de", + "xmpp.jp", + "xmppnet.de", + "zsim.de" + ] +} diff --git a/gajim/data/other/servers.xml b/gajim/data/other/servers.xml deleted file mode 100644 index 0d540ef22..000000000 --- a/gajim/data/other/servers.xml +++ /dev/null @@ -1,83 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gajim/gtk/account_wizard.py b/gajim/gtk/account_wizard.py index 0fc4984e2..61ee6979b 100644 --- a/gajim/gtk/account_wizard.py +++ b/gajim/gtk/account_wizard.py @@ -55,10 +55,11 @@ class AccountCreationWizard: self.update_proxy_list() - # parse servers.xml - servers_xml = os.path.join( - configpaths.get('DATA'), 'other', 'servers.xml') - servers = gtkgui_helpers.parse_server_xml(servers_xml) + # parse servers.json + server_file_path = os.path.join( + configpaths.get('DATA'), 'other', 'servers.json') + servers = helpers.load_json(server_file_path, 'servers', []) + servers_model = self.xml.get_object('server_liststore') for server in servers: servers_model.append((server,)) diff --git a/gajim/gtkgui_helpers.py b/gajim/gtkgui_helpers.py index f332a8a9c..90d37267d 100644 --- a/gajim/gtkgui_helpers.py +++ b/gajim/gtkgui_helpers.py @@ -236,32 +236,6 @@ def scroll_to_end(widget): adj_h.set_value(0) return False - -class ServersXMLHandler(ContentHandler): - def __init__(self): - ContentHandler.__init__(self) - self.servers = [] - - def startElement(self, name, attributes): - if name == 'item': - if 'jid' in attributes.getNames(): - self.servers.append(attributes.getValue('jid')) - - def endElement(self, name): - pass - -def parse_server_xml(path_to_file): - try: - handler = ServersXMLHandler() - xml.sax.parse(path_to_file, handler) - return handler.servers - # handle exception if unable to open file - except IOError as message: - print(_('Error reading file:') + str(message), file=sys.stderr) - # handle exception parsing file - except xml.sax.SAXParseException as message: - print(_('Error parsing file:') + str(message), file=sys.stderr) - def set_unset_urgency_hint(window, unread_messages_no): """ Sets/unset urgency hint in window argument depending if we have unread