core/homeassistant/util/__init__.py

305 lines
9.3 KiB
Python
Raw Normal View History

2016-03-07 22:20:48 +00:00
"""Helper methods for various modules."""
from collections.abc import MutableSet
2014-11-28 23:34:42 +00:00
from itertools import chain
import threading
from datetime import datetime
2013-10-07 07:15:47 +00:00
import re
2014-04-15 06:48:00 +00:00
import enum
import socket
import random
import string
from functools import wraps
2016-02-10 07:27:01 +00:00
from types import MappingProxyType
from unicodedata import normalize
2013-10-07 07:15:47 +00:00
from typing import Any, Optional, TypeVar, Callable, Sequence, KeysView, Union
2016-04-16 07:55:35 +00:00
from .dt import as_local, utcnow
T = TypeVar('T')
U = TypeVar('U')
2014-03-16 00:57:16 +00:00
RE_SANITIZE_FILENAME = re.compile(r'(~|\.\.|/|\\)')
2014-10-22 06:52:24 +00:00
RE_SANITIZE_PATH = re.compile(r'(~|\.(\.)+)')
2015-09-16 06:35:28 +00:00
RE_SLUGIFY = re.compile(r'[^a-z0-9_]+')
2013-11-11 00:46:48 +00:00
def sanitize_filename(filename: str) -> str:
2016-03-07 22:20:48 +00:00
r"""Sanitize a filename by removing .. / and \\."""
return RE_SANITIZE_FILENAME.sub("", filename)
def sanitize_path(path: str) -> str:
2016-03-07 22:20:48 +00:00
"""Sanitize a path by removing ~ and .."""
2014-10-22 06:52:24 +00:00
return RE_SANITIZE_PATH.sub("", path)
def slugify(text: str) -> str:
2016-03-07 22:20:48 +00:00
"""Slugify a given text."""
text = normalize('NFKD', text).lower().replace(" ", "_")
return RE_SLUGIFY.sub("", text)
def repr_helper(inp: Any) -> str:
2016-03-07 22:20:48 +00:00
"""Help creating a more readable string representation of objects."""
2016-02-10 07:27:01 +00:00
if isinstance(inp, (dict, MappingProxyType)):
return ", ".join(
repr_helper(key)+"="+repr_helper(item) for key, item
2014-03-12 05:49:54 +00:00
in inp.items())
elif isinstance(inp, datetime):
2016-04-16 07:55:35 +00:00
return as_local(inp).isoformat()
else:
return str(inp)
def convert(value: T, to_type: Callable[[T], U],
default: Optional[U]=None) -> Optional[U]:
2016-03-07 22:20:48 +00:00
"""Convert value to to_type, returns default if fails."""
try:
2014-03-26 07:08:50 +00:00
return default if value is None else to_type(value)
except (ValueError, TypeError):
# If value could not be converted
return default
def ensure_unique_string(preferred_string: str, current_strings:
Union[Sequence[str], KeysView[str]]) -> str:
2016-03-07 22:20:48 +00:00
"""Return a string that is not present in current_strings.
If preferred string exists will append _2, _3, ..
"""
test_string = preferred_string
current_strings_set = set(current_strings)
tries = 1
while test_string in current_strings_set:
tries += 1
test_string = "{}_{}".format(preferred_string, tries)
return test_string
# Taken from: http://stackoverflow.com/a/11735897
def get_local_ip():
2016-03-07 22:20:48 +00:00
"""Try to determine the local IP address of the machine."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Use Google Public DNS server to determine own IP
sock.connect(('8.8.8.8', 80))
2015-08-03 15:05:33 +00:00
return sock.getsockname()[0]
except socket.error:
return socket.gethostbyname(socket.gethostname())
2015-08-03 15:05:33 +00:00
finally:
sock.close()
# Taken from http://stackoverflow.com/a/23728630
def get_random_string(length=10):
2016-03-07 22:20:48 +00:00
"""Return a random string with letters and digits."""
generator = random.SystemRandom()
source_chars = string.ascii_letters + string.digits
return ''.join(generator.choice(source_chars) for _ in range(length))
2014-04-15 06:48:00 +00:00
class OrderedEnum(enum.Enum):
2016-03-07 22:20:48 +00:00
"""Taken from Python 3.4.0 docs."""
2014-04-15 06:48:00 +00:00
# pylint: disable=no-init
2014-04-15 06:48:00 +00:00
def __ge__(self, other):
2016-03-07 22:20:48 +00:00
"""Return the greater than element."""
2014-04-15 06:48:00 +00:00
if self.__class__ is other.__class__:
return self.value >= other.value
return NotImplemented
def __gt__(self, other):
2016-03-07 22:20:48 +00:00
"""Return the greater element."""
2014-04-15 06:48:00 +00:00
if self.__class__ is other.__class__:
return self.value > other.value
return NotImplemented
def __le__(self, other):
2016-03-07 22:20:48 +00:00
"""Return the lower than element."""
2014-04-15 06:48:00 +00:00
if self.__class__ is other.__class__:
return self.value <= other.value
return NotImplemented
def __lt__(self, other):
2016-03-07 22:20:48 +00:00
"""Return the lower element."""
2014-04-15 06:48:00 +00:00
if self.__class__ is other.__class__:
return self.value < other.value
return NotImplemented
class OrderedSet(MutableSet):
2016-03-07 22:20:48 +00:00
"""Ordered set taken from http://code.activestate.com/recipes/576694/."""
2014-11-28 23:34:42 +00:00
def __init__(self, iterable=None):
2016-03-07 22:20:48 +00:00
"""Initialize the set."""
2014-11-28 23:34:42 +00:00
self.end = end = []
end += [None, end, end] # sentinel node for doubly linked list
self.map = {} # key --> [key, prev, next]
if iterable is not None:
self |= iterable
def __len__(self):
2016-03-07 22:20:48 +00:00
"""Return the length of the set."""
2014-11-28 23:34:42 +00:00
return len(self.map)
def __contains__(self, key):
2016-03-07 22:20:48 +00:00
"""Check if key is in set."""
2014-11-28 23:34:42 +00:00
return key in self.map
def add(self, key):
2016-03-07 22:20:48 +00:00
"""Add an element to the end of the set."""
2014-11-28 23:34:42 +00:00
if key not in self.map:
end = self.end
curr = end[1]
curr[2] = end[1] = self.map[key] = [key, curr, end]
def promote(self, key):
2016-03-07 22:20:48 +00:00
"""Promote element to beginning of the set, add if not there."""
if key in self.map:
self.discard(key)
begin = self.end[2]
curr = begin[1]
curr[2] = begin[1] = self.map[key] = [key, curr, begin]
2014-11-28 23:34:42 +00:00
def discard(self, key):
2016-03-07 22:20:48 +00:00
"""Discard an element from the set."""
2014-11-28 23:34:42 +00:00
if key in self.map:
key, prev_item, next_item = self.map.pop(key)
prev_item[2] = next_item
next_item[1] = prev_item
def __iter__(self):
2016-03-07 22:20:48 +00:00
"""Iteration of the set."""
2014-11-28 23:34:42 +00:00
end = self.end
curr = end[2]
while curr is not end:
yield curr[0]
curr = curr[2]
def __reversed__(self):
2016-03-10 07:34:38 +00:00
"""Reverse the ordering."""
2014-11-28 23:34:42 +00:00
end = self.end
curr = end[1]
while curr is not end:
yield curr[0]
curr = curr[1]
# pylint: disable=arguments-differ
def pop(self, last=True):
2016-03-07 22:20:48 +00:00
"""Pop element of the end of the set.
2016-01-26 23:08:06 +00:00
Set last=False to pop from the beginning.
"""
2014-11-28 23:34:42 +00:00
if not self:
raise KeyError('set is empty')
key = self.end[1][0] if last else self.end[2][0]
self.discard(key)
return key
def update(self, *args):
2016-03-07 22:20:48 +00:00
"""Add elements from args to the set."""
2014-11-28 23:34:42 +00:00
for item in chain(*args):
self.add(item)
def __repr__(self):
2016-03-07 22:20:48 +00:00
"""Return the representation."""
2014-11-28 23:34:42 +00:00
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self))
def __eq__(self, other):
2016-03-07 22:20:48 +00:00
"""Return the comparision."""
2014-11-28 23:34:42 +00:00
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
return set(self) == set(other)
class Throttle(object):
2016-03-07 22:20:48 +00:00
"""A class for throttling the execution of tasks.
This method decorator adds a cooldown to a method to prevent it from being
called more then 1 time within the timedelta interval `min_time` after it
returned its result.
Calling a method a second time during the interval will return None.
Pass keyword argument `no_throttle=True` to the wrapped method to make
the call not throttled.
Decorator takes in an optional second timedelta interval to throttle the
'no_throttle' calls.
Adds a datetime attribute `last_call` to the method.
"""
def __init__(self, min_time, limit_no_throttle=None):
2016-03-07 22:20:48 +00:00
"""Initialize the throttle."""
self.min_time = min_time
self.limit_no_throttle = limit_no_throttle
def __call__(self, method):
2016-03-07 22:20:48 +00:00
"""Caller for the throttle."""
if self.limit_no_throttle is not None:
method = Throttle(self.limit_no_throttle)(method)
2015-10-11 17:42:42 +00:00
# Different methods that can be passed in:
# - a function
# - an unbound function on a class
# - a method (bound function on a class)
# We want to be able to differentiate between function and unbound
# methods (which are considered functions).
2015-10-09 06:49:55 +00:00
# All methods have the classname in their qualname seperated by a '.'
# Functions have a '.' in their qualname if defined inline, but will
# be prefixed by '.<locals>.' so we strip that out.
2015-10-11 17:42:42 +00:00
is_func = (not hasattr(method, '__self__') and
'.' not in method.__qualname__.split('.<locals>.')[-1])
2015-10-09 06:49:55 +00:00
@wraps(method)
def wrapper(*args, **kwargs):
2016-03-07 22:20:48 +00:00
"""Wrapper that allows wrapped to be called only once per min_time.
If we cannot acquire the lock, it is running so return None.
"""
2015-10-11 18:04:16 +00:00
# pylint: disable=protected-access
2015-10-11 17:42:42 +00:00
if hasattr(method, '__self__'):
host = method.__self__
elif is_func:
host = wrapper
else:
host = args[0] if args else wrapper
if not hasattr(host, '_throttle'):
host._throttle = {}
2015-10-09 06:49:55 +00:00
if id(self) not in host._throttle:
host._throttle[id(self)] = [threading.Lock(), None]
throttle = host._throttle[id(self)]
if not throttle[0].acquire(False):
2015-09-13 05:56:49 +00:00
return None
2015-10-09 06:49:55 +00:00
# Check if method is never called or no_throttle is given
force = not throttle[1] or kwargs.pop('no_throttle', False)
2015-09-13 05:56:49 +00:00
2015-10-09 06:49:55 +00:00
try:
if force or utcnow() - throttle[1] > self.min_time:
2015-09-13 05:56:49 +00:00
result = method(*args, **kwargs)
throttle[1] = utcnow()
2015-09-13 05:56:49 +00:00
return result
else:
return None
finally:
throttle[0].release()
return wrapper