utils.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from redis import Redis
  4. from rq import Queue
  5. from tracim.config import CFG
  6. DEFAULT_WEBDAV_CONFIG_FILE = "wsgidav.conf"
  7. DEFAULT_TRACIM_CONFIG_FILE = "development.ini"
  8. def get_redis_connection(config: CFG) -> Redis:
  9. """
  10. :param config: current app_config
  11. :return: redis connection
  12. """
  13. return Redis(
  14. host=config.EMAIL_SENDER_REDIS_HOST,
  15. port=config.EMAIL_SENDER_REDIS_PORT,
  16. db=config.EMAIL_SENDER_REDIS_DB,
  17. )
  18. def get_rq_queue(redis_connection: Redis, queue_name: str ='default') -> Queue:
  19. """
  20. :param queue_name: name of queue
  21. :return: wanted queue
  22. """
  23. return Queue(name=queue_name, connection=redis_connection)
  24. def cmp_to_key(mycmp):
  25. """
  26. List sort related function
  27. Convert a cmp= function into a key= function
  28. """
  29. class K(object):
  30. def __init__(self, obj, *args):
  31. self.obj = obj
  32. def __lt__(self, other):
  33. return mycmp(self.obj, other.obj) < 0
  34. def __gt__(self, other):
  35. return mycmp(self.obj, other.obj) > 0
  36. def __eq__(self, other):
  37. return mycmp(self.obj, other.obj) == 0
  38. def __le__(self, other):
  39. return mycmp(self.obj, other.obj) <= 0
  40. def __ge__(self, other):
  41. return mycmp(self.obj, other.obj) >= 0
  42. def __ne__(self, other):
  43. return mycmp(self.obj, other.obj) != 0
  44. return K
  45. def current_date_for_filename() -> str:
  46. """
  47. ISO8601 current date, adapted to be used in filename (for
  48. webdav feature for example), with trouble-free characters.
  49. :return: current date as string like "2018-03-19T15.49.27.246592"
  50. """
  51. # INFO - G.M - 19-03-2018 - As ':' is in transform_to_bdd method in
  52. # webdav utils, it may cause trouble. So, it should be replaced to
  53. # a character which will not change in bdd.
  54. return datetime.datetime.now().isoformat().replace(':', '.')