Skip to content

Commit

Permalink
Pass constructor arguments via kwargs and override values from config…
Browse files Browse the repository at this point in the history
….toml
  • Loading branch information
sultaniman committed Apr 2, 2024
1 parent ef7a2df commit 3580765
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
36 changes: 29 additions & 7 deletions dlt/destinations/impl/filesystem/configuration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dataclasses

from typing import Callable, Dict, Final, Type, Optional, TypeAlias, Union
from typing import Any, Callable, Dict, Final, Type, Optional, TypeAlias, Union


from pendulum.datetime import DateTime
Expand Down Expand Up @@ -45,14 +45,26 @@ def resolve_credentials_type(self) -> Type[CredentialsConfiguration]:
) # type: ignore[return-value]

def on_resolved(self) -> None:
# If current_datetime is a callableg
"""Resolve configuration for filesystem destination
The following three variables will override the ones
if supplied via filesystem(...) constructor, additionally
we merge extra_params if provided and when provided via
the constructor it will take the priority over config.toml values
* current_datetime,
* datetime_format,
* extra_params
"""
# If current_datetime is a callable
# then we need to inspect it's return type
# if return type is not DateTime
# then call it and check it's instance
# if it is not DateTime then exit.
if self.current_datetime is not None:
if callable(self.current_datetime):
result = self.current_datetime()
current_datetime = self.kwargs.get("current_datetime", self.current_datetime)
if current_datetime is not None:
if callable(current_datetime):
result = current_datetime()
if isinstance(result, DateTime):
self.current_datetime = result
else:
Expand All @@ -61,6 +73,16 @@ def on_resolved(self) -> None:
"didn't return any instance of pendulum.DateTime"
)

# Validate layout and layout params
# layout_helper(self.layout, self.extra_params).check_layout()
datetime_format = self.kwargs.get("datetime_format", self.datetime_format)
if datetime_format is not None:
self.datetime_format = datetime_format

extra_params_arg: Dict[str, Any] = self.kwargs.get("extra_params", {})
if not self.extra_params and extra_params_arg:
self.extra_params = {}

if extra_params_arg:
for key, val in extra_params_arg.items():
self.extra_params[key] = val

super().on_resolved()
19 changes: 14 additions & 5 deletions dlt/destinations/impl/filesystem/factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from datetime import datetime
import typing as t

from dlt.destinations.impl.filesystem.configuration import FilesystemDestinationClientConfiguration
from dlt.destinations.impl.filesystem.configuration import (
FilesystemDestinationClientConfiguration,
)
from dlt.destinations.impl.filesystem import capabilities
from dlt.common.destination import Destination, DestinationCapabilitiesContext
from dlt.common.storages.configuration import FileSystemCredentials
Expand Down Expand Up @@ -56,14 +58,21 @@ def __init__(
values can be primitive types or callables which also should return a primitive type.
**kwargs: Additional arguments passed to the destination config
"""
# TODO: validate parameters
destinatination_kwargs: t.Dict[str, t.Any] = {}
if current_datetime:
destinatination_kwargs["current_datetime"] = current_datetime

if datetime_format:
destinatination_kwargs["datetime_format"] = datetime_format

if extra_params:
destinatination_kwargs["extra_params"] = extra_params

super().__init__(
bucket_url=bucket_url,
credentials=credentials,
destination_name=destination_name,
current_datetime=current_datetime,
datetime_format=datetime_format,
extra_params=extra_params,
environment=environment,
kwargs=destinatination_kwargs,
**kwargs,
)
2 changes: 2 additions & 0 deletions dlt/destinations/impl/filesystem/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def params(self) -> Optional[Dict[str, Any]]:
else:
self._params[key] = value

# For formatting options please see
# https://github.com/sdispater/pendulum/blob/master/docs/docs/string_formatting.md
self._params["year"] = now.year
# month, day, hour and minute padded with 0
self._params["month"] = now.format("MM")
Expand Down

0 comments on commit 3580765

Please sign in to comment.