# Path: django_test_tools/file_utils.py
# def add_date(filename, **kwargs):
#     """
#     Adds to a filename the current date and time in '%Y%m%d_%H%M' format.
#     For a filename /my/path/myexcel.xlsx the function would return /my/path/myexcel_20170101_1305.xlsx.
#     If the file already exists the function will add seconds to the date to attempt to get a unique name.
# 
#     The function will detect if another file exists with the same name if it exist it will append seconds to the
#     filename. For example if file /my/path/myexcel_20170101_1305.xlsx alread exist thte function will return
#     /my/path/myexcel_20170101_130521.xlsx.
# 
#     :param filename: string with fullpath to file or just the filename
#     :param kwargs: dictionary. date_position: suffix or preffix, extension: string to replace extension
#     :return: string with full path string including the date and time
#     """
#     current_datetime = timezone.localtime(timezone.now()).strftime('%Y%m%d_%H%M%S')
#     new_filename_data = dict()
#     suffix_template = '{path}{separator}{filename_with_out_extension}_{datetime}.{extension}'
#     prefix_template = '{path}{separator}{datetime}_{filename_with_out_extension}.{extension}'
#     if '/' in filename and '\\' in filename:
#         raise ValueError('Filename %s contains both / and \\ separators' % filename)
#     if '\\' in filename:
#         path_parts = filename.split('\\')
#         file = path_parts[-1]
#         path = '\\'.join(path_parts[:-1])
#         separator = '\\'
#     elif '/' in filename:
#         path_parts = filename.split('/')
#         file = path_parts[-1]
#         path = '/'.join(path_parts[:-1])
#         separator = '/'
#     else:
#         file = filename
#         path = ''
#         separator = ''
# 
#     new_filename_data['path'] = path
#     parts = file.split('.')
#     if kwargs.get('extension', None) is not None:
#         new_filename_data['extension'] = kwargs['extension']
#     else:
#         if len(parts) > 1:
#             new_filename_data['extension'] = parts[-1]
#         else:
#             new_filename_data['extension'] = ''
# 
#     new_filename_data['separator'] = separator
#     if new_filename_data['extension'] == '':
#         new_filename_data['filename_with_out_extension'] = parts[0]
#     else:
#         new_filename_data['filename_with_out_extension'] = '.'.join(parts[:-1])
#     new_filename_data['datetime'] = current_datetime[:-2]  # Seconds are stripped
# 
#     date_position = kwargs.get('date_position', 'suffix')
#     if date_position == 'suffix':
#         new_filename = suffix_template.format(**new_filename_data)
#         if os.path.exists(new_filename):
#             new_filename_data['datetime'] = current_datetime
#             new_filename = suffix_template.format(**new_filename_data)
#         if new_filename_data['extension'] == '':
#             new_filename = new_filename[:-1]
#     else:
#         new_filename = prefix_template.format(**new_filename_data)
#         if os.path.exists(new_filename):
#             new_filename_data['datetime'] = current_datetime
#             new_filename = prefix_template.format(**new_filename_data)
#         if new_filename_data['extension'] == '':
#             new_filename = new_filename[:-1]
# 
#     return new_filename


import functools
import json
import os
import re
import time
import warnings
import pytz
from datetime import datetime, date, timedelta
from django.conf import settings
from django.utils import timezone
from openpyxl.compat import deprecated
from .file_utils import add_date



__author__ = 'lberrocal'


def versiontuple(v):
    return tuple(map(int, (v.split("."))))

def dict_compare(d1, d2):
    d1_keys = set(d1.keys())
    d2_keys = set(d2.keys())
    intersect_keys = d1_keys.intersection(d2_keys)
    added = d1_keys - d2_keys
    removed = d2_keys - d1_keys
    modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]}
    same = set(o for o in intersect_keys if d1[o] == d2[o])
    return added, removed, modified, same



