Skip to content

Commit

Permalink
Merge pull request #114 from humrochagf/master
Browse files Browse the repository at this point in the history
add config file lookup feature
  • Loading branch information
jtemporal authored Jul 24, 2017
2 parents 8ee9f74 + 74a77a8 commit d6b34a6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
27 changes: 27 additions & 0 deletions serenata_toolbox/datasets/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,30 @@ def save_to_csv(df, data_dir, name):
today = datetime.strftime(datetime.now(), '%Y-%m-%d')
file_path = os.path.join(data_dir, '{}-{}.xz'.format(today, name))
df.to_csv(file_path, **CSV_PARAMS)


# Utility for config file lookup (credits to fabric fabfile lookup)

def find_config(name='config.ini'):
"""
:param name: config file name (defaults to 'config.ini')
"""
# start in cwd and work downwards towards filesystem root
path = '.'
# Stop before falling off root of filesystem (should be platform
# agnostic)
while os.path.split(os.path.abspath(path))[1]:
joined = os.path.join(path, name)

# return config file absolute path if exists
if os.path.exists(joined) and os.path.isfile(joined):
return os.path.abspath(joined)

path = os.path.join('..', path)

# if config not found fallback on tests expected behavior
# wich is to return the filename to be used at remote
# config_exists class property.
# a future refactor could be to return None and not use
# config exists anymore
return name
6 changes: 4 additions & 2 deletions serenata_toolbox/datasets/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import boto3

from serenata_toolbox.datasets.contextmanager import status_message
from serenata_toolbox.datasets.helpers import find_config


class RemoteDatasets:
Expand All @@ -14,6 +15,7 @@ class RemoteDatasets:
def __init__(self):
self.credentials = None
self.client = None
self.config = find_config(self.CONFIG)

if not self.config_exists:
print('Could not find {} file.'.format(self.CONFIG))
Expand All @@ -22,7 +24,7 @@ def __init__(self):
return

settings = configparser.RawConfigParser()
settings.read(self.CONFIG)
settings.read(self.config)
self.settings = partial(settings.get, 'Amazon')

try:
Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(self):

@property
def config_exists(self):
return all((os.path.exists(self.CONFIG), os.path.isfile(self.CONFIG)))
return all((os.path.exists(self.config), os.path.isfile(self.config)))

@property
def bucket(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
'serenata_toolbox.datasets'
],
url=REPO_URL,
version='12.0.2'
version='12.0.3'
)
48 changes: 48 additions & 0 deletions tests/unit/test_datasets_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import tempfile
from datetime import datetime
from unittest import main, TestCase
from unittest.mock import patch
Expand All @@ -8,6 +9,7 @@

from serenata_toolbox.datasets import helpers


class TestDatasetsHelpersXml(TestCase):

def setUp(self):
Expand Down Expand Up @@ -51,6 +53,7 @@ def test_extract_datetime_supports_custom_format(self):

self.assertEqual(expected, extracted)


class TestDatasetsHelpersDataframes(TestCase):

def test_translate_column(self):
Expand Down Expand Up @@ -83,5 +86,50 @@ def test_save_to_csv(self, mock_df):
index=False
)


class TestDatasetsHelpersConfigLookup(TestCase):

def setUp(self):
# good case
self.root = os.path.realpath(tempfile.mkdtemp())
self.subfolder = os.path.relpath(tempfile.mkdtemp(dir=self.root))
self.config_file = os.path.join(self.root, 'config.ini')
self.cwd = os.getcwd()
open(self.config_file, 'a').close()

# not found case
self.root_not_found = os.path.relpath(tempfile.mkdtemp())

# not a file case
self.root_not_a_file = os.path.relpath(tempfile.mkdtemp())
self.config_folder = os.path.join(self.root_not_a_file, 'config.ini')
os.makedirs(self.config_folder)


def tearDown(self):
os.chdir(self.cwd)
os.remove(self.config_file)
os.rmdir(self.subfolder)
os.rmdir(self.root)
os.rmdir(self.root_not_found)
os.rmdir(self.config_folder)
os.rmdir(self.root_not_a_file)

def test_find_config(self):
os.chdir(self.subfolder)

self.assertEqual(helpers.find_config(), self.config_file)

def test_find_config_not_found(self):
os.chdir(self.root_not_found)

self.assertEqual(helpers.find_config(), 'config.ini')

def test_find_config_a_file(self):
os.chdir(self.root_not_a_file)

self.assertEqual(helpers.find_config(), 'config.ini')


if __name__ == '__main__':
main()

0 comments on commit d6b34a6

Please sign in to comment.