Merge pull request #150 from jmathai/add-logger
Add log module and tests
This commit is contained in:
commit
3fc43c97bd
|
@ -12,7 +12,7 @@ import shutil
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from elodie import geolocation
|
from elodie import geolocation
|
||||||
from elodie import constants
|
from elodie import log
|
||||||
from elodie.localstorage import Db
|
from elodie.localstorage import Db
|
||||||
|
|
||||||
|
|
||||||
|
@ -192,8 +192,7 @@ class FileSystem(object):
|
||||||
db = Db()
|
db = Db()
|
||||||
checksum = db.checksum(_file)
|
checksum = db.checksum(_file)
|
||||||
if(checksum is None):
|
if(checksum is None):
|
||||||
if(constants.debug is True):
|
log.info('Could not get checksum for %s. Skipping...' % _file)
|
||||||
print('Could not get checksum for %s. Skipping...' % _file)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# If duplicates are not allowed then we check if we've seen this file
|
# 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)
|
checksum_file = db.get_hash(checksum)
|
||||||
if(allow_duplicate is False and checksum_file is not None):
|
if(allow_duplicate is False and checksum_file is not None):
|
||||||
if(os.path.isfile(checksum_file)):
|
if(os.path.isfile(checksum_file)):
|
||||||
if(constants.debug is True):
|
log.info('%s already exists at %s. Skipping...' % (
|
||||||
print('%s already exists at %s. Skipping...' % (
|
_file,
|
||||||
_file,
|
checksum_file
|
||||||
checksum_file
|
))
|
||||||
))
|
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if(constants.debug is True):
|
log.info('%s matched checksum but file not found at %s. Importing again...' % ( # noqa
|
||||||
print('%s matched checksum but file not found at %s. Importing again...' % ( # noqa
|
_file,
|
||||||
_file,
|
checksum_file
|
||||||
checksum_file
|
))
|
||||||
))
|
|
||||||
|
|
||||||
self.create_directory(dest_directory)
|
self.create_directory(dest_directory)
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import urllib.parse
|
||||||
import urllib.error
|
import urllib.error
|
||||||
|
|
||||||
from elodie import constants
|
from elodie import constants
|
||||||
|
from elodie import log
|
||||||
from elodie.localstorage import Db
|
from elodie.localstorage import Db
|
||||||
|
|
||||||
__KEY__ = None
|
__KEY__ = None
|
||||||
|
@ -170,18 +171,14 @@ def lookup(**kwargs):
|
||||||
path,
|
path,
|
||||||
urllib.parse.urlencode(params)
|
urllib.parse.urlencode(params)
|
||||||
)
|
)
|
||||||
if(constants.debug is True):
|
|
||||||
print(url)
|
|
||||||
r = requests.get(url)
|
r = requests.get(url)
|
||||||
return parse_result(r.json())
|
return parse_result(r.json())
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
if(constants.debug is True):
|
log.error(e)
|
||||||
print(e)
|
|
||||||
return None
|
return None
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
if(constants.debug is True):
|
log.error(r.text)
|
||||||
print(r.text)
|
log.error(e)
|
||||||
print(e)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
"""
|
||||||
|
General file system methods.
|
||||||
|
|
||||||
|
.. moduleauthor:: Jaisen Mathai <jaisen@jmathai.com>
|
||||||
|
"""
|
||||||
|
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)
|
|
@ -15,7 +15,7 @@ from datetime import datetime
|
||||||
from re import compile
|
from re import compile
|
||||||
|
|
||||||
|
|
||||||
from elodie import constants
|
from elodie import log
|
||||||
from .media import Media
|
from .media import Media
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,8 +73,7 @@ class Photo(Media):
|
||||||
seconds_since_epoch = time.mktime(time_tuple)
|
seconds_since_epoch = time.mktime(time_tuple)
|
||||||
break
|
break
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
if(constants.debug is True):
|
log.error(e)
|
||||||
print(e)
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if(seconds_since_epoch == 0):
|
if(seconds_since_epoch == 0):
|
||||||
|
|
|
@ -5,15 +5,15 @@ are tracked by Elodie.
|
||||||
.. moduleauthor:: Jaisen Mathai <jaisen@jmathai.com>
|
.. moduleauthor:: Jaisen Mathai <jaisen@jmathai.com>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# load modules
|
|
||||||
from elodie import constants
|
|
||||||
from elodie.media.base import Base
|
|
||||||
|
|
||||||
from json import dumps, loads
|
from json import dumps, loads
|
||||||
import os
|
import os
|
||||||
from shutil import copyfileobj
|
from shutil import copyfileobj
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
# load modules
|
||||||
|
from elodie import log
|
||||||
|
from elodie.media.base import Base
|
||||||
|
|
||||||
|
|
||||||
class Text(Base):
|
class Text(Base):
|
||||||
|
|
||||||
|
@ -122,8 +122,7 @@ class Text(Base):
|
||||||
if isinstance(parsed_json, dict):
|
if isinstance(parsed_json, dict):
|
||||||
self.metadata_line = parsed_json
|
self.metadata_line = parsed_json
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if(constants.debug is True):
|
log.error('Could not parse JSON from first line: %s' % first_line)
|
||||||
print('Could not parse JSON from first line: %s' % first_line)
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def write_metadata(self, **kwargs):
|
def write_metadata(self, **kwargs):
|
||||||
|
|
|
@ -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, '')
|
Loading…
Reference in New Issue