make_catsoop.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import re
  5. import string
  6. import sys
  7. def exclude(filename):
  8. return filename.endswith('~') or filename.endswith('.swp') or re.match('^#.*#$', filename)
  9. def error(msg):
  10. print('Error: {}'.format(msg))
  11. exit(1)
  12. def mkdir(d):
  13. if not os.path.isdir(d):
  14. try:
  15. os.makedirs(d, exist_ok=True)
  16. except OSError:
  17. error('{} already exists and is not a directory'.format(d))
  18. def build_templates(src, dest, context):
  19. mkdir(dest)
  20. for parent_dir, child_dirs, templates in os.walk(src):
  21. for template in templates:
  22. if exclude(template): continue
  23. template_src = os.path.join(parent_dir, template)
  24. with open(template_src) as f:
  25. template_content = string.Template(f.read())
  26. template_dest = os.path.join(dest, os.path.relpath(template_src, src))
  27. mkdir(os.path.dirname(template_dest))
  28. with open(template_dest, 'w') as f:
  29. f.write(template_content.substitute(**context))
  30. params, destination = sys.argv[1:]
  31. params = json.loads(params)
  32. room_destination = os.path.join(destination, 'pages')
  33. plugin_destination = os.path.join(destination, 'plugin')
  34. # Make plugin
  35. build_templates(params['CATSOOP']['PLUGIN_TEMPLATE'], plugin_destination, {
  36. 'queue_room': repr(params['ROOMS'][0] if len(params['ROOMS']) == 1 else None),
  37. 'queue_url_root': repr(params['URL_ROOT']),
  38. })
  39. if not params['ROOMS']:
  40. error('No room names specified')
  41. # In the case of only one room, there's no need for a root page, so this just
  42. # makes the one room page and exits
  43. if len(params['ROOMS']) == 1:
  44. build_templates(params['CATSOOP']['ROOM_TEMPLATE'], room_destination, {
  45. 'queue_room_name': params['ROOMS'][0],
  46. 'queue_plugin_name': params['CATSOOP']['PLUGIN_NAME'],
  47. })
  48. exit(0)
  49. # Make the root template
  50. build_templates(params['CATSOOP']['ROOM_SELECTION_TEMPLATE'], room_destination, {
  51. 'rooms': repr(params['ROOMS']),
  52. })
  53. # Make a room page for each room
  54. for room in params['ROOMS']:
  55. build_templates(params['CATSOOP']['ROOM_TEMPLATE'], os.path.join(room_destination, room), {
  56. 'queue_room_name': room,
  57. 'queue_plugin_name': params['CATSOOP']['PLUGIN_NAME'],
  58. })