Commit e1bcbd2f authored by Philipp Hörist's avatar Philipp Hörist

Move _compute_resource() into helper module

parent 02915163
......@@ -30,7 +30,6 @@
import sys
import random
import socket
import operator
import time
......@@ -40,8 +39,6 @@ import logging
import base64
import ssl
from functools import partial
import string
from string import Template
from urllib.request import urlopen
from urllib.error import URLError
......@@ -101,7 +98,7 @@ class CommonConnection:
self.connection = None # xmpppy ClientCommon instance
self.is_zeroconf = False
self.password = None
self.server_resource = self._compute_resource()
self.server_resource = helpers.get_resource(self.name)
self.status = ''
self.old_show = ''
self.priority = app.get_priority(name, 'offline')
......@@ -148,18 +145,6 @@ class CommonConnection:
con.UnregisterHandler(*handler)
self.handlers_registered = False
def _compute_resource(self):
resource = app.config.get_per('accounts', self.name, 'resource')
# All valid resource substitution strings should be added to this hash.
if resource:
rand = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(8))
resource = Template(resource).safe_substitute({
'hostname': socket.gethostname(),
'rand': rand
})
app.config.set_per('accounts', self.name, 'resource', resource)
return resource
def dispatch(self, event, data):
"""
Always passes account name as first param
......@@ -397,7 +382,7 @@ class CommonConnection:
# set old_show to requested 'show' in case we need to
# recconect before we auth to server
self.old_show = show
self.server_resource = self._compute_resource()
self.server_resource = helpers.get_resource(self.name)
self.connect_and_init(show, msg)
return
......
......@@ -45,11 +45,13 @@ import logging
import json
import shutil
import collections
import random
import string
from string import Template
from io import StringIO
from datetime import datetime, timedelta
from distutils.version import LooseVersion as V
from encodings.punycode import punycode_encode
from string import Template
from functools import wraps
import nbxmpp
......@@ -511,10 +513,10 @@ def reduce_chars_newlines(text, max_chars=0, max_lines=0):
If any of the params is not present (None or 0) the action on it is not
performed
"""
def _cut_if_long(string):
if len(string) > max_chars:
string = string[:max_chars - 3] + '…'
return string
def _cut_if_long(string_):
if len(string_) > max_chars:
string_ = string_[:max_chars - 3] + '…'
return string_
if max_lines == 0:
lines = text.split('\n')
......@@ -571,13 +573,13 @@ def datetime_tuple(timestamp):
tim = tim.timetuple()
return tim
def convert_bytes(string):
def convert_bytes(string_):
suffix = ''
# IEC standard says KiB = 1024 bytes KB = 1000 bytes
# but do we use the standard?
use_kib_mib = app.config.get('use_kib_mib')
align = 1024.
bytes_ = float(string)
bytes_ = float(string_)
if bytes_ >= align:
bytes_ = round(bytes_/align, 1)
if bytes_ >= align:
......@@ -846,10 +848,10 @@ def get_auth_sha(sid, initiator, target):
return hashlib.sha1(("%s%s%s" % (sid, initiator, target)).encode('utf-8')).\
hexdigest()
def remove_invalid_xml_chars(string):
if string:
string = re.sub(app.interface.invalid_XML_chars_re, '', string)
return string
def remove_invalid_xml_chars(string_):
if string_:
string_ = re.sub(app.interface.invalid_XML_chars_re, '', string_)
return string_
def get_random_string_16():
"""
......@@ -1569,3 +1571,16 @@ def open_uri_with_custom(config_app, uri):
def geo_provider_from_location(lat, lon):
return ('https://www.openstreetmap.org/?'
'mlat=%s&mlon=%s&zoom=16') % (lat, lon)
def get_resource(account):
resource = app.config.get_per('accounts', account, 'resource')
# All valid resource substitution strings should be added to this hash.
if resource:
rand = ''.join(random.choice(
string.ascii_uppercase + string.digits) for _ in range(8))
resource = Template(resource).safe_substitute(
{'hostname': socket.gethostname(),
'rand': rand})
app.config.set_per('accounts', account, 'resource', resource)
return resource
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment