util.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. dt = datetime.strptime(month_year,"%m%y")
  14. dt = dt.replace(tzinfo=UTC)
  15. unix = int(dt.timestamp())
  16. except ValueError:
  17. print("Error parsing date!")
  18. exit(-1)
  19. return unix
  20. def unix_to_datetime(timestamp):
  21. return datetime.fromtimestamp(int(timestamp), UTC)
  22. task_template = {
  23. "workspace": str,
  24. "title": str,
  25. "tags": list,
  26. "desc": str,
  27. "owner": str,
  28. "assign": list,
  29. "project": list,
  30. "due": int,
  31. "rank": float,
  32. "created_at": int,
  33. "state": str,
  34. "events": list,
  35. "comments": list,
  36. }
  37. def _enforce_task_format(task):
  38. for attr, val in task.items():
  39. val_type = task_template[attr]
  40. if val is None:
  41. assert val_type == list or attr not in ["created"]
  42. continue
  43. assert isinstance(val, val_type)