Skip to content

Commit

Permalink
Merge pull request #56 from dplocki/55-file-config-as-parameter
Browse files Browse the repository at this point in the history
55 file config as parameter
  • Loading branch information
dplocki authored Mar 18, 2024
2 parents 29a10e5 + 8e43a60 commit c0419b3
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 53 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The configuration file is placed in home directory.

The name: `.podcast_downloader_config.json`. The file is format in [JSON](https://en.wikipedia.org/wiki/JSON). The expected encoding is [utf-8](https://en.wikipedia.org/wiki/UTF-8).

The configuration file placement can be specified by [script argument](#script-arguments).

### An example of configuration file

```json
Expand Down Expand Up @@ -145,10 +147,11 @@ Example:

The script accept following command line arguments:

| Short version | Long name | Parameter | Default | Note |
|:--------------|:-----------------------|:-------------------:|:---------------:|:-----|
| | `--downloads_limit` | number | infinity | The maximum number of downloaded mp3 files |
| | `--if_directory_empty` | string | `download_last` | The general approach on empty directory |
| Short version | Long name | Parameter | Default | Note |
|:--------------|:-----------------------|:-------------------:|:-----------------------------------:|:-----|
| | `--config` | string | `~/.podcast_downloader_config.json` | The placement of the configuration file |
| | `--downloads_limit` | number | infinity | The maximum number of downloaded mp3 files |
| | `--if_directory_empty` | string | `download_last` | The general approach on empty directory |

## Adding date to file name

Expand Down
43 changes: 23 additions & 20 deletions e2e/fixures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from typing import Dict, Generator, Iterable, List, Set


DEFAULT_CONFIG_NAME = "config.json"


def print_set_content(content: Set):
return ", ".join(sorted(content))

Expand Down Expand Up @@ -190,15 +193,27 @@ def __init__(self, httpserver) -> None:


class PodcastDownloaderRunner:
def run(self):
def __init__(self, script_directory: Path) -> None:
self.script_directory = script_directory

def run(self, additional_parameters: Iterable[str] = None):
args = [sys.executable, "-m", "podcast_downloader"]

if additional_parameters:
args += additional_parameters
else:
args += ["--config", str(self.script_directory / DEFAULT_CONFIG_NAME)]

self.output = subprocess.run(
[sys.executable, "-m", "podcast_downloader"],
args,
check=True,
capture_output=True,
text=True,
)
self.output.check_returncode()

return self

def is_correct(self):
return (self.output.returncode == 0) and self.output.stderr == ""

Expand Down Expand Up @@ -238,32 +253,20 @@ def feed_builder_manager(httpserver):


@pytest.fixture
def use_config():
def use_config(tmp_path):
def internal(config_object: Dict, skip_default: bool = False):
for podcast in config_object["podcasts"]:
if "name" not in podcast and not skip_default:
podcast["name"] = generate_random_string()

config_file_name.write_text(json.dumps(config_object))

home_directory = Path.home()
config_file_name = home_directory / ".podcast_downloader_config.json"
backup_config_file_name = (
home_directory / ".safe_copy_podcast_downloader_config.json"
)

if config_file_name.exists():
config_file_name.rename(backup_config_file_name)
config_file_name = tmp_path / DEFAULT_CONFIG_NAME

yield internal

if backup_config_file_name.exists():
config_file_name.unlink()
backup_config_file_name.rename(config_file_name)


def run_podcast_downloader():
runner = PodcastDownloaderRunner()
runner.run()

return runner
@pytest.fixture
def podcast_downloader(tmp_path) -> Generator[PodcastDownloaderRunner, None, None]:
runner = PodcastDownloaderRunner(tmp_path)
yield runner
23 changes: 15 additions & 8 deletions e2e/test_config_options.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from functools import partial
from itertools import chain
from typing import Callable, Dict
from typing import Callable, Dict, List
from e2e.fixures import (
FeedBuilder,
MultipleFeedBuilder,
MultiplePodcastDirectory,
PodcastDirectory,
run_podcast_downloader,
PodcastDownloaderRunner,
# fixures:
download_destination_directory,
feed,
feed_builder_manager,
use_config,
podcast_directory,
podcast_directory_manager,
podcast_downloader,
)
from e2e.random import (
call_n_times,
Expand All @@ -28,6 +29,7 @@
def test_configuration_ignore_option(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand All @@ -46,7 +48,7 @@ def test_configuration_ignore_option(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
podcast_directory.is_containing_only([])
Expand All @@ -55,6 +57,7 @@ def test_configuration_ignore_option(
def test_configuration_podcast_extensions_option(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand Down Expand Up @@ -82,7 +85,7 @@ def test_configuration_podcast_extensions_option(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
podcast_directory.is_containing_only([file_name.lower() for file_name in avi_files])
Expand All @@ -91,6 +94,7 @@ def test_configuration_podcast_extensions_option(
def test_configuration_file_name_template_option(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand All @@ -113,7 +117,7 @@ def test_configuration_file_name_template_option(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
podcast_directory.is_containing_only(
Expand All @@ -127,6 +131,7 @@ def test_configuration_file_name_template_option(
def test_configuration_downloads_limit_option(
feed_builder_manager: MultipleFeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory_manager: MultiplePodcastDirectory,
):
# Arrange
Expand Down Expand Up @@ -157,7 +162,7 @@ def test_configuration_downloads_limit_option(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
downloaded_files_count = sum(
Expand All @@ -172,6 +177,7 @@ def test_configuration_downloads_limit_option(
def test_configuration_http_headers_option(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory_manager: MultiplePodcastDirectory,
):
# Arrange
Expand All @@ -197,7 +203,7 @@ def test_configuration_http_headers_option(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
assert (
Expand All @@ -211,6 +217,7 @@ def test_configuration_http_headers_option(
def test_configuration_fill_up_gaps_option(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand Down Expand Up @@ -248,7 +255,7 @@ def test_configuration_fill_up_gaps_option(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
podcast_directory.is_containing_only(
Expand Down
20 changes: 13 additions & 7 deletions e2e/test_logic.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from itertools import chain
from typing import Callable, Dict
from typing import Callable, Dict, List
from e2e.fixures import (
FeedBuilder,
PodcastDirectory,
run_podcast_downloader,
PodcastDownloaderRunner,
# fixures:
download_destination_directory,
feed,
use_config,
podcast_directory,
podcast_downloader,
)
from e2e.random import call_n_times, generate_random_mp3_file, generate_random_string


def test_configuration_hierarchy(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand All @@ -36,7 +38,7 @@ def test_configuration_hierarchy(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
assert len(podcast_directory.get_files_list()) == 1
Expand All @@ -45,6 +47,7 @@ def test_configuration_hierarchy(
def test_ignore_files_not_being_part_of_the_feed(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand All @@ -69,7 +72,7 @@ def test_ignore_files_not_being_part_of_the_feed(
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
podcast_directory.is_containing_only(
Expand All @@ -81,6 +84,7 @@ def test_ignore_files_not_being_part_of_the_feed(
def test_configuration_during_filling_up_gaps_should_not_download_existing_files(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand Down Expand Up @@ -118,7 +122,7 @@ def test_configuration_during_filling_up_gaps_should_not_download_existing_files
)

# Act
run_podcast_downloader()
podcast_downloader.run()

# Assert
assert set(
Expand All @@ -131,6 +135,7 @@ def test_configuration_during_filling_up_gaps_should_not_download_existing_files
def test_should_get_name_from_the_feed(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand All @@ -152,7 +157,7 @@ def test_should_get_name_from_the_feed(
)

# Act
runner = run_podcast_downloader()
runner = podcast_downloader.run()

# Assert
assert runner.is_correct(), "The script haven't finished work correctly"
Expand All @@ -164,6 +169,7 @@ def test_should_get_name_from_the_feed(
def test_configuration_ignore_option_display_feed_name(
feed: FeedBuilder,
use_config: Callable[[Dict], None],
podcast_downloader: Callable[[List[str]], PodcastDownloaderRunner],
podcast_directory: PodcastDirectory,
):
# Arrange
Expand All @@ -184,7 +190,7 @@ def test_configuration_ignore_option_display_feed_name(
)

# Act
runner = run_podcast_downloader()
runner = podcast_downloader.run()

# Assert
assert runner.is_highlighted_in_outcome(feed_title)
Expand Down
Loading

0 comments on commit c0419b3

Please sign in to comment.