Make poezio.common more Cython-friendly.
Also remove support for OLD_PYTHON (before 3.3), some unused constants, mark private functions with a leading underscore, and clean up some pep8 violations.
This commit is contained in:
parent
bfb02d64a8
commit
840ad7433f
1 changed files with 45 additions and 62 deletions
107
poezio/common.py
107
poezio/common.py
|
@ -9,7 +9,6 @@
|
||||||
Various useful functions.
|
Various useful functions.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from sys import version_info
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from slixmpp import JID, InvalidJID
|
from slixmpp import JID, InvalidJID
|
||||||
from poezio_shlex import shlex
|
from poezio_shlex import shlex
|
||||||
|
@ -23,16 +22,6 @@ import time
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
|
||||||
# Needed to avoid datetime.datetime.timestamp()
|
|
||||||
# on python < 3.3. Older versions do not get good dst detection.
|
|
||||||
OLD_PYTHON = (version_info.major + version_info.minor/10) < 3.3
|
|
||||||
|
|
||||||
ROOM_STATE_NONE = 11
|
|
||||||
ROOM_STATE_CURRENT = 10
|
|
||||||
ROOM_STATE_PRIVATE = 15
|
|
||||||
ROOM_STATE_MESSAGE = 12
|
|
||||||
ROOM_STATE_HL = 13
|
|
||||||
|
|
||||||
def get_base64_from_file(path):
|
def get_base64_from_file(path):
|
||||||
"""
|
"""
|
||||||
Convert the content of a file to base64
|
Convert the content of a file to base64
|
||||||
|
@ -48,7 +37,7 @@ def get_base64_from_file(path):
|
||||||
return (None, None, "File does not exist")
|
return (None, None, "File does not exist")
|
||||||
size = os.path.getsize(path)
|
size = os.path.getsize(path)
|
||||||
if size > 16384:
|
if size > 16384:
|
||||||
return (None, None,"File is too big")
|
return (None, None, "File is too big")
|
||||||
fdes = open(path, 'rb')
|
fdes = open(path, 'rb')
|
||||||
data = fdes.read()
|
data = fdes.read()
|
||||||
encoded = base64.encodestring(data)
|
encoded = base64.encodestring(data)
|
||||||
|
@ -56,7 +45,7 @@ def get_base64_from_file(path):
|
||||||
mime_type = mimetypes.guess_type(path)[0]
|
mime_type = mimetypes.guess_type(path)[0]
|
||||||
return (encoded, mime_type, sha1)
|
return (encoded, mime_type, sha1)
|
||||||
|
|
||||||
def get_output_of_command(command):
|
def _get_output_of_command(command):
|
||||||
"""
|
"""
|
||||||
Runs a command and returns its output.
|
Runs a command and returns its output.
|
||||||
|
|
||||||
|
@ -69,7 +58,7 @@ def get_output_of_command(command):
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def is_in_path(command, return_abs_path=False):
|
def _is_in_path(command, return_abs_path=False):
|
||||||
"""
|
"""
|
||||||
Check if *command* is in the $PATH or not.
|
Check if *command* is in the $PATH or not.
|
||||||
|
|
||||||
|
@ -93,25 +82,25 @@ def is_in_path(command, return_abs_path=False):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
DISTRO_INFO = {
|
DISTRO_INFO = {
|
||||||
'Arch Linux': '/etc/arch-release',
|
'Arch Linux': '/etc/arch-release',
|
||||||
'Aurox Linux': '/etc/aurox-release',
|
'Aurox Linux': '/etc/aurox-release',
|
||||||
'Conectiva Linux': '/etc/conectiva-release',
|
'Conectiva Linux': '/etc/conectiva-release',
|
||||||
'CRUX': '/usr/bin/crux',
|
'CRUX': '/usr/bin/crux',
|
||||||
'Debian GNU/Linux': '/etc/debian_version',
|
'Debian GNU/Linux': '/etc/debian_version',
|
||||||
'Fedora Linux': '/etc/fedora-release',
|
'Fedora Linux': '/etc/fedora-release',
|
||||||
'Gentoo Linux': '/etc/gentoo-release',
|
'Gentoo Linux': '/etc/gentoo-release',
|
||||||
'Linux from Scratch': '/etc/lfs-release',
|
'Linux from Scratch': '/etc/lfs-release',
|
||||||
'Mandrake Linux': '/etc/mandrake-release',
|
'Mandrake Linux': '/etc/mandrake-release',
|
||||||
'Slackware Linux': '/etc/slackware-version',
|
'Slackware Linux': '/etc/slackware-version',
|
||||||
'Solaris/Sparc': '/etc/release',
|
'Solaris/Sparc': '/etc/release',
|
||||||
'Source Mage': '/etc/sourcemage_version',
|
'Source Mage': '/etc/sourcemage_version',
|
||||||
'SUSE Linux': '/etc/SuSE-release',
|
'SUSE Linux': '/etc/SuSE-release',
|
||||||
'Sun JDS': '/etc/sun-release',
|
'Sun JDS': '/etc/sun-release',
|
||||||
'PLD Linux': '/etc/pld-release',
|
'PLD Linux': '/etc/pld-release',
|
||||||
'Yellow Dog Linux': '/etc/yellowdog-release',
|
'Yellow Dog Linux': '/etc/yellowdog-release',
|
||||||
# many distros use the /etc/redhat-release for compatibility
|
# many distros use the /etc/redhat-release for compatibility
|
||||||
# so Redhat is the last
|
# so Redhat is the last
|
||||||
'Redhat Linux': '/etc/redhat-release'
|
'Redhat Linux': '/etc/redhat-release'
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_os_info():
|
def get_os_info():
|
||||||
|
@ -124,7 +113,7 @@ def get_os_info():
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
executable = 'lsb_release'
|
executable = 'lsb_release'
|
||||||
params = ' --description --codename --release --short'
|
params = ' --description --codename --release --short'
|
||||||
full_path_to_executable = is_in_path(executable, return_abs_path = True)
|
full_path_to_executable = _is_in_path(executable, return_abs_path=True)
|
||||||
if full_path_to_executable:
|
if full_path_to_executable:
|
||||||
command = executable + params
|
command = executable + params
|
||||||
process = subprocess.Popen([command], shell=True,
|
process = subprocess.Popen([command], shell=True,
|
||||||
|
@ -144,7 +133,7 @@ def get_os_info():
|
||||||
if os.access(path_to_file, os.X_OK):
|
if os.access(path_to_file, os.X_OK):
|
||||||
# the file is executable (f.e. CRUX)
|
# the file is executable (f.e. CRUX)
|
||||||
# yes, then run it and get the first line of output.
|
# yes, then run it and get the first line of output.
|
||||||
text = get_output_of_command(path_to_file)[0]
|
text = _get_output_of_command(path_to_file)[0]
|
||||||
else:
|
else:
|
||||||
fdes = open(path_to_file, encoding='utf-8')
|
fdes = open(path_to_file, encoding='utf-8')
|
||||||
text = fdes.readline().strip() # get only first line
|
text = fdes.readline().strip() # get only first line
|
||||||
|
@ -153,11 +142,11 @@ def get_os_info():
|
||||||
# sourcemage_version and slackware-version files
|
# sourcemage_version and slackware-version files
|
||||||
# have all the info we need (name and version of distro)
|
# have all the info we need (name and version of distro)
|
||||||
if not os.path.basename(path_to_file).startswith(
|
if not os.path.basename(path_to_file).startswith(
|
||||||
'sourcemage') or not\
|
'sourcemage') or not\
|
||||||
os.path.basename(path_to_file).startswith('slackware'):
|
os.path.basename(path_to_file).startswith('slackware'):
|
||||||
text = distro_name + ' ' + text
|
text = distro_name + ' ' + text
|
||||||
elif path_to_file.endswith('aurox-release') or \
|
elif path_to_file.endswith('aurox-release') or \
|
||||||
path_to_file.endswith('arch-release'):
|
path_to_file.endswith('arch-release'):
|
||||||
# file doesn't have version
|
# file doesn't have version
|
||||||
text = distro_name
|
text = distro_name
|
||||||
elif path_to_file.endswith('lfs-release'):
|
elif path_to_file.endswith('lfs-release'):
|
||||||
|
@ -167,14 +156,14 @@ def get_os_info():
|
||||||
return os_info
|
return os_info
|
||||||
|
|
||||||
# our last chance, ask uname and strip it
|
# our last chance, ask uname and strip it
|
||||||
uname_output = get_output_of_command('uname -sr')
|
uname_output = _get_output_of_command('uname -sr')
|
||||||
if uname_output is not None:
|
if uname_output is not None:
|
||||||
os_info = uname_output[0] # only first line
|
os_info = uname_output[0] # only first line
|
||||||
return os_info
|
return os_info
|
||||||
os_info = 'N/A'
|
os_info = 'N/A'
|
||||||
return os_info
|
return os_info
|
||||||
|
|
||||||
def datetime_tuple(timestamp):
|
def _datetime_tuple(timestamp):
|
||||||
"""
|
"""
|
||||||
Convert a timestamp using strptime and the format: %Y%m%dT%H:%M:%S.
|
Convert a timestamp using strptime and the format: %Y%m%dT%H:%M:%S.
|
||||||
|
|
||||||
|
@ -194,7 +183,7 @@ def datetime_tuple(timestamp):
|
||||||
tz_msg = timestamp[15:]
|
tz_msg = timestamp[15:]
|
||||||
try:
|
try:
|
||||||
ret = datetime.strptime(date, '%Y%m%dT%H%M%S')
|
ret = datetime.strptime(date, '%Y%m%dT%H%M%S')
|
||||||
except Exception:
|
except ValueError:
|
||||||
ret = datetime.now()
|
ret = datetime.now()
|
||||||
# add the message timezone if any
|
# add the message timezone if any
|
||||||
try:
|
try:
|
||||||
|
@ -204,7 +193,7 @@ def datetime_tuple(timestamp):
|
||||||
tz_msg = tz_msg.tm_hour * 3600 + tz_msg.tm_min * 60
|
tz_msg = tz_msg.tm_hour * 3600 + tz_msg.tm_min * 60
|
||||||
tz_msg = timedelta(seconds=tz_mod * tz_msg)
|
tz_msg = timedelta(seconds=tz_mod * tz_msg)
|
||||||
ret -= tz_msg
|
ret -= tz_msg
|
||||||
except Exception:
|
except ValueError:
|
||||||
pass # ignore if we got a badly-formatted offset
|
pass # ignore if we got a badly-formatted offset
|
||||||
# convert UTC to local time, with DST etc.
|
# convert UTC to local time, with DST etc.
|
||||||
if time.daylight and time.localtime().tm_isdst:
|
if time.daylight and time.localtime().tm_isdst:
|
||||||
|
@ -225,10 +214,7 @@ def get_utc_time(local_time=None):
|
||||||
local_time = datetime.now()
|
local_time = datetime.now()
|
||||||
isdst = time.localtime().tm_isdst
|
isdst = time.localtime().tm_isdst
|
||||||
else:
|
else:
|
||||||
if OLD_PYTHON:
|
isdst = time.localtime(int(local_time.timestamp())).tm_isdst
|
||||||
isdst = time.localtime(int(local_time.strftime("%s"))).tm_isdst
|
|
||||||
else:
|
|
||||||
isdst = time.localtime(int(local_time.timestamp())).tm_isdst
|
|
||||||
|
|
||||||
if time.daylight and isdst:
|
if time.daylight and isdst:
|
||||||
tz = timedelta(seconds=time.altzone)
|
tz = timedelta(seconds=time.altzone)
|
||||||
|
@ -243,10 +229,7 @@ def get_local_time(utc_time):
|
||||||
"""
|
"""
|
||||||
Get the local time from an UTC time
|
Get the local time from an UTC time
|
||||||
"""
|
"""
|
||||||
if OLD_PYTHON:
|
isdst = time.localtime(int(utc_time.timestamp())).tm_isdst
|
||||||
isdst = time.localtime(int(utc_time.strftime("%s"))).tm_isdst
|
|
||||||
else:
|
|
||||||
isdst = time.localtime(int(utc_time.timestamp())).tm_isdst
|
|
||||||
|
|
||||||
if time.daylight and isdst:
|
if time.daylight and isdst:
|
||||||
tz = timedelta(seconds=time.altzone)
|
tz = timedelta(seconds=time.altzone)
|
||||||
|
@ -269,14 +252,14 @@ def find_delayed_tag(message):
|
||||||
delay_tag = message.find('{urn:xmpp:delay}delay')
|
delay_tag = message.find('{urn:xmpp:delay}delay')
|
||||||
if delay_tag is not None:
|
if delay_tag is not None:
|
||||||
delayed = True
|
delayed = True
|
||||||
date = datetime_tuple(delay_tag.attrib['stamp'])
|
date = _datetime_tuple(delay_tag.attrib['stamp'])
|
||||||
else:
|
else:
|
||||||
# We support the OLD and deprecated XEP: http://xmpp.org/extensions/xep-0091.html
|
# We support the OLD and deprecated XEP: http://xmpp.org/extensions/xep-0091.html
|
||||||
# But it sucks, please, Jabber servers, don't do this :(
|
# But it sucks, please, Jabber servers, don't do this :(
|
||||||
delay_tag = message.find('{jabber:x:delay}x')
|
delay_tag = message.find('{jabber:x:delay}x')
|
||||||
if delay_tag is not None:
|
if delay_tag is not None:
|
||||||
delayed = True
|
delayed = True
|
||||||
date = datetime_tuple(delay_tag.attrib['stamp'])
|
date = _datetime_tuple(delay_tag.attrib['stamp'])
|
||||||
else:
|
else:
|
||||||
delayed = False
|
delayed = False
|
||||||
date = None
|
date = None
|
||||||
|
@ -316,15 +299,15 @@ def find_argument(pos, text, quoted=True):
|
||||||
|
|
||||||
:param int pos: The position to search.
|
:param int pos: The position to search.
|
||||||
:param str text: The text to analyze.
|
:param str text: The text to analyze.
|
||||||
:param quoted: Whether to take quotes into account or not.
|
:param bool quoted: Whether to take quotes into account or not.
|
||||||
:rtype: int
|
:rtype: int
|
||||||
"""
|
"""
|
||||||
if quoted:
|
if quoted:
|
||||||
return find_argument_quoted(pos, text)
|
return _find_argument_quoted(pos, text)
|
||||||
else:
|
else:
|
||||||
return find_argument_unquoted(pos, text)
|
return _find_argument_unquoted(pos, text)
|
||||||
|
|
||||||
def find_argument_quoted(pos, text):
|
def _find_argument_quoted(pos, text):
|
||||||
"""
|
"""
|
||||||
Get the number of the argument at position pos in
|
Get the number of the argument at position pos in
|
||||||
a string with possibly quoted text.
|
a string with possibly quoted text.
|
||||||
|
@ -340,7 +323,7 @@ def find_argument_quoted(pos, text):
|
||||||
|
|
||||||
return count + 1
|
return count + 1
|
||||||
|
|
||||||
def find_argument_unquoted(pos, text):
|
def _find_argument_unquoted(pos, text):
|
||||||
"""
|
"""
|
||||||
Get the number of the argument at position pos in
|
Get the number of the argument at position pos in
|
||||||
a string without interpreting quotes.
|
a string without interpreting quotes.
|
||||||
|
@ -405,10 +388,10 @@ def parse_secs_to_str(duration=0):
|
||||||
hours = (duration % 86400) // 3600
|
hours = (duration % 86400) // 3600
|
||||||
days = duration // 86400
|
days = duration // 86400
|
||||||
|
|
||||||
result += '%sd' % days if days else ''
|
result += '%dd' % days if days else ''
|
||||||
result += '%sh' % hours if hours else ''
|
result += '%dh' % hours if hours else ''
|
||||||
result += '%sm' % mins if mins else ''
|
result += '%dm' % mins if mins else ''
|
||||||
result += '%ss' % secs if secs else ''
|
result += '%ds' % secs if secs else ''
|
||||||
if not result:
|
if not result:
|
||||||
result = '0s'
|
result = '0s'
|
||||||
return result
|
return result
|
||||||
|
@ -439,7 +422,7 @@ def format_tune_string(infos):
|
||||||
|
|
||||||
rating = infos.get('rating')
|
rating = infos.get('rating')
|
||||||
if rating:
|
if rating:
|
||||||
elems.append('[ ' + rating + '/10' + ' ]')
|
elems.append('[ ' + rating + '/10 ]')
|
||||||
length = infos.get('length')
|
length = infos.get('length')
|
||||||
if length:
|
if length:
|
||||||
length = int(length)
|
length = int(length)
|
||||||
|
|
Loading…
Reference in a new issue