您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

34 行
1.2 KiB

  1. import logging
  2. logger = logging.getLogger(__name__)
  3. def get_sources(path, delimiter='\n'):
  4. """Load source URLs from a file, one per line.
  5. Blank lines and lines starting with ``#`` are ignored, so the file can be
  6. annotated with comments (e.g. grouping HTML vs. RSS feeds).
  7. """
  8. logger.info("Attempting to load sources from file: '%s' with delimiter: %r", path, delimiter)
  9. try:
  10. with open(path, 'r', encoding='utf-8-sig') as source_file:
  11. content = source_file.read()
  12. except FileNotFoundError:
  13. logger.error("Source file not found at path: '%s'", path, exc_info=True)
  14. raise
  15. except PermissionError:
  16. logger.error("Permission denied when accessing source file: '%s'", path, exc_info=True)
  17. raise
  18. except Exception as e:
  19. logger.error("Failed to read sources from '%s': %s", path, e, exc_info=True)
  20. raise
  21. sources = []
  22. for line in content.split(delimiter):
  23. stripped = line.strip()
  24. if not stripped or stripped.startswith('#'):
  25. continue
  26. sources.append(stripped)
  27. logger.info("Successfully loaded %d source entries from '%s'", len(sources), path)
  28. return sources