common.py 960 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import os
  2. def exit_immediately(s):
  3. print("\nERROR\n", s)
  4. exit(1)
  5. # Safely open dir/filename, creating dir if it doesn't exist
  6. def open_outfile(dir, filename, mode):
  7. if not dir.exists():
  8. os.makedirs(str(dir))
  9. return (dir / filename).open(mode, encoding="utf-8")
  10. def stream_validator(settings):
  11. if not hasattr(settings, "included_streams"):
  12. exit_immediately("Please set included_streams.")
  13. if len(settings.included_streams) == 0:
  14. exit_immediately('Please add "*" to included_streams.')
  15. if hasattr(settings, "excluded_streams"):
  16. excluded_streams = set(settings.excluded_streams)
  17. else:
  18. excluded_streams = set()
  19. included_streams = set(settings.included_streams)
  20. def validator(stream):
  21. if stream in excluded_streams:
  22. return False
  23. if "*" in included_streams:
  24. return True
  25. return stream in included_streams
  26. return validator