Add log module and tests
This commit is contained in:
		
							parent
							
								
									8eaba9e98c
								
							
						
					
					
						commit
						a1d3cb35fc
					
				@ -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,15 +203,13 @@ 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...' % (
 | 
			
		||||
                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
 | 
			
		||||
                log.info('%s matched checksum but file not found at %s. Importing again...' % (  # noqa
 | 
			
		||||
                    _file,
 | 
			
		||||
                    checksum_file
 | 
			
		||||
                ))
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										39
									
								
								elodie/log.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								elodie/log.py
									
									
									
									
									
										Normal file
									
								
							@ -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 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):
 | 
			
		||||
 | 
			
		||||
@ -5,15 +5,15 @@ are tracked by Elodie.
 | 
			
		||||
.. moduleauthor:: Jaisen Mathai <jaisen@jmathai.com>
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
# 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):
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										67
									
								
								elodie/tests/log_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								elodie/tests/log_test.py
									
									
									
									
									
										Normal file
									
								
							@ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user