Files
tryton/trytond/tools/misc.py
T

284 lines
8.5 KiB
Python
Raw Normal View History

2008-06-07 12:58:28 +02:00
# -*- coding: utf-8 -*-
2014-12-02 12:54:01 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2007-12-10 01:04:13 +01:00
"""
Miscelleanous tools used by tryton
"""
2019-11-26 22:27:36 +01:00
import importlib
import io
2012-04-01 23:08:15 +02:00
import os
2019-11-26 22:27:36 +01:00
import re
2012-04-01 23:08:15 +02:00
import sys
2019-11-26 22:27:36 +01:00
import types
import unicodedata
import warnings
2013-09-18 23:11:09 +02:00
from array import array
2020-07-21 00:36:40 +02:00
from functools import wraps
2014-07-17 18:53:05 +02:00
from itertools import islice
2013-09-06 22:32:37 +02:00
from sql import Literal
2020-10-08 21:34:15 +02:00
from sql.conditionals import Case
2013-09-06 22:32:37 +02:00
from sql.operators import Or
from trytond.const import OPERATORS
2007-12-10 01:04:13 +01:00
2012-04-01 23:08:15 +02:00
def file_open(name, mode="r", subdir='modules', encoding=None):
2007-12-10 01:04:13 +01:00
"""Open a file from the root dir, using a subdir folder."""
2008-11-18 17:40:30 +01:00
from trytond.modules import EGG_MODULES
2018-06-26 19:11:19 +02:00
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2008-11-18 17:40:30 +01:00
2016-08-30 12:15:25 +02:00
def secure_join(root, *paths):
"Join paths and ensure it still below root"
path = os.path.join(root, *paths)
path = os.path.normpath(path)
2017-04-03 18:23:41 +02:00
if not path.startswith(os.path.join(root, '')):
2016-08-30 12:15:25 +02:00
raise IOError("Permission denied: %s" % name)
return path
2011-05-26 15:21:15 +02:00
egg_name = False
2008-11-18 17:40:30 +01:00
if subdir == 'modules':
module_name = name.split(os.sep)[0]
if module_name in EGG_MODULES:
2010-02-18 18:50:35 +01:00
epoint = EGG_MODULES[module_name]
mod_path = os.path.join(epoint.dist.location,
*epoint.module_name.split('.')[:-1])
2017-07-03 18:13:27 +02:00
mod_path = os.path.abspath(mod_path)
2016-08-30 12:15:25 +02:00
egg_name = secure_join(mod_path, name)
2011-05-26 15:21:15 +02:00
if not os.path.isfile(egg_name):
# Find module in path
for path in sys.path:
mod_path = os.path.join(path,
*epoint.module_name.split('.')[:-1])
2017-07-03 18:13:27 +02:00
mod_path = os.path.abspath(mod_path)
2016-08-30 12:15:25 +02:00
egg_name = secure_join(mod_path, name)
2011-05-26 15:21:15 +02:00
if os.path.isfile(egg_name):
break
2011-05-26 15:21:15 +02:00
if not os.path.isfile(egg_name):
# When testing modules from setuptools location is the
# module directory
2016-08-30 12:15:25 +02:00
egg_name = secure_join(
2011-05-26 15:21:15 +02:00
os.path.dirname(epoint.dist.location), name)
2008-11-18 17:40:30 +01:00
2007-12-10 01:04:13 +01:00
if subdir:
2013-02-27 22:50:31 +01:00
if (subdir == 'modules'
and (name.startswith('ir' + os.sep)
or name.startswith('res' + os.sep)
2013-09-16 11:38:11 +02:00
or name.startswith('tests' + os.sep))):
2016-08-30 12:15:25 +02:00
name = secure_join(root_path, name)
2007-12-10 01:04:13 +01:00
else:
2016-08-30 12:15:25 +02:00
name = secure_join(root_path, subdir, name)
2007-12-10 01:04:13 +01:00
else:
2016-08-30 12:15:25 +02:00
name = secure_join(root_path, name)
2007-12-10 01:04:13 +01:00
2011-05-26 15:21:15 +02:00
for i in (name, egg_name):
2007-12-10 01:04:13 +01:00
if i and os.path.isfile(i):
2016-02-09 21:04:39 +01:00
return io.open(i, mode, encoding=encoding)
2007-12-10 01:04:13 +01:00
2011-05-26 15:21:15 +02:00
raise IOError('File not found : %s ' % name)
2007-12-10 01:04:13 +01:00
2012-04-01 23:08:15 +02:00
2009-09-23 14:47:14 +02:00
def get_smtp_server():
"""
Instanciate, configure and return a SMTP or SMTP_SSL instance from
smtplib.
:return: A SMTP instance. The quit() method must be call when all
the calls to sendmail() have been made.
"""
2020-07-21 14:38:36 +02:00
from trytond.sendmail import get_smtp_server
2016-03-28 22:40:37 +02:00
warnings.warn(
'get_smtp_server is deprecated use trytond.sendmail',
DeprecationWarning)
return get_smtp_server()
2007-12-10 01:04:13 +01:00
2012-04-01 23:08:15 +02:00
2009-09-08 13:03:57 +02:00
def reduce_ids(field, ids):
'''
2013-09-06 22:32:37 +02:00
Return a small SQL expression for the list of ids and the sql column
2009-09-08 13:03:57 +02:00
'''
2014-07-17 18:53:05 +02:00
ids = list(ids)
2009-12-21 16:09:18 +01:00
if not ids:
2013-09-06 22:32:37 +02:00
return Literal(False)
2012-01-28 12:46:09 +01:00
assert all(x.is_integer() for x in ids if isinstance(x, float)), \
'ids must be integer'
2018-06-26 19:11:19 +02:00
ids = list(map(int, ids))
2009-09-08 13:03:57 +02:00
ids.sort()
prev = ids.pop(0)
continue_list = [prev, prev]
2013-09-18 23:11:09 +02:00
discontinue_list = array('l')
2013-09-06 22:32:37 +02:00
sql = Or()
2009-09-08 13:03:57 +02:00
for i in ids:
if i == prev:
continue
if i != prev + 1:
if continue_list[-1] - continue_list[0] < 5:
discontinue_list.extend([continue_list[0] + x for x in
range(continue_list[-1] - continue_list[0] + 1)])
else:
2013-09-06 22:32:37 +02:00
sql.append((field >= continue_list[0])
& (field <= continue_list[-1]))
2009-09-08 13:03:57 +02:00
continue_list = []
continue_list.append(i)
prev = i
if continue_list[-1] - continue_list[0] < 5:
discontinue_list.extend([continue_list[0] + x for x in
2012-01-28 12:46:09 +01:00
range(continue_list[-1] - continue_list[0] + 1)])
2009-09-08 13:03:57 +02:00
else:
2013-09-06 22:32:37 +02:00
sql.append((field >= continue_list[0]) & (field <= continue_list[-1]))
2009-09-08 13:03:57 +02:00
if discontinue_list:
2013-09-06 22:32:37 +02:00
sql.append(field.in_(discontinue_list))
return sql
2009-09-28 11:21:11 +02:00
2012-04-01 23:08:15 +02:00
def reduce_domain(domain):
'''
Reduce domain
'''
if not domain:
return []
operator = 'AND'
2018-06-26 19:11:19 +02:00
if isinstance(domain[0], str):
operator = domain[0]
domain = domain[1:]
result = [operator]
for arg in domain:
2020-03-01 12:33:54 +01:00
if (isinstance(arg, tuple)
or (isinstance(arg, list)
and len(arg) > 2
and arg[1] in OPERATORS)):
2014-12-02 12:54:01 +01:00
# clause
result.append(arg)
elif isinstance(arg, list) and arg:
2014-12-02 12:54:01 +01:00
# sub-domain
sub_domain = reduce_domain(arg)
sub_operator = sub_domain[0]
if sub_operator == operator:
result.extend(sub_domain[1:])
else:
result.append(sub_domain)
2011-07-11 20:57:10 +02:00
else:
result.append(arg)
return result
2014-07-17 18:53:05 +02:00
def grouped_slice(records, count=None):
'Grouped slice'
from trytond.transaction import Transaction
2014-07-17 18:53:05 +02:00
if count is None:
count = Transaction().database.IN_MAX
2019-07-13 00:06:22 +02:00
count = max(1, count)
2018-06-26 19:11:19 +02:00
for i in range(0, len(records), count):
2014-07-17 18:53:05 +02:00
yield islice(records, i, i + count)
def is_instance_method(cls, method):
for klass in cls.__mro__:
type_ = klass.__dict__.get(method)
if type_ is not None:
return isinstance(type_, types.FunctionType)
2016-07-29 10:46:52 +02:00
def resolve(name):
"Resolve a dotted name to a global object."
name = name.split('.')
used = name.pop(0)
found = importlib.import_module(used)
for n in name:
used = used + '.' + n
try:
found = getattr(found, n)
except AttributeError:
found = importlib.import_module(used)
return found
2019-04-11 15:17:30 +02:00
def strip_wildcard(string, wildcard='%', escape='\\'):
"Strip starting and ending wildcard from string"
string = lstrip_wildcard(string, wildcard)
return rstrip_wildcard(string, wildcard, escape)
def lstrip_wildcard(string, wildcard='%'):
"Strip starting wildcard from string"
if not string:
return string
return string.lstrip(wildcard)
def rstrip_wildcard(string, wildcard='%', escape='\\'):
"Strip ending wildcard from string"
if not string:
return string
new_string = string.rstrip(wildcard)
if new_string[-1] == escape:
return string
return new_string
2019-11-26 22:27:36 +01:00
2020-07-21 14:37:01 +02:00
def escape_wildcard(string, wildcards='%_', escape='\\'):
for wildcard in escape + wildcards:
string = string.replace(wildcard, escape + wildcard)
return string
2021-03-21 16:21:05 +01:00
def unescape_wildcard(string, wildcards='%_', escape='\\'):
for wildcard in wildcards + escape:
string = string.replace(escape + wildcard, wildcard)
return string
def is_full_text(value, escape='\\'):
escaped = value.strip('%')
escaped = escaped.replace(escape + '%', '').replace(escape + '_', '')
if '%' in escaped or '_' in escaped:
return False
return value.startswith('%') == value.endswith('%')
2019-11-26 22:27:36 +01:00
_slugify_strip_re = re.compile(r'[^\w\s-]')
_slugify_hyphenate_re = re.compile(r'[-\s]+')
def slugify(value, hyphenate='-'):
if not isinstance(value, str):
value = str(value)
value = unicodedata.normalize('NFKD', value)
value = str(_slugify_strip_re.sub('', value).strip())
return _slugify_hyphenate_re.sub(hyphenate, value)
2020-07-21 00:36:40 +02:00
def sortable_values(func):
"Decorator that makes list of couple values sortable"
@wraps(func)
def wrapper(*args, **kwargs):
result = list(func(*args, **kwargs))
for i, (name, value) in enumerate(list(result)):
result[i] = (name, value is None, value)
return result
return wrapper
2020-10-08 21:34:15 +02:00
def sql_pairing(x, y):
"""Return SQL expression to pair x and y
Pairing function from http://szudzik.com/ElegantPairing.pdf"""
return Case(
(x < y, (y * y) + x),
else_=(x * x) + x + y)
2020-12-25 17:14:57 +01:00
def firstline(text):
"Returns first non-empty line"
try:
return next((x for x in text.splitlines() if x.strip()))
except StopIteration:
return ''
2021-09-13 23:43:03 +02:00
def remove_forbidden_chars(value):
from trytond.model.fields import Char
if value is None:
return value
for c in Char.forbidden_chars:
if c in value:
value = value.replace(c, ' ')
return value.strip()