# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# Copyright 2018-2020 BigML
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import time
import json
import os
from datetime import datetime, timedelta
from world import world
from nose.tools import eq_, assert_less

from bigml.api import HTTP_CREATED
from bigml.api import HTTP_ACCEPTED
from bigml.api import FINISHED
from bigml.api import FAULTY
from bigml.api import get_status

from read_pca_steps import i_get_the_pca


#@step(r'the pca name is "(.*)"')
def i_check_pca_name(step, name):
    pca_name = world.pca['name']
    eq_(name, pca_name)

#@step(r'I create a PCA from a dataset$')
def i_create_a_pca_from_dataset(step):
    dataset = world.dataset.get('resource')
    resource = world.api.create_pca(dataset, {'name': 'new PCA'})
    world.status = resource['code']
    eq_(world.status, HTTP_CREATED)
    world.location = resource['location']
    world.pca = resource['object']
    world.pcas.append(resource['resource'])


#@step(r'I create a PCA from a dataset$')
def i_create_a_pca_with_params(step, params):
    params = json.loads(params)
    dataset = world.dataset.get('resource')
    resource = world.api.create_pca(dataset, params)
    world.status = resource['code']
    eq_(world.status, HTTP_CREATED)
    world.location = resource['location']
    world.pca = resource['object']
    world.pcas.append(resource['resource'])

def i_create_a_pca(step):
    i_create_a_pca_from_dataset(step)


#@step(r'I update the PCA name to "(.*)"$')
def i_update_pca_name(step, name):
    resource = world.api.update_pca(world.pca['resource'],
                                    {'name': name})
    world.status = resource['code']
    eq_(world.status, HTTP_ACCEPTED)
    world.location = resource['location']
    world.pca = resource['object']


#@step(r'I wait until the PCA status code is either (\d) or (-\d) less than (\d+)')
def wait_until_pca_status_code_is(step, code1, code2, secs):
    start = datetime.utcnow()
    delta = int(secs) * world.delta
    pca_id = world.pca['resource']
    i_get_the_pca(step, pca_id)
    status = get_status(world.pca)
    while (status['code'] != int(code1) and
           status['code'] != int(code2)):
           time.sleep(3)
           assert_less(datetime.utcnow() - start, timedelta(seconds=delta))
           i_get_the_pca(step, pca_id)
           status = get_status(world.pca)
    eq_(status['code'], int(code1))


#@step(r'I wait until the PCA is ready less than (\d+)')
def the_pca_is_finished_in_less_than(step, secs):
    wait_until_pca_status_code_is(step, FINISHED, FAULTY, secs)

[((1076, 1095), 'nose.tools.eq_', 'eq_', (['name', 'pca_name'], {}), '(name, pca_name)\n', (1079, 1095), False, 'from nose.tools import eq_, assert_less\n'), ((1192, 1221), 'world.world.dataset.get', 'world.dataset.get', (['"""resource"""'], {}), "('resource')\n", (1209, 1221), False, 'from world import world\n'), ((1237, 1287), 'world.world.api.create_pca', 'world.api.create_pca', (['dataset', "{'name': 'new PCA'}"], {}), "(dataset, {'name': 'new PCA'})\n", (1257, 1287), False, 'from world import world\n'), ((1328, 1359), 'nose.tools.eq_', 'eq_', (['world.status', 'HTTP_CREATED'], {}), '(world.status, HTTP_CREATED)\n', (1331, 1359), False, 'from nose.tools import eq_, assert_less\n'), ((1441, 1480), 'world.world.pcas.append', 'world.pcas.append', (["resource['resource']"], {}), "(resource['resource'])\n", (1458, 1480), False, 'from world import world\n'), ((1584, 1602), 'json.loads', 'json.loads', (['params'], {}), '(params)\n', (1594, 1602), False, 'import json\n'), ((1617, 1646), 'world.world.dataset.get', 'world.dataset.get', (['"""resource"""'], {}), "('resource')\n", (1634, 1646), False, 'from world import world\n'), ((1662, 1699), 'world.world.api.create_pca', 'world.api.create_pca', (['dataset', 'params'], {}), '(dataset, params)\n', (1682, 1699), False, 'from world import world\n'), ((1740, 1771), 'nose.tools.eq_', 'eq_', (['world.status', 'HTTP_CREATED'], {}), '(world.status, HTTP_CREATED)\n', (1743, 1771), False, 'from nose.tools import eq_, assert_less\n'), ((1853, 1892), 'world.world.pcas.append', 'world.pcas.append', (["resource['resource']"], {}), "(resource['resource'])\n", (1870, 1892), False, 'from world import world\n'), ((2054, 2113), 'world.world.api.update_pca', 'world.api.update_pca', (["world.pca['resource']", "{'name': name}"], {}), "(world.pca['resource'], {'name': name})\n", (2074, 2113), False, 'from world import world\n'), ((2190, 2222), 'nose.tools.eq_', 'eq_', (['world.status', 'HTTP_ACCEPTED'], {}), '(world.status, HTTP_ACCEPTED)\n', (2193, 2222), False, 'from nose.tools import eq_, assert_less\n'), ((2459, 2476), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (2474, 2476), False, 'from datetime import datetime, timedelta\n'), ((2552, 2579), 'read_pca_steps.i_get_the_pca', 'i_get_the_pca', (['step', 'pca_id'], {}), '(step, pca_id)\n', (2565, 2579), False, 'from read_pca_steps import i_get_the_pca\n'), ((2593, 2614), 'bigml.api.get_status', 'get_status', (['world.pca'], {}), '(world.pca)\n', (2603, 2614), False, 'from bigml.api import get_status\n'), ((2712, 2725), 'time.sleep', 'time.sleep', (['(3)'], {}), '(3)\n', (2722, 2725), False, 'import time\n'), ((2813, 2840), 'read_pca_steps.i_get_the_pca', 'i_get_the_pca', (['step', 'pca_id'], {}), '(step, pca_id)\n', (2826, 2840), False, 'from read_pca_steps import i_get_the_pca\n'), ((2861, 2882), 'bigml.api.get_status', 'get_status', (['world.pca'], {}), '(world.pca)\n', (2871, 2882), False, 'from bigml.api import get_status\n'), ((2776, 2800), 'datetime.timedelta', 'timedelta', ([], {'seconds': 'delta'}), '(seconds=delta)\n', (2785, 2800), False, 'from datetime import datetime, timedelta\n'), ((2749, 2766), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (2764, 2766), False, 'from datetime import datetime, timedelta\n')]