Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(templates): Simplify tap template file names with post_gen_project.py hook #2060

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions cookiecutter/tap-template/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"JWT",
"Custom or N/A"
],
"include_ci_files": ["GitHub", "None (Skip)"],
"license": ["Apache-2.0"],
"include_ci_files": ["GitHub", "None"],
"license": ["Apache-2.0", "None"],
"__prompts__": {
"source_name": "The name of the source, in CamelCase",
"admin_name": "Provide your [bold yellow]full name[/]",
Expand Down
35 changes: 35 additions & 0 deletions cookiecutter/tap-template/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python
from pathlib import Path


BASE_PATH = Path('{{cookiecutter.library_name}}')


def delete_folder(pth: Path):
for sub in pth.iterdir():
if sub.is_dir():
delete_folder(sub)
else:
sub.unlink()
pth.rmdir()
vicmattos marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == '__main__':

# Rename stream type client and delete others
target = Path(BASE_PATH, 'client.py')
Path(BASE_PATH, '{{cookiecutter.stream_type|lower}}-client.py').rename(target)
[c.unlink() for c in Path(BASE_PATH).rglob("*-client.py")]

if '{{ cookiecutter.auth_method }}' not in ('OAuth2', 'JWT'):
Path(BASE_PATH, 'auth.py').unlink()

if '{{ cookiecutter.stream_type }}' == 'SQL':
Path(BASE_PATH, 'streams.py').unlink()

if '{{ cookiecutter.license }}' != 'Apache-2.0':
Path('LICENSE').unlink()

if '{{ cookiecutter.include_ci_files }}' != 'GitHub':
delete_folder(Path('.github'))

Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from typing import Iterable

import requests # noqa: TCH002
from singer_sdk.streams import {{ cookiecutter.stream_type }}Stream
from singer_sdk.streams import GraphQLStream

{%- if cookiecutter.auth_method in ("OAuth2", "JWT") %}

from {{ cookiecutter.library_name }}.auth import {{ cookiecutter.source_name }}Authenticator
{%- endif %}


class {{ cookiecutter.source_name }}Stream({{ cookiecutter.stream_type }}Stream):
class {{ cookiecutter.source_name }}Stream(GraphQLStream):
"""{{ cookiecutter.source_name }} stream class."""

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,24 @@
from __future__ import annotations

{% if cookiecutter.auth_method in ("OAuth2", "JWT") -%}
import sys
{% endif -%}
import sys{% endif -%}
from pathlib import Path
from typing import Any, Callable, Iterable

import requests
{% if cookiecutter.auth_method == "API Key" -%}
from singer_sdk.authenticators import APIKeyAuthenticator
from singer_sdk.helpers.jsonpath import extract_jsonpath
from singer_sdk.pagination import BaseAPIPaginator # noqa: TCH002
from singer_sdk.streams import {{ cookiecutter.stream_type }}Stream

{% elif cookiecutter.auth_method == "Bearer Token" -%}
from singer_sdk.authenticators import BearerTokenAuthenticator
from singer_sdk.helpers.jsonpath import extract_jsonpath
from singer_sdk.pagination import BaseAPIPaginator # noqa: TCH002
from singer_sdk.streams import {{ cookiecutter.stream_type }}Stream

{% elif cookiecutter.auth_method == "Basic Auth" -%}
from singer_sdk.authenticators import BasicAuthenticator
{% endif -%}
from singer_sdk.helpers.jsonpath import extract_jsonpath
from singer_sdk.pagination import BaseAPIPaginator # noqa: TCH002
from singer_sdk.streams import {{ cookiecutter.stream_type }}Stream

{% elif cookiecutter.auth_method == "Custom or N/A" -%}
from singer_sdk.helpers.jsonpath import extract_jsonpath
from singer_sdk.pagination import BaseAPIPaginator # noqa: TCH002
from singer_sdk.streams import {{ cookiecutter.stream_type }}Stream

{% elif cookiecutter.auth_method in ("OAuth2", "JWT") -%}
from singer_sdk.helpers.jsonpath import extract_jsonpath
from singer_sdk.pagination import BaseAPIPaginator # noqa: TCH002
from singer_sdk.streams import {{ cookiecutter.stream_type }}Stream
from singer_sdk.streams import RESTStream

from {{ cookiecutter.library_name }}.auth import {{ cookiecutter.source_name }}Authenticator

{% endif -%}

{%- if cookiecutter.auth_method in ("OAuth2", "JWT") -%}
if sys.version_info >= (3, 8):
Expand All @@ -53,7 +34,7 @@
SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")


class {{ cookiecutter.source_name }}Stream({{ cookiecutter.stream_type }}Stream):
class {{ cookiecutter.source_name }}Stream(RESTStream):
"""{{ cookiecutter.source_name }} stream class."""

@property
Expand Down
Loading