-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Expectations, env config and pipelines
- Loading branch information
Showing
7 changed files
with
128 additions
and
82 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 |
---|---|---|
@@ -1,31 +1,33 @@ | ||
import enum | ||
import os | ||
from typing import Optional | ||
|
||
from dotenv import load_dotenv | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
|
||
load_dotenv() | ||
|
||
|
||
class Config(BaseModel): | ||
# S3_ENDPOINT: str = Field(default=os.environ["AWS_ENDPOINT_URL"]) | ||
# S3_BUCKET: str = Field(default=os.environ["S3_BUCKET"]) | ||
# S3_ACCESS_KEY_ID: str = Field(default=os.environ["AWS_ACCESS_KEY_ID"]) | ||
# S3_SECRET_ACCESS_KEY: str = Field(default=os.environ["AWS_SECRET_ACCESS_KEY"]) | ||
# TELEGRAM_SENDER_TOKEN: str = Field(default=os.environ["TELEGRAM_SENDER_TOKEN"]) | ||
pass | ||
class EnvConfig(BaseModel): | ||
@classmethod | ||
def create_config(cls): | ||
fields = {} | ||
for field_name, field_value in cls.__fields__.items(): | ||
if field_name in os.environ: | ||
fields[field_name] = os.environ[field_name] | ||
else: | ||
raise ValueError( | ||
f"Required environment variable '{field_name}' not set" | ||
) | ||
|
||
return cls(**fields) | ||
|
||
config = Config() | ||
|
||
config = EnvConfig() | ||
|
||
class BackendType(enum.Enum): | ||
FILE = "file" | ||
S3 = "s3" | ||
META = "meta" | ||
SCRAPEOPS_API_KEY: Optional[str] = Field(default=os.environ.get("SCRAPEOPS_API_KEY")) | ||
|
||
|
||
class PipelineConfig(BaseModel): | ||
base_url: str | ||
backend: BackendType | ||
s3_bucket: str | ||
|
||
filename: str |
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,7 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
# This is a generic class to manage output expectation, like number of items, format, etc. | ||
class Expectations(BaseModel): | ||
def is_met(self): | ||
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 |
---|---|---|
@@ -1,30 +1,52 @@ | ||
import json | ||
from typing import Dict, Iterable, List, Union | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Dict, Iterable, Union | ||
|
||
import boto3 | ||
from pydantic import BaseModel | ||
from pydantic import Field | ||
|
||
from scrapework.config import BackendType, PipelineConfig | ||
|
||
class Pipeline(ABC): | ||
@abstractmethod | ||
def process_items( | ||
self, | ||
items: Union[Dict[str, Any], Iterable[Dict[str, Any]]], | ||
filename: str | None, | ||
): | ||
pass | ||
|
||
class ItemPipeline(BaseModel): | ||
def process_items(self, items: Union[Dict, Iterable], config: PipelineConfig): | ||
if config.backend == BackendType.FILE: | ||
self.export_to_json(list(items), config) | ||
elif config.backend == BackendType.S3: | ||
self.export_to_s3(items, config) | ||
|
||
def export_to_json(self, items: Union[Dict, List], config: PipelineConfig): | ||
file_name = config.filename | ||
with open(file_name, "w") as f: | ||
class JsonFilePipeline(Pipeline): | ||
def process_items( | ||
self, items: Union[Dict[str, Any], Iterable[Dict[str, Any]]], filename: str | ||
): | ||
|
||
with open(filename, "w") as f: | ||
json.dump(items, f) | ||
|
||
def export_to_s3(self, items: Union[Dict, Iterable], config: PipelineConfig): | ||
if not config.s3_bucket: | ||
raise ValueError("S3 bucket name not provided in the configuration.") | ||
|
||
class S3Pipeline(Pipeline): | ||
s3_bucket: str = Field(default_factory=str) | ||
|
||
def process_items( | ||
self, | ||
items: Union[Dict[str, Any], Iterable[Dict[str, Any]]], | ||
filename: str, | ||
): | ||
|
||
s3_client = boto3.client("s3") | ||
file_name = config.filename | ||
|
||
s3_client.put_object( | ||
Body=json.dumps(items), Bucket=config.s3_bucket, Key=file_name | ||
Body=json.dumps(items), Bucket=self.s3_bucket, Key=filename | ||
) | ||
|
||
|
||
class MetadataPipeline(Pipeline): | ||
def process_items( | ||
self, | ||
items: Union[Dict[str, Any], Iterable[Dict[str, Any]]], | ||
): | ||
if isinstance(items, dict): | ||
return {"items_count": 1} | ||
else: | ||
return {"items_count": len(list(items))} |
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