# Path: django_test_tools/file_utils.py
# def add_date(filename, **kwargs):
#     """
#     Adds to a filename the current date and time in '%Y%m%d_%H%M' format.
#     For a filename /my/path/myexcel.xlsx the function would return /my/path/myexcel_20170101_1305.xlsx.
#     If the file already exists the function will add seconds to the date to attempt to get a unique name.
# 
#     The function will detect if another file exists with the same name if it exist it will append seconds to the
#     filename. For example if file /my/path/myexcel_20170101_1305.xlsx alread exist thte function will return
#     /my/path/myexcel_20170101_130521.xlsx.
# 
#     :param filename: string with fullpath to file or just the filename
#     :param kwargs: dictionary. date_position: suffix or preffix, extension: string to replace extension
#     :return: string with full path string including the date and time
#     """
#     current_datetime = timezone.localtime(timezone.now()).strftime('%Y%m%d_%H%M%S')
#     new_filename_data = dict()
#     suffix_template = '{path}{separator}{filename_with_out_extension}_{datetime}.{extension}'
#     prefix_template = '{path}{separator}{datetime}_{filename_with_out_extension}.{extension}'
#     if '/' in filename and '\\' in filename:
#         raise ValueError('Filename %s contains both / and \\ separators' % filename)
#     if '\\' in filename:
#         path_parts = filename.split('\\')
#         file = path_parts[-1]
#         path = '\\'.join(path_parts[:-1])
#         separator = '\\'
#     elif '/' in filename:
#         path_parts = filename.split('/')
#         file = path_parts[-1]
#         path = '/'.join(path_parts[:-1])
#         separator = '/'
#     else:
#         file = filename
#         path = ''
#         separator = ''
# 
#     new_filename_data['path'] = path
#     parts = file.split('.')
#     if kwargs.get('extension', None) is not None:
#         new_filename_data['extension'] = kwargs['extension']
#     else:
#         if len(parts) > 1:
#             new_filename_data['extension'] = parts[-1]
#         else:
#             new_filename_data['extension'] = ''
# 
#     new_filename_data['separator'] = separator
#     if new_filename_data['extension'] == '':
#         new_filename_data['filename_with_out_extension'] = parts[0]
#     else:
#         new_filename_data['filename_with_out_extension'] = '.'.join(parts[:-1])
#     new_filename_data['datetime'] = current_datetime[:-2]  # Seconds are stripped
# 
#     date_position = kwargs.get('date_position', 'suffix')
#     if date_position == 'suffix':
#         new_filename = suffix_template.format(**new_filename_data)
#         if os.path.exists(new_filename):
#             new_filename_data['datetime'] = current_datetime
#             new_filename = suffix_template.format(**new_filename_data)
#         if new_filename_data['extension'] == '':
#             new_filename = new_filename[:-1]
#     else:
#         new_filename = prefix_template.format(**new_filename_data)
#         if os.path.exists(new_filename):
#             new_filename_data['datetime'] = current_datetime
#             new_filename = prefix_template.format(**new_filename_data)
#         if new_filename_data['extension'] == '':
#             new_filename = new_filename[:-1]
# 
#     return new_filename


# Path: django_test_tools/utils.py
import functools
import json
import os
import re
import time
import warnings
import pytz
from datetime import datetime, date, timedelta
from django.conf import settings
from django.utils import timezone
from openpyxl.compat import deprecated
from .file_utils import add_date




__author__ = 'lberrocal'


def versiontuple(v):
    return tuple(map(int, (v.split("."))))

def dict_compare(d1, d2):
    d1_keys = set(d1.keys())
    d2_keys = set(d2.keys())
    intersect_keys = d1_keys.intersection(d2_keys)
    added = d1_keys - d2_keys
    removed = d2_keys - d1_keys
    modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]}
    same = set(o for o in intersect_keys if d1[o] == d2[o])
    return added, removed, modified, same



first_cap_re = re.compile('(.)([A-Z][a-z]+)')