Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Changed return types and values for `fetch_transcripts`, `fetch_snippets` and `fetch_comments` to improve type hints.
- Changed CLI comment flags: `--comments` and `--comments-only` now select the fetch mode, while `--max-comments` controls the number of comments per video.
- Exporters, `PreviewRenderer`, and `channel_data_to_rows()` now accept any supported fetch result shape and normalize it internally.
- `BaseExporter` now creates directory for exporter path instead of raising.

### Fixed
- Fixed ytfetcher raises an error if output directory could not found in `Exporter` class.

## [2.3.2]
### Fixed
Expand Down
8 changes: 2 additions & 6 deletions tests/exports/test_exporter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ytfetcher.exceptions import NoDataToExport, OutputDirectoryNotFoundError
from ytfetcher.exceptions import NoDataToExport
from ytfetcher.services.exports import TXTExporter
from ytfetcher.models.channel import ChannelData, DLSnippet
import pytest
Expand Down Expand Up @@ -27,8 +27,4 @@ def mock_transcript_response(sample_snippet):

def test_export_with_txt_no_channel_data_exception():
with pytest.raises(NoDataToExport):
TXTExporter([])

def test_export_with_txt_wrong_output_dir_exception(mock_transcript_response):
with pytest.raises(OutputDirectoryNotFoundError):
TXTExporter(mock_transcript_response, output_dir='dwadwadwadwa')
TXTExporter([])
4 changes: 2 additions & 2 deletions ytfetcher/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class ExporterError(Exception):
Base exception for all Exporter errors.
"""

class OutputDirectoryNotFoundError(ExporterError):
class OutputDirectoryCannotBeCreated(ExporterError):
"""
Raised when the specified output directory does not exist.
Raised when the specified output directory cannot be created.
"""
Comment on lines +11 to 14

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. Exception rename breaks imports 🐞 Bug ≡ Correctness

The PR removes/renames the public exception OutputDirectoryNotFoundError to
OutputDirectoryCannotBeCreated without a compatibility alias, but existing code still imports the
old name (tests currently do), causing ImportError and a failing test suite. This is a breaking
API change for any downstream callers that catch/import the old exception name.
Agent Prompt
## Issue description
`OutputDirectoryNotFoundError` was renamed/removed, but internal tests (and potentially external users) still import it, causing `ImportError` and breaking callers.

## Issue Context
Exporter behavior changed to create directories, so tests expecting an exception for a non-existent relative directory should be updated to assert directory creation; a separate test should cover the failure case (e.g., output_dir points to an existing file or a non-writable location).

## Fix Focus Areas
- ytfetcher/exceptions.py[6-16]
- tests/exports/test_exporter.py[1-40]
- ytfetcher/services/exports.py[1-60]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


class NoDataToExport(ExporterError):
Expand Down
11 changes: 4 additions & 7 deletions ytfetcher/services/exports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from pathlib import Path
from ytfetcher.models.channel import ChannelData
from ytfetcher.exceptions import NoDataToExport, OutputDirectoryNotFoundError
from ytfetcher.exceptions import NoDataToExport, OutputDirectoryCannotBeCreated
from typing import Literal, Sequence, get_args, Any
import json
import csv
Expand Down Expand Up @@ -30,7 +30,7 @@ class BaseExporter(ABC):

Raises:
NoDataToExport: If no data is provided.
OutputDirectoryNotFoundError: If specified path cannot found.
OutputDirectoryCannotBeCreated: If specified path cannot be created.
"""
def __init__(self, channel_data: FetchResult, allowed_metadata_list: Sequence[METADATA_LIST] = DEFAULT_METADATA, timing: bool = True, filename: str = 'data', output_dir: str | None = None):
self.channel_data: list[ChannelData] = normalize_for_export(channel_data)
Expand All @@ -41,9 +41,6 @@ def __init__(self, channel_data: FetchResult, allowed_metadata_list: Sequence[ME

if not self.channel_data:
raise NoDataToExport("No data to export.")

if not self.output_dir.exists():
raise OutputDirectoryNotFoundError("System path could not found.")

@abstractmethod
def write(self) -> None:
Expand All @@ -56,9 +53,9 @@ def _initialize_output_path(self, export_type: Literal['txt', 'json', 'csv'] = '

logger.debug(f"Writing as {export_type} file, output path: {output_path}")
return output_path
except OSError:
except OSError as e:
logger.exception("Failed to initialize output directory %s", self.output_dir)
raise
raise OutputDirectoryCannotBeCreated(f"Output directory {self.output_dir} could not be created") from e

def _get_clean_metadata(self, data: ChannelData):
"""
Expand Down
Loading