util.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. """This module contains several handy functions primarily meant for internal use."""
  2. from __future__ import division
  3. from datetime import date, datetime, time, timedelta, tzinfo
  4. from calendar import timegm
  5. import re
  6. from pytz import timezone, utc
  7. import six
  8. try:
  9. from inspect import signature
  10. except ImportError: # pragma: nocover
  11. from funcsigs import signature
  12. try:
  13. from threading import TIMEOUT_MAX
  14. except ImportError:
  15. TIMEOUT_MAX = 4294967 # Maximum value accepted by Event.wait() on Windows
  16. __all__ = ('asint', 'asbool', 'astimezone', 'convert_to_datetime', 'datetime_to_utc_timestamp',
  17. 'utc_timestamp_to_datetime', 'timedelta_seconds', 'datetime_ceil', 'get_callable_name',
  18. 'obj_to_ref', 'ref_to_obj', 'maybe_ref', 'repr_escape', 'check_callable_args')
  19. class _Undefined(object):
  20. def __nonzero__(self):
  21. return False
  22. def __bool__(self):
  23. return False
  24. def __repr__(self):
  25. return '<undefined>'
  26. undefined = _Undefined() #: a unique object that only signifies that no value is defined
  27. def asint(text):
  28. """
  29. Safely converts a string to an integer, returning ``None`` if the string is ``None``.
  30. :type text: str
  31. :rtype: int
  32. """
  33. if text is not None:
  34. return int(text)
  35. def asbool(obj):
  36. """
  37. Interprets an object as a boolean value.
  38. :rtype: bool
  39. """
  40. if isinstance(obj, str):
  41. obj = obj.strip().lower()
  42. if obj in ('true', 'yes', 'on', 'y', 't', '1'):
  43. return True
  44. if obj in ('false', 'no', 'off', 'n', 'f', '0'):
  45. return False
  46. raise ValueError('Unable to interpret value "%s" as boolean' % obj)
  47. return bool(obj)
  48. def astimezone(obj):
  49. """
  50. Interprets an object as a timezone.
  51. :rtype: tzinfo
  52. """
  53. if isinstance(obj, six.string_types):
  54. return timezone(obj)
  55. if isinstance(obj, tzinfo):
  56. if not hasattr(obj, 'localize') or not hasattr(obj, 'normalize'):
  57. raise TypeError('Only timezones from the pytz library are supported')
  58. if obj.zone == 'local':
  59. raise ValueError(
  60. 'Unable to determine the name of the local timezone -- you must explicitly '
  61. 'specify the name of the local timezone. Please refrain from using timezones like '
  62. 'EST to prevent problems with daylight saving time. Instead, use a locale based '
  63. 'timezone name (such as Europe/Helsinki).')
  64. return obj
  65. if obj is not None:
  66. raise TypeError('Expected tzinfo, got %s instead' % obj.__class__.__name__)
  67. _DATE_REGEX = re.compile(
  68. r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
  69. r'(?: (?P<hour>\d{1,2}):(?P<minute>\d{1,2}):(?P<second>\d{1,2})'
  70. r'(?:\.(?P<microsecond>\d{1,6}))?)?')
  71. def convert_to_datetime(input, tz, arg_name):
  72. """
  73. Converts the given object to a timezone aware datetime object.
  74. If a timezone aware datetime object is passed, it is returned unmodified.
  75. If a native datetime object is passed, it is given the specified timezone.
  76. If the input is a string, it is parsed as a datetime with the given timezone.
  77. Date strings are accepted in three different forms: date only (Y-m-d), date with time
  78. (Y-m-d H:M:S) or with date+time with microseconds (Y-m-d H:M:S.micro).
  79. :param str|datetime input: the datetime or string to convert to a timezone aware datetime
  80. :param datetime.tzinfo tz: timezone to interpret ``input`` in
  81. :param str arg_name: the name of the argument (used in an error message)
  82. :rtype: datetime
  83. """
  84. if input is None:
  85. return
  86. elif isinstance(input, datetime):
  87. datetime_ = input
  88. elif isinstance(input, date):
  89. datetime_ = datetime.combine(input, time())
  90. elif isinstance(input, six.string_types):
  91. m = _DATE_REGEX.match(input)
  92. if not m:
  93. raise ValueError('Invalid date string')
  94. values = [(k, int(v or 0)) for k, v in m.groupdict().items()]
  95. values = dict(values)
  96. datetime_ = datetime(**values)
  97. else:
  98. raise TypeError('Unsupported type for %s: %s' % (arg_name, input.__class__.__name__))
  99. if datetime_.tzinfo is not None:
  100. return datetime_
  101. if tz is None:
  102. raise ValueError(
  103. 'The "tz" argument must be specified if %s has no timezone information' % arg_name)
  104. if isinstance(tz, six.string_types):
  105. tz = timezone(tz)
  106. try:
  107. return tz.localize(datetime_, is_dst=None)
  108. except AttributeError:
  109. raise TypeError(
  110. 'Only pytz timezones are supported (need the localize() and normalize() methods)')
  111. def datetime_to_utc_timestamp(timeval):
  112. """
  113. Converts a datetime instance to a timestamp.
  114. :type timeval: datetime
  115. :rtype: float
  116. """
  117. if timeval is not None:
  118. return timegm(timeval.utctimetuple()) + timeval.microsecond / 1000000
  119. def utc_timestamp_to_datetime(timestamp):
  120. """
  121. Converts the given timestamp to a datetime instance.
  122. :type timestamp: float
  123. :rtype: datetime
  124. """
  125. if timestamp is not None:
  126. return datetime.fromtimestamp(timestamp, utc)
  127. def timedelta_seconds(delta):
  128. """
  129. Converts the given timedelta to seconds.
  130. :type delta: timedelta
  131. :rtype: float
  132. """
  133. return delta.days * 24 * 60 * 60 + delta.seconds + \
  134. delta.microseconds / 1000000.0
  135. def datetime_ceil(dateval):
  136. """
  137. Rounds the given datetime object upwards.
  138. :type dateval: datetime
  139. """
  140. if dateval.microsecond > 0:
  141. return dateval + timedelta(seconds=1, microseconds=-dateval.microsecond)
  142. return dateval
  143. def datetime_repr(dateval):
  144. return dateval.strftime('%Y-%m-%d %H:%M:%S %Z') if dateval else 'None'
  145. def get_callable_name(func):
  146. """
  147. Returns the best available display name for the given function/callable.
  148. :rtype: str
  149. """
  150. # the easy case (on Python 3.3+)
  151. if hasattr(func, '__qualname__'):
  152. return func.__qualname__
  153. # class methods, bound and unbound methods
  154. f_self = getattr(func, '__self__', None) or getattr(func, 'im_self', None)
  155. if f_self and hasattr(func, '__name__'):
  156. f_class = f_self if isinstance(f_self, type) else f_self.__class__
  157. else:
  158. f_class = getattr(func, 'im_class', None)
  159. if f_class and hasattr(func, '__name__'):
  160. return '%s.%s' % (f_class.__name__, func.__name__)
  161. # class or class instance
  162. if hasattr(func, '__call__'):
  163. # class
  164. if hasattr(func, '__name__'):
  165. return func.__name__
  166. # instance of a class with a __call__ method
  167. return func.__class__.__name__
  168. raise TypeError('Unable to determine a name for %r -- maybe it is not a callable?' % func)
  169. def obj_to_ref(obj):
  170. """
  171. Returns the path to the given object.
  172. :rtype: str
  173. """
  174. try:
  175. ref = '%s:%s' % (obj.__module__, get_callable_name(obj))
  176. obj2 = ref_to_obj(ref)
  177. if obj != obj2:
  178. raise ValueError
  179. except Exception:
  180. raise ValueError('Cannot determine the reference to %r' % obj)
  181. return ref
  182. def ref_to_obj(ref):
  183. """
  184. Returns the object pointed to by ``ref``.
  185. :type ref: str
  186. """
  187. if not isinstance(ref, six.string_types):
  188. raise TypeError('References must be strings')
  189. if ':' not in ref:
  190. raise ValueError('Invalid reference')
  191. modulename, rest = ref.split(':', 1)
  192. try:
  193. obj = __import__(modulename)
  194. except ImportError:
  195. raise LookupError('Error resolving reference %s: could not import module' % ref)
  196. try:
  197. for name in modulename.split('.')[1:] + rest.split('.'):
  198. obj = getattr(obj, name)
  199. return obj
  200. except Exception:
  201. raise LookupError('Error resolving reference %s: error looking up object' % ref)
  202. def maybe_ref(ref):
  203. """
  204. Returns the object that the given reference points to, if it is indeed a reference.
  205. If it is not a reference, the object is returned as-is.
  206. """
  207. if not isinstance(ref, str):
  208. return ref
  209. return ref_to_obj(ref)
  210. if six.PY2:
  211. def repr_escape(string):
  212. if isinstance(string, six.text_type):
  213. return string.encode('ascii', 'backslashreplace')
  214. return string
  215. else:
  216. def repr_escape(string):
  217. return string
  218. def check_callable_args(func, args, kwargs):
  219. """
  220. Ensures that the given callable can be called with the given arguments.
  221. :type args: tuple
  222. :type kwargs: dict
  223. """
  224. pos_kwargs_conflicts = [] # parameters that have a match in both args and kwargs
  225. positional_only_kwargs = [] # positional-only parameters that have a match in kwargs
  226. unsatisfied_args = [] # parameters in signature that don't have a match in args or kwargs
  227. unsatisfied_kwargs = [] # keyword-only arguments that don't have a match in kwargs
  228. unmatched_args = list(args) # args that didn't match any of the parameters in the signature
  229. # kwargs that didn't match any of the parameters in the signature
  230. unmatched_kwargs = list(kwargs)
  231. # indicates if the signature defines *args and **kwargs respectively
  232. has_varargs = has_var_kwargs = False
  233. try:
  234. sig = signature(func)
  235. except ValueError:
  236. # signature() doesn't work against every kind of callable
  237. return
  238. for param in six.itervalues(sig.parameters):
  239. if param.kind == param.POSITIONAL_OR_KEYWORD:
  240. if param.name in unmatched_kwargs and unmatched_args:
  241. pos_kwargs_conflicts.append(param.name)
  242. elif unmatched_args:
  243. del unmatched_args[0]
  244. elif param.name in unmatched_kwargs:
  245. unmatched_kwargs.remove(param.name)
  246. elif param.default is param.empty:
  247. unsatisfied_args.append(param.name)
  248. elif param.kind == param.POSITIONAL_ONLY:
  249. if unmatched_args:
  250. del unmatched_args[0]
  251. elif param.name in unmatched_kwargs:
  252. unmatched_kwargs.remove(param.name)
  253. positional_only_kwargs.append(param.name)
  254. elif param.default is param.empty:
  255. unsatisfied_args.append(param.name)
  256. elif param.kind == param.KEYWORD_ONLY:
  257. if param.name in unmatched_kwargs:
  258. unmatched_kwargs.remove(param.name)
  259. elif param.default is param.empty:
  260. unsatisfied_kwargs.append(param.name)
  261. elif param.kind == param.VAR_POSITIONAL:
  262. has_varargs = True
  263. elif param.kind == param.VAR_KEYWORD:
  264. has_var_kwargs = True
  265. # Make sure there are no conflicts between args and kwargs
  266. if pos_kwargs_conflicts:
  267. raise ValueError('The following arguments are supplied in both args and kwargs: %s' %
  268. ', '.join(pos_kwargs_conflicts))
  269. # Check if keyword arguments are being fed to positional-only parameters
  270. if positional_only_kwargs:
  271. raise ValueError('The following arguments cannot be given as keyword arguments: %s' %
  272. ', '.join(positional_only_kwargs))
  273. # Check that the number of positional arguments minus the number of matched kwargs matches the
  274. # argspec
  275. if unsatisfied_args:
  276. raise ValueError('The following arguments have not been supplied: %s' %
  277. ', '.join(unsatisfied_args))
  278. # Check that all keyword-only arguments have been supplied
  279. if unsatisfied_kwargs:
  280. raise ValueError(
  281. 'The following keyword-only arguments have not been supplied in kwargs: %s' %
  282. ', '.join(unsatisfied_kwargs))
  283. # Check that the callable can accept the given number of positional arguments
  284. if not has_varargs and unmatched_args:
  285. raise ValueError(
  286. 'The list of positional arguments is longer than the target callable can handle '
  287. '(allowed: %d, given in args: %d)' % (len(args) - len(unmatched_args), len(args)))
  288. # Check that the callable can accept the given keyword arguments
  289. if not has_var_kwargs and unmatched_kwargs:
  290. raise ValueError(
  291. 'The target callable does not accept the following keyword arguments: %s' %
  292. ', '.join(unmatched_kwargs))