Skip to content

Commit

Permalink
fix: Use importlib.resources
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Aug 4, 2022
1 parent a1e5589 commit 95b5642
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ from {{ cookiecutter.library_name }}.auth import {{ cookiecutter.source_name }}A
{%- endif %}


SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ from pathlib import Path
from typing import Any, Dict, Optional, Union, List, Iterable

from singer_sdk import typing as th # JSON Schema typing helpers
from singer_sdk.helpers._util import get_package_files

from {{ cookiecutter.library_name }} import schemas
from {{ cookiecutter.library_name }}.client import {{ cookiecutter.source_name }}Stream

# TODO: Delete this is if not using json files for schema definition
SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")
SCHEMAS_DIR = get_package_files(schemas)


{%- if cookiecutter.stream_type == "GraphQL" %}
Expand Down
6 changes: 6 additions & 0 deletions docs/code_samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class TapCountries(Tap):
### Define a simple GraphQL-based stream with schema defined in a file

```python
from singer_sdk.helpers._util import get_package_files
from tap_countries import schemas

SCHEMAS_DIR = get_package_files(schemas)


class ContinentsStream(GraphQLStream):
"""Continents stream from the Countries API."""

Expand Down
71 changes: 45 additions & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ PyJWT = "~=2.4"
requests = "^2.25.1"
cryptography = ">=3.4.6,<38.0.0"
importlib-metadata = {version = "*", markers = "python_version < \"3.8\""}
importlib-resources = {version = "^5.9.0", markers = "python_version < \"3.9\""}
memoization = ">=0.3.2,<0.5.0"
jsonpath-ng = "^1.5.3"
joblib = "^1.0.1"
Expand Down
Empty file added samples/__init__.py
Empty file.
Empty file added samples/aapl/__init__.py
Empty file.
5 changes: 3 additions & 2 deletions samples/aapl/aapl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""A simple tap with one big record and schema."""

import json
from pathlib import Path

from samples import aapl
from singer_sdk import Stream, Tap
from singer_sdk.helpers._util import get_package_files

PROJECT_DIR = Path(__file__).parent
PROJECT_DIR = get_package_files(aapl)


class AAPL(Stream):
Expand Down
7 changes: 3 additions & 4 deletions samples/sample_tap_countries/countries_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
"""

import abc
from pathlib import Path

from samples.sample_tap_countries import schemas
from singer_sdk import typing as th
from singer_sdk.helpers._util import get_package_files
from singer_sdk.streams.graphql import GraphQLStream

SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")


class CountriesAPIStream(GraphQLStream, metaclass=abc.ABCMeta):
"""Sample tap test for countries.
Expand Down Expand Up @@ -80,7 +79,7 @@ class ContinentsStream(CountriesAPIStream):

name = "continents"
primary_keys = ["code"]
schema_filepath = SCHEMAS_DIR / "continents.json"
schema_filepath = get_package_files(schemas, "continents.json")
query = """
continents {
code
Expand Down
Empty file.
6 changes: 4 additions & 2 deletions samples/sample_tap_gitlab/gitlab_graphql_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
# - https://gitlab.com/-/graphql-explorer
"""

from pathlib import Path
import sys

from samples.sample_tap_gitlab import schemas
from singer_sdk.helpers._util import get_package_files
from singer_sdk.streams import GraphQLStream

SITE_URL = "https://gitlab.com/graphql"

SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")
SCHEMAS_DIR = get_package_files(schemas)


class GitlabGraphQLStream(GraphQLStream):
Expand Down
5 changes: 3 additions & 2 deletions samples/sample_tap_gitlab/gitlab_rest_streams.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Sample tap stream test for tap-gitlab."""

from pathlib import Path
from typing import Any, Dict, List, Optional, cast

import requests

from samples.sample_tap_gitlab import schemas
from singer_sdk.authenticators import SimpleAuthenticator
from singer_sdk.helpers._util import get_package_files
from singer_sdk.streams.rest import RESTStream
from singer_sdk.typing import (
ArrayType,
Expand All @@ -16,7 +17,7 @@
StringType,
)

SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")
SCHEMAS_DIR = get_package_files(schemas)

DEFAULT_URL_BASE = "https://gitlab.com/api/v4"

Expand Down
Empty file.
5 changes: 4 additions & 1 deletion samples/sample_tap_google_analytics/ga_tap_stream.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"""Sample tap stream test for tap-google-analytics."""

import sys
from pathlib import Path
from typing import Any, Iterable, List, Optional, cast

import pendulum

from samples.sample_tap_google_analytics import schemas
from singer_sdk.authenticators import OAuthJWTAuthenticator
from singer_sdk.helpers._util import get_package_files
from singer_sdk.streams import RESTStream

GOOGLE_OAUTH_ENDPOINT = "https://oauth2.googleapis.com/token"
GA_OAUTH_SCOPES = "https://www.googleapis.com/auth/analytics.readonly"
SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")
SCHEMAS_DIR = get_package_files(schemas)


class GoogleJWTAuthenticator(OAuthJWTAuthenticator):
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions singer_sdk/helpers/_util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"""General helper functions, helper classes, and decorators."""

import json
import sys
from pathlib import Path, PurePath
from typing import Any, Dict, Union, cast

import pendulum

if sys.version_info >= (3, 9):
import importlib.resources as resources
from importlib.abc import Traversable
else:
import importlib_resources as resources
from importlib_resources.abc import Traversable


def read_json_file(path: Union[PurePath, str]) -> Dict[str, Any]:
"""Read json file, thowing an error if missing."""
Expand All @@ -25,3 +33,15 @@ def read_json_file(path: Union[PurePath, str]) -> Dict[str, Any]:
def utc_now() -> pendulum.DateTime:
"""Return current time in UTC."""
return pendulum.now(tz="UTC")


def get_package_files(package: resources.Package, resource: str) -> Traversable:
"""Load a static files path from a package.
This is a wrapper around importlib.resources.files().
Args:
package: The package to load the resource from.
resource: The name of the resource to load.
"""
return resources.files(package)
Loading

0 comments on commit 95b5642

Please sign in to comment.