util.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import random, time
  2. from datetime import timezone, datetime
  3. UTC = timezone.utc
  4. def random_blob_idx():
  5. return "%030x" % random.randrange(16**30)
  6. def datetime_to_unix(dt):
  7. return int(time.mktime(dt.timetuple()))
  8. def now():
  9. return datetime_to_unix(datetime.now(tz=UTC))
  10. def month_to_unix(month=None):
  11. month_year = month if month is not None else datetime.now(UTC).strftime("%m%y")
  12. try:
  13. unix = int(datetime.strptime(month_year,"%m%y").timestamp())
  14. except ValueError:
  15. print("Error parsing date!")
  16. exit(-1)
  17. return unix
  18. def unix_to_datetime(timestamp):
  19. return datetime.fromtimestamp(int(timestamp), UTC)
  20. task_template = {
  21. "workspace": str,
  22. "title": str,
  23. "tags": list,
  24. "desc": str,
  25. "owner": str,
  26. "assign": list,
  27. "project": list,
  28. "due": int,
  29. "rank": float,
  30. "created_at": int,
  31. "state": str,
  32. "events": list,
  33. "comments": list,
  34. }
  35. def _enforce_task_format(task):
  36. for attr, val in task.items():
  37. val_type = task_template[attr]
  38. if val is None:
  39. assert val_type == list or attr not in ["created"]
  40. continue
  41. assert isinstance(val, val_type)