Passed
Push — master ( 9ddb09...7fc5b5 )
by Jaisen
01:14
created

elodie.tests.helper   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 179
Duplicated Lines 5.59 %

Importance

Changes 0
Metric Value
eloc 118
dl 10
loc 179
rs 9.28
c 0
b 0
f 0
wmc 39

17 Functions

Rating   Name   Duplication   Size   Complexity  
A random_decimal() 0 2 1
A random_coordinate() 0 3 1
A path_tz_fix() 0 12 2
A download_file() 0 15 3
A reset_dbs() 0 16 5
B populate_folder() 0 16 6
A time_convert() 0 5 2
A create_working_folder() 0 6 1
A checksum() 10 10 3
A random_string() 0 8 3
A isclose() 0 7 3
A is_windows() 0 2 1
A get_file() 0 6 2
A get_file_path() 0 3 1
A get_test_location() 0 2 1
A restore_dbs() 0 9 3
A temp_dir() 0 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from __future__ import division
2
from __future__ import unicode_literals
3
from builtins import range
4
from past.utils import old_div
5
import hashlib
6
import os
7
import random
8
import string
9
import tempfile
10
import re
11
import time
12
import urllib
13
14
from datetime import datetime
15
from datetime import timedelta
16
17
from elodie.compatability import _rename
18
from elodie import constants
19
20 View Code Duplication
def checksum(file_path, blocksize=65536):
0 ignored issues
show
Duplication introduced by Jaisen Mathai
This code seems to be duplicated in your project.
Loading history...
21
    hasher = hashlib.sha256()
22
    with open(file_path, 'rb') as f:
23
        buf = f.read(blocksize)
24
25
        while len(buf) > 0:
26
            hasher.update(buf)
27
            buf = f.read(blocksize)
28
        return hasher.hexdigest()
29
    return None
30
31
def create_working_folder(format=None):
32
    temporary_folder = tempfile.gettempdir()
33
    folder = os.path.join(temporary_folder, random_string(10, format), random_string(10, format))
34
    os.makedirs(folder)
35
36
    return (temporary_folder, folder)
37
38
def download_file(name, destination):
39
    try:
40
        url_to_file = 'https://s3.amazonaws.com/jmathai/github/elodie/{}'.format(name)
41
        # urlretrieve works differently for python 2 and 3
42
        if constants.python_version < 3:
43
            final_name = '{}/{}{}'.format(destination, random_string(10), os.path.splitext(name)[1])
44
            urllib.urlretrieve(
45
                url_to_file,
46
                final_name
47
            )
48
        else:
49
            final_name, headers = urllib.request.urlretrieve(url_to_file)
50
        return final_name
51
    except Exception as e:
52
        return False
53
    
54
def get_file(name):
55
    file_path = get_file_path(name)
56
    if not os.path.isfile(file_path):
57
        return False
58
59
    return file_path
60
61
def get_file_path(name):
62
    current_folder = os.path.dirname(os.path.realpath(__file__))
63
    return os.path.join(current_folder, 'files', name)
64
65
def get_test_location():
66
    return (61.013710, 99.196656, 'Siberia')
67
68
def populate_folder(number_of_files, include_invalid=False):
69
    folder = '%s/%s' % (tempfile.gettempdir(), random_string(10))
70
    os.makedirs(folder)
71
72
    for x in range(0, number_of_files):
73
        ext = 'jpg' if x % 2 == 0 else 'txt'
74
        fname = '%s/%s.%s' % (folder, x, ext)
75
        with open(fname, 'a'):
76
            os.utime(fname, None)
77
78
    if include_invalid:
79
        fname = '%s/%s' % (folder, 'invalid.invalid')
80
        with open(fname, 'a'):
81
            os.utime(fname, None)
82
83
    return folder
84
85
def random_string(length, format=None):
86
    format_choice = string.ascii_uppercase + string.digits
87
    if format == 'int':
88
        format_choice = string.digits
89
    elif format == 'str':
90
        format_choice = string.asci_uppercase
91
92
    return ''.join(random.SystemRandom().choice(format_choice) for _ in range(length))
93
94
def random_decimal():
95
    return random.random()
96
97
def random_coordinate(coordinate, precision):
98
    # Here we add to the decimal section of the coordinate by a given precision
99
    return coordinate + ((old_div(10.0, (10.0**precision))) * random_decimal())
100
101
def temp_dir():
102
    return tempfile.gettempdir()
103
104
def is_windows():
105
    return os.name == 'nt'
106
107
# path_tz_fix(file_name)
108
# Change timestamp in file_name by the offset
109
# between UTC and local time, i.e.
110
#  2015-12-05_00-59-26-with-title-some-title.jpg ->
111
#  2015-12-04_20-59-26-with-title-some-title.jpg
112
# (Windows only)
113
114
def path_tz_fix(file_name):
115
  if is_windows():
116
      # Calculate the offset between UTC and local time
117
      tz_shift = old_div((datetime.fromtimestamp(0) -
118
                  datetime.utcfromtimestamp(0)).seconds,3600)
119
      # replace timestamp in file_name
120
      m = re.search('(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})',file_name)
121
      t_date = datetime.fromtimestamp(time.mktime(time.strptime(m.group(0), '%Y-%m-%d_%H-%M-%S')))
122
      s_date_fix = (t_date-timedelta(hours=tz_shift)).strftime('%Y-%m-%d_%H-%M-%S')
123
      return re.sub('\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}',s_date_fix,file_name)
124
  else:
125
      return file_name
126
127
# time_convert(s_time)
128
# Change s_time (struct_time) by the offset
129
# between UTC and local time
130
# (Windows only)
131
132
def time_convert(s_time):
133
    if is_windows():
134
        return time.gmtime((time.mktime(s_time)))
135
    else:
136
        return s_time
137
138
139
# isclose(a,b,rel_tol)
140
# To compare float coordinates a and b
141
# with relative tolerance c
142
143
def isclose(a, b, rel_tol = 1e-8):
144
    if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
145
        return False
146
147
    diff = abs(a - b)
148
    return (diff <= abs(rel_tol * a) and
149
            diff <= abs(rel_tol * b))
150
151
def reset_dbs():
152
    """ Back up hash_db and location_db """
153
    hash_db = '{}-test'.format(constants.hash_db)
154
    if not os.path.isfile(hash_db):
155
	    hash_db = constants.hash_db
156
	    if os.path.isfile(hash_db):
157
	        _rename(hash_db, '{}-test'.format(hash_db))
158
    #else restore_dbs wasn't called by a previous test, keep the
159
    #existing hash_db backup
160
	
161
162
    location_db = '{}-test'.format(constants.location_db)
163
    if not os.path.isfile(location_db):
164
	    location_db = constants.location_db
165
	    if os.path.isfile(location_db):
166
	        _rename(location_db, '{}-test'.format(location_db))
167
    #else restore_dbs wasn't called by a previous test, keep the
168
    #existing location_db backup
169
170
def restore_dbs():
171
    """ Restore back ups of hash_db and location_db """
172
    hash_db = '{}-test'.format(constants.hash_db)
173
    if os.path.isfile(hash_db):
174
        _rename(hash_db, hash_db.replace('-test', ''))
175
176
    location_db = '{}-test'.format(constants.location_db)
177
    if os.path.isfile(location_db):
178
        _rename(location_db, location_db.replace('-test', ''))
179