misc.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import os
  2. import datetime
  3. from showroom.constants import HHMM_FMT, FULL_DATE_FMT
  4. CENTURY_OFFSET = 2000
  5. # TODO: take a datetime object instead of a time_str
  6. def format_name(root_dir, time_str, room, ext):
  7. """
  8. Get file and folder names for a live stream.
  9. Takes a root directory, a time string, and a room and returns a temp directory,
  10. a destination directory, and a file name for the associated stream.
  11. Args:
  12. root_dir: path the top of the output directory, generally taken from settings.
  13. Must be a string, Path objects are not handled.
  14. time_str: date and time in YYYY-MM-DD HHmmss format
  15. room: A Room object containing information about the room
  16. Returns:
  17. A tuple of three strings, representing the temp directory, the destination
  18. directory, and the filename, as follows:
  19. (temp ("active") directory, destination directory, filename)
  20. TODO:
  21. Eliminate double spaces more cleanly.
  22. Is there ever a situation where outfile could already exist?
  23. """
  24. rootdir = root_dir
  25. dir_format = '{root}/{date}/{group}'
  26. tempdir = '{root}/active'.format(root=rootdir)
  27. name_format = '{date} Showroom - {handle} {time}{count}.{ext}'
  28. # count = 0
  29. # count_str = '_{:02d}'
  30. destdir = dir_format.format(root=rootdir, date=time_str[:10], group=room.group)
  31. os.makedirs('{}/logs'.format(destdir), exist_ok=True)
  32. _date, _time = time_str.split(' ')
  33. short_date = _date[2:].replace('-', '')
  34. outfile = name_format.format(date=short_date, handle=room.handle,
  35. time=_time.replace(':', ''), count='', ext=ext)
  36. return tempdir, destdir, outfile
  37. def iso_date_to_six_char(date):
  38. y, m, d = date.split('-')
  39. return '{:02d}{}{}'.format(int(y)-CENTURY_OFFSET, m, d)
  40. def strftime(dt: datetime.datetime, format_str: str):
  41. """
  42. Custom strftime.
  43. Checks for most frequently used format strings and handles those manually,
  44. hands any others off to dt.strftime()
  45. Args:
  46. dt: A datetime object.
  47. format_str: A valid strftime format string.
  48. '%H:%M' and '%Y-%m-%d %H%M%S' are handled specially.
  49. Returns:
  50. A string representation of dt as described by format_str
  51. TODO:
  52. Test if this is any faster than calling dt.strftime.
  53. Since dt.strftime is a C function, it's PROBABLY NOT.
  54. Also check that dt is a valid datetime object?
  55. """
  56. if format_str == HHMM_FMT:
  57. return "{:02d}:{:02d}".format(dt.hour, dt.minute)
  58. elif format_str == FULL_DATE_FMT:
  59. return "{year:04d}-{mon:02d}-{day:02d} " \
  60. "{hour:02d}:{min:02d}:{sec:02d}".format(
  61. year=dt.year, mon=dt.month, day=dt.day,
  62. hour=dt.hour, min=dt.minute, sec=dt.second)
  63. else:
  64. return dt.strftime(format_str)