Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

31 rinda
1.3 KiB

  1. import logging
  2. logger = logging.getLogger(__name__)
  3. def get_sources(path, delimiter='\n'):
  4. logger.info("Attempting to load sources from file: '%s' with delimiter: %r", path, delimiter)
  5. sources = None
  6. try:
  7. with open(path, 'r', encoding='utf-8') as source_file:
  8. logger.debug("Successfully opened file '%s' for reading", path)
  9. content = source_file.read()
  10. logger.debug("Read %d bytes/characters from '%s'", len(content), path)
  11. sources = content.split(delimiter)
  12. logger.info("Successfully read and split %d source entries from '%s'", len(sources), path)
  13. for idx, src in enumerate(sources):
  14. if not src.strip():
  15. logger.warning("Source at index %d is empty or whitespace-only: %r", idx, src)
  16. else:
  17. logger.debug("Source [%d]: %s", idx, src)
  18. except FileNotFoundError:
  19. logger.error("Source file not found at path: '%s'", path, exc_info=True)
  20. raise
  21. except PermissionError:
  22. logger.error("Permission denied when accessing source file: '%s'", path, exc_info=True)
  23. raise
  24. except Exception as e:
  25. logger.error("Failed to read sources from '%s': %s", path, e, exc_info=True)
  26. raise
  27. return sources