From b53c71c9ba02f2faab77c07983dfc86e7e249194 Mon Sep 17 00:00:00 2001 From: Marc Portier Date: Thu, 23 Sep 2021 16:23:54 +0200 Subject: [PATCH] refactoring to introduce separate factories for Sink and Source objects - in anticipation of issues #5 #6 #7 #8 #9 --- pyldt/__init__.py | 6 ++++-- pyldt/__main__.py | 19 ++++++++++--------- pyldt/api.py | 29 ----------------------------- requirements.txt | 3 +++ tests/test_generator.py | 2 +- 5 files changed, 18 insertions(+), 41 deletions(-) diff --git a/pyldt/__init__.py b/pyldt/__init__.py index 3d7447e..3e3cb1b 100644 --- a/pyldt/__init__.py +++ b/pyldt/__init__.py @@ -7,5 +7,7 @@ """ -from .api import * -from .generator import * +from .api import Sink, Source, Settings +from .generator import JinjaBasedGenerator +from .sources import SourceFactory +from .sinks import SinkFactory diff --git a/pyldt/__main__.py b/pyldt/__main__.py index 8c46291..b74d37d 100644 --- a/pyldt/__main__.py +++ b/pyldt/__main__.py @@ -2,9 +2,10 @@ import argparse import sys -from .api import * -from .generator import JinjaBasedGenerator - +from .generator import Generator, JinjaBasedGenerator +from .api import Sink, Source, Settings, log +from .sources import SourceFactory +from .sinks import SinkFactory def get_arg_parser(): @@ -33,19 +34,19 @@ def make_service(args: argparse.Namespace) -> Generator: template_folder = args.templates return JinjaBasedGenerator(template_folder) + def make_sources(args: argparse.Namespace) -> dict: inputs = dict() if args.input is not None: - inputs['_'] = CSVFileSource(args.input) + inputs['_'] = SourceFactory.make_source(args.input) if args.sets is not None: - assert True is False, "failing --sets to load additional input sets is not yet supported" + assert False, "failing --sets to load additional input sets is not yet supported" return inputs + def make_sink(args:argparse.Namespace ) -> Sink: - if args.output is None: - return StdOutSink() - #else - assert True is False, "failing --output is not yet supported, we just dump results to stdout for now" + return SinkFactory.make_sink(args.output) + def main(): """ diff --git a/pyldt/api.py b/pyldt/api.py index 6a0a547..95b36b5 100644 --- a/pyldt/api.py +++ b/pyldt/api.py @@ -1,7 +1,6 @@ from abc import ABC, abstractmethod from contextlib import contextmanager import os -import csv import logging @@ -22,17 +21,6 @@ def add(self, part: str): """ -class StdOutSink(Sink) : - def __init__ (self): - pass - - def add(self, part: str): - print(part) - - def __repr__(self): - return "StdOutSink" - - class Source(ABC): """ Abstract Interface for input Sources @@ -43,23 +31,6 @@ def iterator(self ): return [] -class CSVFileSource(Source): - """ - Source producing iterator over data-set coming from CSV on file - """ - def __init__ (self, csv_file_path): - #todo assert if file exists and is readable - self._csv = csv_file_path - - @contextmanager - def iterator(self): - with open(self._csv) as csvfile: - csvreader = csv.DictReader(csvfile, delimiter=',') - yield csvreader - - def __repr__(self): - return "CSVFileSource('%s')" % os.path.abspath(self._csv) - class Settings: """ Embodies all the actual possible modifiers to the process diff --git a/requirements.txt b/requirements.txt index 013a86a..4630c26 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,5 @@ jinja2 uritemplate +validators +requests +rfc6266 diff --git a/tests/test_generator.py b/tests/test_generator.py index 2867c53..cfcb0c7 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -51,7 +51,7 @@ def test_something(self): settings = Settings() sets = dict() - sets["_"] = CSVFileSource(os.path.join(base, "data.csv")) + sets["_"] = SourceFactory.make_source(os.path.join(base, "data.csv")) sink = AssertingSink(self)