Skip to content

Commit

Permalink
refactoring to introduce separate factories for Sink and Source objec…
Browse files Browse the repository at this point in the history
…ts - in anticipation of issues #5 #6 #7 #8 #9
  • Loading branch information
marc-portier committed Sep 23, 2021
1 parent 884dde3 commit b53c71c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 41 deletions.
6 changes: 4 additions & 2 deletions pyldt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions pyldt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
"""
Expand Down
29 changes: 0 additions & 29 deletions pyldt/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from abc import ABC, abstractmethod
from contextlib import contextmanager
import os
import csv
import logging


Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
jinja2
uritemplate
validators
requests
rfc6266
2 changes: 1 addition & 1 deletion tests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b53c71c

Please sign in to comment.