# 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
# def datetime_to_local_time(date_time):
#     """
#     Converts a naive date to a time zoned date based in hte setting.TIME_ZONE variable. If the date has already a time zone
#     it will localize the date.
#     :param date_time: <date> or <datetime> to be localized
#     :return: localized non naive datetime
#     """
#     if isinstance(date_time, date) and not isinstance(date_time, datetime):
#         date_time = datetime.combine(date_time, datetime.min.time())
# 
#     is_naive = date_time.tzinfo is None or date_time.tzinfo.utcoffset(date_time) is None
#     time_zone = pytz.timezone(settings.TIME_ZONE)
#     if is_naive:
#         return time_zone.localize(date_time)
#     else:
#         return date_time.astimezone(time_zone)


import pytz
import os
import subprocess
import logging
from datetime import datetime
from django.conf import settings
from django.core.management import BaseCommand
from openpyxl import Workbook
from ...file_utils import add_date
from ...utils import datetime_to_local_time
        #                     help="List data",
        #                     )

        parser.add_argument("--format",
                            dest="format",
                            help="Git format",
                            default=None,
                            )

        parser.add_argument('-f', "--filename",
                            dest="filename",
                            help="Output filename",
                            default=None,
                            )

        # parser.add_argument("-u", "--username",
        #                 dest="usernames",
        #                 help="LDAP usernames for employees",
        #                 nargs='+',
        #                 )

    def handle(self, *args, **options):
        headers = ['Hash', 'Email', 'Date', 'Description']
        if options.get('format') is None:
            git_format = '%h|%ae|%ai|%s'
        else:
            git_format = options.get('format')

        if options.get('filename') is None:
            filepath = os.path.join(settings.TEST_OUTPUT_PATH, 'git_report.xlsx')

# 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
# def datetime_to_local_time(date_time):
#     """
#     Converts a naive date to a time zoned date based in hte setting.TIME_ZONE variable. If the date has already a time zone
#     it will localize the date.
#     :param date_time: <date> or <datetime> to be localized
#     :return: localized non naive datetime
#     """
#     if isinstance(date_time, date) and not isinstance(date_time, datetime):
#         date_time = datetime.combine(date_time, datetime.min.time())
# 
#     is_naive = date_time.tzinfo is None or date_time.tzinfo.utcoffset(date_time) is None
#     time_zone = pytz.timezone(settings.TIME_ZONE)
#     if is_naive:
#         return time_zone.localize(date_time)
#     else:
#         return date_time.astimezone(time_zone)


# Path: django_test_tools/management/commands/git_report.py
import pytz
import os
import subprocess
import logging
from datetime import datetime
from django.conf import settings
from django.core.management import BaseCommand
from openpyxl import Workbook
from ...file_utils import add_date
from ...utils import datetime_to_local_time

        #                     help="List data",
        #                     )

        parser.add_argument("--format",
                            dest="format",
                            help="Git format",
                            default=None,
                            )

        parser.add_argument('-f', "--filename",
                            dest="filename",
                            help="Output filename",
                            default=None,
                            )

        # parser.add_argument("-u", "--username",
        #                 dest="usernames",
        #                 help="LDAP usernames for employees",
        #                 nargs='+',
        #                 )

    def handle(self, *args, **options):
        headers = ['Hash', 'Email', 'Date', 'Description']
        if options.get('format') is None:
            git_format = '%h|%ae|%ai|%s'
        else:
            git_format = options.get('format')

        if options.get('filename') is None:
            filepath = os.path.join(settings.TEST_OUTPUT_PATH, 'git_report.xlsx')

            filename = add_date(filepath)