-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from jwills/jwills_plugins
Create a simple plugin system for loading data from external sources
- Loading branch information
Showing
22 changed files
with
700 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,3 +77,4 @@ target/ | |
|
||
.DS_Store | ||
.idea/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import abc | ||
import importlib | ||
from typing import Any | ||
from typing import Dict | ||
|
||
from ..utils import SourceConfig | ||
from dbt.dataclass_schema import dbtClassMixin | ||
|
||
|
||
class PluginConfig(dbtClassMixin): | ||
"""A helper class for defining the configuration settings a particular plugin uses.""" | ||
|
||
pass | ||
|
||
|
||
class Plugin(abc.ABC): | ||
WELL_KNOWN_PLUGINS = { | ||
"excel": "dbt.adapters.duckdb.plugins.excel.ExcelPlugin", | ||
"gsheet": "dbt.adapters.duckdb.plugins.gsheet.GSheetPlugin", | ||
"iceberg": "dbt.adapters.duckdb.plugins.iceberg.IcebergPlugin", | ||
"sqlalchemy": "dbt.adapters.duckdb.plugins.sqlalchemy.SQLAlchemyPlugin", | ||
} | ||
|
||
@classmethod | ||
def create(cls, impl: str, config: Dict[str, Any]) -> "Plugin": | ||
module_name, class_name = impl.rsplit(".", 1) | ||
module = importlib.import_module(module_name) | ||
Class = getattr(module, class_name) | ||
if not issubclass(Class, Plugin): | ||
raise TypeError(f"{impl} is not a subclass of Plugin") | ||
return Class(config) | ||
|
||
@abc.abstractmethod | ||
def __init__(self, plugin_config: Dict): | ||
pass | ||
|
||
def load(self, source_config: SourceConfig): | ||
"""Load data from a source config and return it as a DataFrame-like object that DuckDB can read.""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pathlib | ||
from typing import Dict | ||
|
||
import pandas as pd | ||
|
||
from . import Plugin | ||
from ..utils import SourceConfig | ||
|
||
|
||
class ExcelPlugin(Plugin): | ||
def __init__(self, config: Dict): | ||
self._config = config | ||
|
||
def load(self, source_config: SourceConfig): | ||
ext_location = source_config.meta["external_location"] | ||
ext_location = ext_location.format(**source_config.as_dict()) | ||
source_location = pathlib.Path(ext_location.strip("'")) | ||
sheet_name = source_config.meta.get("sheet_name", 0) | ||
return pd.read_excel(source_location, sheet_name=sheet_name) |
Oops, something went wrong.