# -*- coding: utf-8 -*-

# Copyright (c) 2015-2016 Ericsson AB
#
# 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 os
from functools import partial

from calvin.utilities.calvinlogger import get_logger

_log = get_logger(__name__)


def get_local_ip():
    import socket
    return [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close())
            for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]


def enum(*sequential, **named):
    enums = dict(zip(sequential, range(len(sequential))), **named)
    reverse = dict((value, key) for key, value in enums.iteritems())
    enums['reverse_mapping'] = reverse
    return type('Enum', (), enums)


def ensure_path(d):
    if not os.path.exists(d):
        os.makedirs(d)


def ensure_dir(f):
    d = os.path.dirname(f)
    if not os.path.exists(d):
        os.makedirs(d)


def uniq_list(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]


# Generate async_* versions of all functions with async argument set to True
funcs = {}
g = None
f = None

for g, f in globals().iteritems():
    if hasattr(f, '__call__') and ((hasattr(f, '__code__') and 'async' in f.__code__.co_varnames) or f.__name__ == 'peer_setup'):
        funcs['async_' + g] = partial(f, async=True)
globals().update(funcs)
del funcs
del g
del f

@@ -1466,377 +1466,4 @@
 ))%5D%0A
-%0A%0A# Generate async_* versions of all functions with async argument set to True%0Afuncs = %7B%7D%0Ag = None%0Af = None%0A%0Afor g, f in globals().iteritems():%0A    if hasattr(f, '__call__') and ((hasattr(f, '__code__') and 'async' in f.__code__.co_varnames) or f.__name__ == 'peer_setup'):%0A        funcs%5B'async_' + g%5D = partial(f, async=True)%0Aglobals().update(funcs)%0Adel funcs%0Adel g%0Adel f%0A

remove async generation from utils