diff --git a/elodie/filesystem.py b/elodie/filesystem.py index 01912d2..0686bfe 100644 --- a/elodie/filesystem.py +++ b/elodie/filesystem.py @@ -12,7 +12,7 @@ import shutil import time from elodie import geolocation -from elodie import constants +from elodie import log from elodie.localstorage import Db @@ -192,8 +192,7 @@ class FileSystem(object): db = Db() checksum = db.checksum(_file) if(checksum is None): - if(constants.debug is True): - print('Could not get checksum for %s. Skipping...' % _file) + log.info('Could not get checksum for %s. Skipping...' % _file) return # If duplicates are not allowed then we check if we've seen this file @@ -204,18 +203,16 @@ class FileSystem(object): checksum_file = db.get_hash(checksum) if(allow_duplicate is False and checksum_file is not None): if(os.path.isfile(checksum_file)): - if(constants.debug is True): - print('%s already exists at %s. Skipping...' % ( - _file, - checksum_file - )) + log.info('%s already exists at %s. Skipping...' % ( + _file, + checksum_file + )) return else: - if(constants.debug is True): - print('%s matched checksum but file not found at %s. Importing again...' % ( # noqa - _file, - checksum_file - )) + log.info('%s matched checksum but file not found at %s. Importing again...' % ( # noqa + _file, + checksum_file + )) self.create_directory(dest_directory) diff --git a/elodie/geolocation.py b/elodie/geolocation.py index 1befd9b..bce3423 100644 --- a/elodie/geolocation.py +++ b/elodie/geolocation.py @@ -15,6 +15,7 @@ import urllib.parse import urllib.error from elodie import constants +from elodie import log from elodie.localstorage import Db __KEY__ = None @@ -170,18 +171,14 @@ def lookup(**kwargs): path, urllib.parse.urlencode(params) ) - if(constants.debug is True): - print(url) r = requests.get(url) return parse_result(r.json()) except requests.exceptions.RequestException as e: - if(constants.debug is True): - print(e) + log.error(e) return None except ValueError as e: - if(constants.debug is True): - print(r.text) - print(e) + log.error(r.text) + log.error(e) return None diff --git a/elodie/log.py b/elodie/log.py new file mode 100644 index 0000000..b720057 --- /dev/null +++ b/elodie/log.py @@ -0,0 +1,39 @@ +""" +General file system methods. + +.. moduleauthor:: Jaisen Mathai +""" +from __future__ import print_function + +from json import dumps + +from elodie import constants + + +def info(message): + _print(message) + + +def info_json(payload): + _print(dumps(payload)) + + +def warn(message): + _print(message) + + +def warn_json(payload): + _print(dumps(payload)) + + +def error(message): + _print(message) + + +def error_json(payload): + _print(dumps(payload)) + + +def _print(string): + if(constants.debug is True): + print(string) diff --git a/elodie/media/photo.py b/elodie/media/photo.py index e89c032..8114990 100644 --- a/elodie/media/photo.py +++ b/elodie/media/photo.py @@ -15,7 +15,7 @@ from datetime import datetime from re import compile -from elodie import constants +from elodie import log from .media import Media @@ -73,8 +73,7 @@ class Photo(Media): seconds_since_epoch = time.mktime(time_tuple) break except BaseException as e: - if(constants.debug is True): - print(e) + log.error(e) pass if(seconds_since_epoch == 0): diff --git a/elodie/media/text.py b/elodie/media/text.py index ccdb6d3..559817e 100644 --- a/elodie/media/text.py +++ b/elodie/media/text.py @@ -5,15 +5,15 @@ are tracked by Elodie. .. moduleauthor:: Jaisen Mathai """ -# load modules -from elodie import constants -from elodie.media.base import Base - from json import dumps, loads import os from shutil import copyfileobj import time +# load modules +from elodie import log +from elodie.media.base import Base + class Text(Base): @@ -122,8 +122,7 @@ class Text(Base): if isinstance(parsed_json, dict): self.metadata_line = parsed_json except ValueError: - if(constants.debug is True): - print('Could not parse JSON from first line: %s' % first_line) + log.error('Could not parse JSON from first line: %s' % first_line) pass def write_metadata(self, **kwargs): diff --git a/elodie/tests/log_test.py b/elodie/tests/log_test.py new file mode 100644 index 0000000..d5367e1 --- /dev/null +++ b/elodie/tests/log_test.py @@ -0,0 +1,67 @@ +from __future__ import absolute_import +# Project imports + +import os +import sys +import unittest + +from json import dumps +from mock import patch +from nose.tools import with_setup +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) + +from elodie import constants +from elodie import log + + +os.environ['TZ'] = 'GMT' + +def call_log_and_assert(func, message, expected): + saved_stdout = sys.stdout + try: + out = StringIO() + sys.stdout = out + func(message) + output = out.getvalue().strip() + assert output == expected, (func, output) + finally: + sys.stdout = saved_stdout + +@patch('elodie.log') +@patch('elodie.constants.debug', True) +def test_calls_print_debug_true(fake_log): + expected = 'some string' + fake_log.info.return_value = expected + fake_log.warn.return_value = expected + fake_log.error.return_value = expected + for func in [log.info, log.warn, log.error]: + call_log_and_assert(func, expected, expected) + + expected_json = {'foo':'bar'} + fake_log.info.return_value = expected_json + fake_log.warn.return_value = expected_json + fake_log.error.return_value = expected_json + for func in [log.info_json, log.warn_json, log.error_json]: + call_log_and_assert(func, expected_json, dumps(expected_json)) + +@patch('elodie.log') +@patch('elodie.constants.debug', False) +def test_calls_print_debug_false(fake_log): + expected = 'some other string' + fake_log.info.return_value = expected + fake_log.warn.return_value = expected + fake_log.error.return_value = expected + for func in [log.info, log.warn, log.error]: + call_log_and_assert(func, expected, '') + + expected_json = {'foo':'bar'} + fake_log.info.return_value = expected_json + fake_log.warn.return_value = expected_json + fake_log.error.return_value = expected_json + for func in [log.info_json, log.warn_json, log.error_json]: + call_log_and_assert(func, expected_json, '')