Move _compute_resource() into helper module

This commit is contained in:
Philipp Hörist 2019-05-18 22:01:45 +02:00
parent 02915163dc
commit e1bcbd2f1f
2 changed files with 28 additions and 28 deletions

View file

@ -30,7 +30,6 @@
import sys import sys
import random import random
import socket
import operator import operator
import time import time
@ -40,8 +39,6 @@ import logging
import base64 import base64
import ssl import ssl
from functools import partial from functools import partial
import string
from string import Template
from urllib.request import urlopen from urllib.request import urlopen
from urllib.error import URLError from urllib.error import URLError
@ -101,7 +98,7 @@ class CommonConnection:
self.connection = None # xmpppy ClientCommon instance self.connection = None # xmpppy ClientCommon instance
self.is_zeroconf = False self.is_zeroconf = False
self.password = None self.password = None
self.server_resource = self._compute_resource() self.server_resource = helpers.get_resource(self.name)
self.status = '' self.status = ''
self.old_show = '' self.old_show = ''
self.priority = app.get_priority(name, 'offline') self.priority = app.get_priority(name, 'offline')
@ -148,18 +145,6 @@ class CommonConnection:
con.UnregisterHandler(*handler) con.UnregisterHandler(*handler)
self.handlers_registered = False 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): def dispatch(self, event, data):
""" """
Always passes account name as first param Always passes account name as first param
@ -397,7 +382,7 @@ class CommonConnection:
# set old_show to requested 'show' in case we need to # set old_show to requested 'show' in case we need to
# recconect before we auth to server # recconect before we auth to server
self.old_show = show self.old_show = show
self.server_resource = self._compute_resource() self.server_resource = helpers.get_resource(self.name)
self.connect_and_init(show, msg) self.connect_and_init(show, msg)
return return

View file

@ -45,11 +45,13 @@ import logging
import json import json
import shutil import shutil
import collections import collections
import random
import string
from string import Template
from io import StringIO from io import StringIO
from datetime import datetime, timedelta from datetime import datetime, timedelta
from distutils.version import LooseVersion as V from distutils.version import LooseVersion as V
from encodings.punycode import punycode_encode from encodings.punycode import punycode_encode
from string import Template
from functools import wraps from functools import wraps
import nbxmpp 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 If any of the params is not present (None or 0) the action on it is not
performed performed
""" """
def _cut_if_long(string): def _cut_if_long(string_):
if len(string) > max_chars: if len(string_) > max_chars:
string = string[:max_chars - 3] + '' string_ = string_[:max_chars - 3] + ''
return string return string_
if max_lines == 0: if max_lines == 0:
lines = text.split('\n') lines = text.split('\n')
@ -571,13 +573,13 @@ def datetime_tuple(timestamp):
tim = tim.timetuple() tim = tim.timetuple()
return tim return tim
def convert_bytes(string): def convert_bytes(string_):
suffix = '' suffix = ''
# IEC standard says KiB = 1024 bytes KB = 1000 bytes # IEC standard says KiB = 1024 bytes KB = 1000 bytes
# but do we use the standard? # but do we use the standard?
use_kib_mib = app.config.get('use_kib_mib') use_kib_mib = app.config.get('use_kib_mib')
align = 1024. align = 1024.
bytes_ = float(string) bytes_ = float(string_)
if bytes_ >= align: if bytes_ >= align:
bytes_ = round(bytes_/align, 1) bytes_ = round(bytes_/align, 1)
if bytes_ >= align: 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')).\ return hashlib.sha1(("%s%s%s" % (sid, initiator, target)).encode('utf-8')).\
hexdigest() hexdigest()
def remove_invalid_xml_chars(string): def remove_invalid_xml_chars(string_):
if string: if string_:
string = re.sub(app.interface.invalid_XML_chars_re, '', string) string_ = re.sub(app.interface.invalid_XML_chars_re, '', string_)
return string return string_
def get_random_string_16(): def get_random_string_16():
""" """
@ -1569,3 +1571,16 @@ def open_uri_with_custom(config_app, uri):
def geo_provider_from_location(lat, lon): def geo_provider_from_location(lat, lon):
return ('https://www.openstreetmap.org/?' return ('https://www.openstreetmap.org/?'
'mlat=%s&mlon=%s&zoom=16') % (lat, lon) '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