import sys

# This module contains a variety of wrapper functions for miscellaneous test 
# actions. This module may be deleted if the functions within all have a better
# location.

def wait_for_openstack_poller(cloud_name, item_flag, item_name, wait=sys.maxsize, output=False):

    import subprocess
    from time import sleep

    out = subprocess.DEVNULL
    if output:
        out = None
    count = 0
    while subprocess.run(['cloudscheduler', 'cloud', 'update', '-cn', cloud_name, item_flag, item_name], stdout=out).returncode != 0 and count < wait:
        count += 1
        if output:
            print("Error connecting to the cloud. This may happen several times. Retrying...")
        sleep(15)
    
def misc_file_full_path(filename):
    import os

    path = os.path.abspath('web_tests/misc_files/' + filename)
    return path

def cumulative_days(month, year):
    feb = 28
    if year%4 == 0 and year%100 !=0:
        feb = 29
    days = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    sum = 0
    for number in days[:month]:
        sum += number
    return sum

def time_before(difference, units):
    import datetime

    now = datetime.datetime.now()
    time_difference = None
    if units == 'minutes':
        time_difference = datetime.timedelta(minutes=difference)
    if units == 'hours':
        time_difference = datetime.timedelta(hours=difference)
    if units == 'days':
        time_difference = datetime.timedelta(days=difference)
    if units == 'months':
        time_difference = datetime.timedelta(days=int(difference*30.4))
    if units == 'years':
        time_difference = datetime.timedelta(days=difference*365)

    before = now - time_difference
    return before

def parse_datetime(datetime_string):
    import datetime

    start_time = None
    try:
        start_time = datetime.datetime.strptime(datetime_string, '%H:%M:%S%b %d, %Y')
        print("H:M:Sb d, Y")
    except ValueError:
        try:
            start_time = datetime.datetime.strptime(datetime_string, '%H:%M%b %d, %Y')
            print("H:Mb d, Y")
        except ValueError:
            try:
                start_time = datetime.datetime.strptime(datetime_string, '%b %d, %Y')
                print("b d, Y")
            except ValueError:
                try:
                    start_time = datetime.datetime.strptime(datetime_string, '%b %d%Y')
                    print("b dY")
                except ValueError:
                    start_time = datetime.datetime.strptime(datetime_string, '%b %Y')
                    print("b Y")

    return start_time

def round_datetime(dt, round, forward):
    import datetime

    round = int(round)
    subtract = datetime.timedelta(seconds=(dt.hour*3600 + dt.minute*60 + dt.second)%round, microseconds=dt.microsecond)
    dt = dt - subtract
    if forward and (round >= 60 or subtract.seconds > 6):
        dt += datetime.timedelta(seconds=round)
    return dt

def round_date(dt, round, forward):
    import datetime

    subtract = datetime.timedelta(days=(dt.year*365 + cumulative_days(dt.month, dt.year) + dt.day) % round)
    dt = dt - subtract
    if forward:
        dt += datetime.timedelta(days=round)
    if round >= 7:
        if dt.weekday() < 3:
            dt -= datetime.timedelta(dt.weekday())
        else:
            dt += datetime.timedelta(7 - dt.weekday())
    return dt

@@ -3175,24 +3175,42 @@
 days=round)%0A
+        print(dt)%0A
     if round
@@ -3216,16 +3216,44 @@
 d %3E= 7:%0A
+        print(dt.weekday())%0A
         
@@ -3270,17 +3270,17 @@
 day() %3C 
-3
+2
 :%0A      
@@ -3311,18 +3311,26 @@
 edelta(d
-t.
+ays=dt.iso
 weekday(
@@ -3387,15 +3387,24 @@
 lta(
+days=(
 7 - dt.
+iso
 week
@@ -3405,24 +3405,25 @@
 soweekday())
+)
 %0A    return 

fix day tests