You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.py 27KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) Crossbar.io Technologies GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", fWITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. from __future__ import absolute_import
  27. import os
  28. import time
  29. import struct
  30. import sys
  31. import re
  32. import base64
  33. import math
  34. import random
  35. import binascii
  36. from datetime import datetime, timedelta
  37. from pprint import pformat
  38. from array import array
  39. import six
  40. import txaio
  41. try:
  42. _TLS = True
  43. from OpenSSL import SSL
  44. except ImportError:
  45. _TLS = False
  46. __all__ = ("public",
  47. "encode_truncate",
  48. "xor",
  49. "utcnow",
  50. "utcstr",
  51. "id",
  52. "rid",
  53. "newid",
  54. "rtime",
  55. "Stopwatch",
  56. "Tracker",
  57. "EqualityMixin",
  58. "ObservableMixin",
  59. "IdGenerator",
  60. "generate_token",
  61. "generate_activation_code",
  62. "generate_serial_number",
  63. "generate_user_password")
  64. def public(obj):
  65. """
  66. The public user API of Autobahn is marked using this decorator.
  67. Everything that is not decorated @public is library internal, can
  68. change at any time and should not be used in user program code.
  69. """
  70. try:
  71. obj._is_public = True
  72. except AttributeError:
  73. # FIXME: exceptions.AttributeError: 'staticmethod' object has no attribute '_is_public'
  74. pass
  75. return obj
  76. @public
  77. def encode_truncate(text, limit, encoding='utf8', return_encoded=True):
  78. """
  79. Given a string, return a truncated version of the string such that
  80. the UTF8 encoding of the string is smaller than the given limit.
  81. This function correctly truncates even in the presence of Unicode code
  82. points that encode to multi-byte encodings which must not be truncated
  83. in the middle.
  84. :param text: The (Unicode) string to truncate.
  85. :type text: str
  86. :param limit: The number of bytes to limit the UTF8 encoding to.
  87. :type limit: int
  88. :param encoding: Truncate the string in this encoding (default is ``utf-8``).
  89. :type encoding: str
  90. :param return_encoded: If ``True``, return the string encoded into bytes
  91. according to the specified encoding, else return the string as a string.
  92. :type return_encoded: bool
  93. :returns: The truncated string.
  94. :rtype: str or bytes
  95. """
  96. assert(text is None or type(text) == six.text_type)
  97. assert(type(limit) in six.integer_types)
  98. assert(limit >= 0)
  99. if text is None:
  100. return
  101. # encode the given string in the specified encoding
  102. s = text.encode(encoding)
  103. # when the resulting byte string is longer than the given limit ..
  104. if len(s) > limit:
  105. # .. truncate, and
  106. s = s[:limit]
  107. # decode back, ignoring errors that result from truncation
  108. # in the middle of multi-byte encodings
  109. text = s.decode(encoding, 'ignore')
  110. if return_encoded:
  111. s = text.encode(encoding)
  112. if return_encoded:
  113. return s
  114. else:
  115. return text
  116. @public
  117. def xor(d1, d2):
  118. """
  119. XOR two binary strings of arbitrary (equal) length.
  120. :param d1: The first binary string.
  121. :type d1: binary
  122. :param d2: The second binary string.
  123. :type d2: binary
  124. :returns: XOR of the binary strings (``XOR(d1, d2)``)
  125. :rtype: bytes
  126. """
  127. if type(d1) != six.binary_type:
  128. raise Exception("invalid type {} for d1 - must be binary".format(type(d1)))
  129. if type(d2) != six.binary_type:
  130. raise Exception("invalid type {} for d2 - must be binary".format(type(d2)))
  131. if len(d1) != len(d2):
  132. raise Exception("cannot XOR binary string of differing length ({} != {})".format(len(d1), len(d2)))
  133. d1 = array('B', d1)
  134. d2 = array('B', d2)
  135. for i in range(len(d1)):
  136. d1[i] ^= d2[i]
  137. if six.PY3:
  138. return d1.tobytes()
  139. else:
  140. return d1.tostring()
  141. @public
  142. def utcstr(ts=None):
  143. """
  144. Format UTC timestamp in ISO 8601 format.
  145. Note: to parse an ISO 8601 formatted string, use the **iso8601**
  146. module instead (e.g. ``iso8601.parse_date("2014-05-23T13:03:44.123Z")``).
  147. :param ts: The timestamp to format.
  148. :type ts: instance of :py:class:`datetime.datetime` or ``None``
  149. :returns: Timestamp formatted in ISO 8601 format.
  150. :rtype: str
  151. """
  152. assert(ts is None or isinstance(ts, datetime))
  153. if ts is None:
  154. ts = datetime.utcnow()
  155. return u"{0}Z".format(ts.strftime(u"%Y-%m-%dT%H:%M:%S.%f")[:-3])
  156. @public
  157. def utcnow():
  158. """
  159. Get current time in UTC as ISO 8601 string.
  160. :returns: Current time as string in ISO 8601 format.
  161. :rtype: str
  162. """
  163. return utcstr()
  164. class IdGenerator(object):
  165. """
  166. ID generator for WAMP request IDs.
  167. WAMP request IDs are sequential per WAMP session, starting at 1 and
  168. wrapping around at 2**53 (both value are inclusive [1, 2**53]).
  169. The upper bound **2**53** is chosen since it is the maximum integer that can be
  170. represented as a IEEE double such that all smaller integers are representable as well.
  171. Hence, IDs can be safely used with languages that use IEEE double as their
  172. main (or only) number type (JavaScript, Lua, etc).
  173. See https://github.com/wamp-proto/wamp-proto/blob/master/spec/basic.md#ids
  174. """
  175. def __init__(self):
  176. self._next = 0 # starts at 1; next() pre-increments
  177. def next(self):
  178. """
  179. Returns next ID.
  180. :returns: The next ID.
  181. :rtype: int
  182. """
  183. self._next += 1
  184. if self._next > 9007199254740992:
  185. self._next = 1
  186. return self._next
  187. # generator protocol
  188. def __next__(self):
  189. return self.next()
  190. #
  191. # Performance comparison of IdGenerator.next(), id() and rid().
  192. #
  193. # All tests were performed on:
  194. #
  195. # - Ubuntu 14.04 LTS x86-64
  196. # - Intel Core i7 920 @ 3.3GHz
  197. #
  198. # The tests generated 100 mio. IDs and run-time was measured
  199. # as wallclock from Unix "time" command. In each run, a single CPU
  200. # core was essentially at 100% load all the time (though the sys/usr
  201. # ratio was different).
  202. #
  203. # PyPy 2.6.1:
  204. #
  205. # IdGenerator.next() 0.5s
  206. # id() 29.4s
  207. # rid() 106.1s
  208. #
  209. # CPython 2.7.10:
  210. #
  211. # IdGenerator.next() 49.0s
  212. # id() 370.5s
  213. # rid() 196.4s
  214. #
  215. #
  216. # Note on the ID range [0, 2**53]. We once reduced the range to [0, 2**31].
  217. # This lead to extremely hard to track down issues due to ID collisions!
  218. # Here: https://github.com/crossbario/autobahn-python/issues/419#issue-90483337
  219. #
  220. # 8 byte mask with 53 LSBs set (WAMP requires IDs from [0, 2**53]
  221. _WAMP_ID_MASK = struct.unpack(">Q", b"\x00\x1f\xff\xff\xff\xff\xff\xff")[0]
  222. def rid():
  223. """
  224. Generate a new random integer ID from range **[0, 2**53]**.
  225. The generated ID is uniformly distributed over the whole range, doesn't have
  226. a period (no pseudo-random generator is used) and cryptographically strong.
  227. The upper bound **2**53** is chosen since it is the maximum integer that can be
  228. represented as a IEEE double such that all smaller integers are representable as well.
  229. Hence, IDs can be safely used with languages that use IEEE double as their
  230. main (or only) number type (JavaScript, Lua, etc).
  231. :returns: A random integer ID.
  232. :rtype: int
  233. """
  234. return struct.unpack("@Q", os.urandom(8))[0] & _WAMP_ID_MASK
  235. # noinspection PyShadowingBuiltins
  236. def id():
  237. """
  238. Generate a new random integer ID from range **[0, 2**53]**.
  239. The generated ID is based on a pseudo-random number generator (Mersenne Twister,
  240. which has a period of 2**19937-1). It is NOT cryptographically strong, and
  241. hence NOT suitable to generate e.g. secret keys or access tokens.
  242. The upper bound **2**53** is chosen since it is the maximum integer that can be
  243. represented as a IEEE double such that all smaller integers are representable as well.
  244. Hence, IDs can be safely used with languages that use IEEE double as their
  245. main (or only) number type (JavaScript, Lua, etc).
  246. :returns: A random integer ID.
  247. :rtype: int
  248. """
  249. return random.randint(0, 9007199254740992)
  250. def newid(length=16):
  251. """
  252. Generate a new random string ID.
  253. The generated ID is uniformly distributed and cryptographically strong. It is
  254. hence usable for things like secret keys and access tokens.
  255. :param length: The length (in chars) of the ID to generate.
  256. :type length: int
  257. :returns: A random string ID.
  258. :rtype: str
  259. """
  260. l = int(math.ceil(float(length) * 6. / 8.))
  261. return base64.b64encode(os.urandom(l))[:length].decode('ascii')
  262. # a standard base36 character set
  263. # DEFAULT_TOKEN_CHARS = string.digits + string.ascii_uppercase
  264. # we take out the following 9 chars (leaving 27), because there
  265. # is visual ambiguity: 0/O/D, 1/I, 8/B, 2/Z
  266. DEFAULT_TOKEN_CHARS = u'345679ACEFGHJKLMNPQRSTUVWXY'
  267. """
  268. Default set of characters to create rtokens from.
  269. """
  270. DEFAULT_ZBASE32_CHARS = u'13456789abcdefghijkmnopqrstuwxyz'
  271. """
  272. Our choice of confusing characters to eliminate is: `0', `l', `v', and `2'. Our
  273. reasoning is that `0' is potentially mistaken for `o', that `l' is potentially
  274. mistaken for `1' or `i', that `v' is potentially mistaken for `u' or `r'
  275. (especially in handwriting) and that `2' is potentially mistaken for `z'
  276. (especially in handwriting).
  277. Note that we choose to focus on typed and written transcription more than on
  278. vocal, since humans already have a well-established system of disambiguating
  279. spoken alphanumerics, such as the United States military's "Alpha Bravo Charlie
  280. Delta" and telephone operators' "Is that 'd' as in 'dog'?".
  281. * http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
  282. """
  283. @public
  284. def generate_token(char_groups, chars_per_group, chars=None, sep=None, lower_case=False):
  285. """
  286. Generate cryptographically strong tokens, which are strings like `M6X5-YO5W-T5IK`.
  287. These can be used e.g. for used-only-once activation tokens or the like.
  288. The returned token has an entropy of
  289. ``math.log(len(chars), 2.) * chars_per_group * char_groups``
  290. bits.
  291. With the default charset and 4 characters per group, ``generate_token()`` produces
  292. strings with the following entropy:
  293. ================ =================== ========================================
  294. character groups entropy (at least) recommended use
  295. ================ =================== ========================================
  296. 2 38 bits
  297. 3 57 bits one-time activation or pairing code
  298. 4 76 bits secure user password
  299. 5 95 bits
  300. 6 114 bits globally unique serial / product code
  301. 7 133 bits
  302. ================ =================== ========================================
  303. Here are some examples:
  304. * token(3): ``9QXT-UXJW-7R4H``
  305. * token(4): ``LPNN-JMET-KWEP-YK45``
  306. * token(6): ``NXW9-74LU-6NUH-VLPV-X6AG-QUE3``
  307. :param char_groups: Number of character groups (or characters if chars_per_group == 1).
  308. :type char_groups: int
  309. :param chars_per_group: Number of characters per character group (or 1 to return a token with no grouping).
  310. :type chars_per_group: int
  311. :param chars: Characters to choose from. Default is 27 character subset
  312. of the ISO basic Latin alphabet (see: ``DEFAULT_TOKEN_CHARS``).
  313. :type chars: str or None
  314. :param sep: When separating groups in the token, the separater string.
  315. :type sep: str
  316. :param lower_case: If ``True``, generate token in lower-case.
  317. :type lower_case: bool
  318. :returns: The generated token.
  319. :rtype: str
  320. """
  321. assert(type(char_groups) in six.integer_types)
  322. assert(type(chars_per_group) in six.integer_types)
  323. assert(chars is None or type(chars) == six.text_type)
  324. chars = chars or DEFAULT_TOKEN_CHARS
  325. if lower_case:
  326. chars = chars.lower()
  327. sep = sep or u'-'
  328. rng = random.SystemRandom()
  329. token_value = u''.join(rng.choice(chars) for _ in range(char_groups * chars_per_group))
  330. if chars_per_group > 1:
  331. return sep.join(map(u''.join, zip(*[iter(token_value)] * chars_per_group)))
  332. else:
  333. return token_value
  334. @public
  335. def generate_activation_code():
  336. """
  337. Generate a one-time activation code or token of the form ``u'W97F-96MJ-YGJL'``.
  338. The generated value is cryptographically strong and has (at least) 57 bits of entropy.
  339. :returns: The generated activation code.
  340. :rtype: str
  341. """
  342. return generate_token(char_groups=3, chars_per_group=4, chars=DEFAULT_TOKEN_CHARS, sep=u'-', lower_case=False)
  343. @public
  344. def generate_user_password():
  345. """
  346. Generate a secure, random user password of the form ``u'kgojzi61dn5dtb6d'``.
  347. The generated value is cryptographically strong and has (at least) 76 bits of entropy.
  348. :returns: The generated password.
  349. :rtype: str
  350. """
  351. return generate_token(char_groups=16, chars_per_group=1, chars=DEFAULT_ZBASE32_CHARS, sep=u'-', lower_case=True)
  352. @public
  353. def generate_serial_number():
  354. """
  355. Generate a globally unique serial / product code of the form ``u'YRAC-EL4X-FQQE-AW4T-WNUV-VN6T'``.
  356. The generated value is cryptographically strong and has (at least) 114 bits of entropy.
  357. :returns: The generated serial number / product code.
  358. :rtype: str
  359. """
  360. return generate_token(char_groups=6, chars_per_group=4, chars=DEFAULT_TOKEN_CHARS, sep=u'-', lower_case=False)
  361. # Select the most precise walltime measurement function available
  362. # on the platform
  363. #
  364. if sys.platform.startswith('win'):
  365. # On Windows, this function returns wall-clock seconds elapsed since the
  366. # first call to this function, as a floating point number, based on the
  367. # Win32 function QueryPerformanceCounter(). The resolution is typically
  368. # better than one microsecond
  369. if sys.version_info >= (3, 8):
  370. _rtime = time.perf_counter
  371. else:
  372. _rtime = time.clock
  373. _ = _rtime() # this starts wallclock
  374. else:
  375. # On Unix-like platforms, this used the first available from this list:
  376. # (1) gettimeofday() -- resolution in microseconds
  377. # (2) ftime() -- resolution in milliseconds
  378. # (3) time() -- resolution in seconds
  379. _rtime = time.time
  380. @public
  381. def rtime():
  382. """
  383. Precise, fast wallclock time.
  384. :returns: The current wallclock in seconds. Returned values are only guaranteed
  385. to be meaningful relative to each other.
  386. :rtype: float
  387. """
  388. return _rtime()
  389. class Stopwatch(object):
  390. """
  391. Stopwatch based on walltime.
  392. This can be used to do code timing and uses the most precise walltime measurement
  393. available on the platform. This is a very light-weight object,
  394. so create/dispose is very cheap.
  395. """
  396. def __init__(self, start=True):
  397. """
  398. :param start: If ``True``, immediately start the stopwatch.
  399. :type start: bool
  400. """
  401. self._elapsed = 0
  402. if start:
  403. self._started = rtime()
  404. self._running = True
  405. else:
  406. self._started = None
  407. self._running = False
  408. def elapsed(self):
  409. """
  410. Return total time elapsed in seconds during which the stopwatch was running.
  411. :returns: The elapsed time in seconds.
  412. :rtype: float
  413. """
  414. if self._running:
  415. now = rtime()
  416. return self._elapsed + (now - self._started)
  417. else:
  418. return self._elapsed
  419. def pause(self):
  420. """
  421. Pauses the stopwatch and returns total time elapsed in seconds during which
  422. the stopwatch was running.
  423. :returns: The elapsed time in seconds.
  424. :rtype: float
  425. """
  426. if self._running:
  427. now = rtime()
  428. self._elapsed += now - self._started
  429. self._running = False
  430. return self._elapsed
  431. else:
  432. return self._elapsed
  433. def resume(self):
  434. """
  435. Resumes a paused stopwatch and returns total elapsed time in seconds
  436. during which the stopwatch was running.
  437. :returns: The elapsed time in seconds.
  438. :rtype: float
  439. """
  440. if not self._running:
  441. self._started = rtime()
  442. self._running = True
  443. return self._elapsed
  444. else:
  445. now = rtime()
  446. return self._elapsed + (now - self._started)
  447. def stop(self):
  448. """
  449. Stops the stopwatch and returns total time elapsed in seconds during which
  450. the stopwatch was (previously) running.
  451. :returns: The elapsed time in seconds.
  452. :rtype: float
  453. """
  454. elapsed = self.pause()
  455. self._elapsed = 0
  456. self._started = None
  457. self._running = False
  458. return elapsed
  459. class Tracker(object):
  460. """
  461. A key-based statistics tracker.
  462. """
  463. def __init__(self, tracker, tracked):
  464. """
  465. """
  466. self.tracker = tracker
  467. self.tracked = tracked
  468. self._timings = {}
  469. self._offset = rtime()
  470. self._dt_offset = datetime.utcnow()
  471. def track(self, key):
  472. """
  473. Track elapsed for key.
  474. :param key: Key under which to track the timing.
  475. :type key: str
  476. """
  477. self._timings[key] = rtime()
  478. def diff(self, start_key, end_key, formatted=True):
  479. """
  480. Get elapsed difference between two previously tracked keys.
  481. :param start_key: First key for interval (older timestamp).
  482. :type start_key: str
  483. :param end_key: Second key for interval (younger timestamp).
  484. :type end_key: str
  485. :param formatted: If ``True``, format computed time period and return string.
  486. :type formatted: bool
  487. :returns: Computed time period in seconds (or formatted string).
  488. :rtype: float or str
  489. """
  490. if end_key in self._timings and start_key in self._timings:
  491. d = self._timings[end_key] - self._timings[start_key]
  492. if formatted:
  493. if d < 0.00001: # 10us
  494. s = "%d ns" % round(d * 1000000000.)
  495. elif d < 0.01: # 10ms
  496. s = "%d us" % round(d * 1000000.)
  497. elif d < 10: # 10s
  498. s = "%d ms" % round(d * 1000.)
  499. else:
  500. s = "%d s" % round(d)
  501. return s.rjust(8)
  502. else:
  503. return d
  504. else:
  505. if formatted:
  506. return "n.a.".rjust(8)
  507. else:
  508. return None
  509. def absolute(self, key):
  510. """
  511. Return the UTC wall-clock time at which a tracked event occurred.
  512. :param key: The key
  513. :type key: str
  514. :returns: Timezone-naive datetime.
  515. :rtype: instance of :py:class:`datetime.datetime`
  516. """
  517. elapsed = self[key]
  518. if elapsed is None:
  519. raise KeyError("No such key \"%s\"." % elapsed)
  520. return self._dt_offset + timedelta(seconds=elapsed)
  521. def __getitem__(self, key):
  522. if key in self._timings:
  523. return self._timings[key] - self._offset
  524. else:
  525. return None
  526. def __iter__(self):
  527. return self._timings.__iter__()
  528. def __str__(self):
  529. return pformat(self._timings)
  530. class EqualityMixin(object):
  531. """
  532. Mixing to add equality comparison operators to a class.
  533. Two objects are identical under this mixin, if and only if:
  534. 1. both object have the same class
  535. 2. all non-private object attributes are equal
  536. """
  537. def __eq__(self, other):
  538. """
  539. Compare this object to another object for equality.
  540. :param other: The other object to compare with.
  541. :type other: obj
  542. :returns: ``True`` iff the objects are equal.
  543. :rtype: bool
  544. """
  545. if not isinstance(other, self.__class__):
  546. return False
  547. # we only want the actual message data attributes (not eg _serialize)
  548. for k in self.__dict__:
  549. if not k.startswith('_'):
  550. if not self.__dict__[k] == other.__dict__[k]:
  551. return False
  552. return True
  553. # return (isinstance(other, self.__class__) and self.__dict__ == other.__dict__)
  554. def __ne__(self, other):
  555. """
  556. Compare this object to another object for inequality.
  557. :param other: The other object to compare with.
  558. :type other: obj
  559. :returns: ``True`` iff the objects are not equal.
  560. :rtype: bool
  561. """
  562. return not self.__eq__(other)
  563. def wildcards2patterns(wildcards):
  564. """
  565. Compute a list of regular expression patterns from a list of
  566. wildcard strings. A wildcard string uses '*' as a wildcard character
  567. matching anything.
  568. :param wildcards: List of wildcard strings to compute regular expression patterns for.
  569. :type wildcards: list of str
  570. :returns: Computed regular expressions.
  571. :rtype: list of obj
  572. """
  573. # note that we add the ^ and $ so that the *entire* string must
  574. # match. Without this, e.g. a prefix will match:
  575. # re.match('.*good\\.com', 'good.com.evil.com') # match!
  576. # re.match('.*good\\.com$', 'good.com.evil.com') # no match!
  577. return [re.compile('^' + wc.replace('.', r'\.').replace('*', '.*') + '$') for wc in wildcards]
  578. class ObservableMixin(object):
  579. """
  580. Internal utility for enabling event-listeners on particular objects
  581. """
  582. # A "helper" style composable class (as opposed to a mix-in) might
  583. # be a lot easier to deal with here. Having an __init__ method
  584. # with a "mix in" style class can be fragile and error-prone,
  585. # especially if it takes arguments. Since we don't use the
  586. # "parent" beavior anywhere, I didn't add a .set_parent() (yet?)
  587. # these are class-level globals; individual instances are
  588. # initialized as-needed (e.g. the first .on() call adds a
  589. # _listeners dict). Thus, subclasses don't have to call super()
  590. # properly etc.
  591. _parent = None
  592. _valid_events = None
  593. _listeners = None
  594. def set_valid_events(self, valid_events=None):
  595. """
  596. :param valid_events: if non-None, .on() or .fire() with an event
  597. not listed in valid_events raises an exception.
  598. """
  599. self._valid_events = list(valid_events)
  600. def _check_event(self, event):
  601. """
  602. Internal helper. Throws RuntimeError if we have a valid_events
  603. list, and the given event isnt' in it. Does nothing otherwise.
  604. """
  605. if self._valid_events and event not in self._valid_events:
  606. raise RuntimeError(
  607. "Invalid event '{event}'. Expected one of: {events}".format(
  608. event=event,
  609. events=', '.join(self._valid_events),
  610. )
  611. )
  612. def on(self, event, handler):
  613. """
  614. Add a handler for an event.
  615. :param event: the name of the event
  616. :param handler: a callable thats invoked when .fire() is
  617. called for this events. Arguments will be whatever are given
  618. to .fire()
  619. """
  620. # print("adding '{}' to '{}': {}".format(event, hash(self), handler))
  621. self._check_event(event)
  622. if self._listeners is None:
  623. self._listeners = dict()
  624. if event not in self._listeners:
  625. self._listeners[event] = []
  626. self._listeners[event].append(handler)
  627. def off(self, event=None, handler=None):
  628. """
  629. Stop listening for a single event, or all events.
  630. :param event: if None, remove all listeners. Otherwise, remove
  631. listeners for the single named event.
  632. :param handler: if None, remove all handlers for the named
  633. event; otherwise remove just the given handler.
  634. """
  635. if event is None:
  636. if handler is not None:
  637. # maybe this should mean "remove the given handler
  638. # from any event at all that contains it"...?
  639. raise RuntimeError(
  640. "Can't specificy a specific handler without an event"
  641. )
  642. self._listeners = dict()
  643. else:
  644. if self._listeners is None:
  645. return
  646. self._check_event(event)
  647. if event in self._listeners:
  648. if handler is None:
  649. del self._listeners[event]
  650. else:
  651. self._listeners[event].discard(handler)
  652. def fire(self, event, *args, **kwargs):
  653. """
  654. Fire a particular event.
  655. :param event: the event to fire. All other args and kwargs are
  656. passed on to the handler(s) for the event.
  657. :return: a Deferred/Future gathering all async results from
  658. all handlers and/or parent handlers.
  659. """
  660. # print("firing '{}' from '{}'".format(event, hash(self)))
  661. if self._listeners is None:
  662. return txaio.create_future(result=[])
  663. self._check_event(event)
  664. res = []
  665. for handler in self._listeners.get(event, []):
  666. future = txaio.as_future(handler, *args, **kwargs)
  667. res.append(future)
  668. if self._parent is not None:
  669. res.append(self._parent.fire(event, *args, **kwargs))
  670. return txaio.gather(res, consume_exceptions=False)
  671. class _LazyHexFormatter(object):
  672. """
  673. This is used to avoid calling binascii.hexlify() on data given to
  674. log.debug() calls unless debug is active (for example). Like::
  675. self.log.debug(
  676. "Some data: {octets}",
  677. octets=_LazyHexFormatter(os.urandom(32)),
  678. )
  679. """
  680. __slots__ = ('obj',)
  681. def __init__(self, obj):
  682. self.obj = obj
  683. def __str__(self):
  684. return binascii.hexlify(self.obj).decode('ascii')
  685. def _is_tls_error(instance):
  686. """
  687. :returns: True if we have TLS support and 'instance' is an
  688. instance of :class:`OpenSSL.SSL.Error` otherwise False
  689. """
  690. if _TLS:
  691. return isinstance(instance, SSL.Error)
  692. return False
  693. def _maybe_tls_reason(instance):
  694. """
  695. :returns: a TLS error-message, or empty-string if 'instance' is
  696. not a TLS error.
  697. """
  698. if _is_tls_error(instance):
  699. ssl_error = instance.args[0][0]
  700. return u"SSL error: {msg} (in {func})".format(
  701. func=ssl_error[1],
  702. msg=ssl_error[2],
  703. )
  704. return u""