# -*- coding: utf-8 -*-
"""Template Helpers used in workbox"""

import logging
import socket
from datetime import datetime
from markupsafe import Markup
import psutil
import tg

log = logging.getLogger(__name__)


def current_year():
    """ Return current year. """

    now = datetime.now()
    return now.strftime('%Y')

def is_docker_enabled():
    """ Detect if docker service is started. """

    for proc in psutil.process_iter():
        if 'docker' in proc.name():
            return True
    return False

def get_server_load_value():
    """ Get server load value. """

    return psutil.virtual_memory().percent

def get_free_port():
    """ Find and returns free port number. """

    soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    soc.bind(("", 0))
    free_port = soc.getsockname()[1]
    soc.close()
    return free_port

def get_vagrantfiles_base_folder():
    """ Return base folder for vagrantfiles. """

    return tg.config.get('workbox.vagrantfiles.basefolder')

def get_hostname():
    """ Return hostname. """

    return tg.config.get('workbox.hostname')

try:
    from webhelpers2 import date, html, number, misc, text
except SyntaxError:
    log.error("WebHelpers2 helpers not available with this Python Version")

[((11, 6, 11, 33), 'logging.getLogger', 'logging.getLogger', ({(11, 24, 11, 32): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((17, 10, 17, 24), 'datetime.datetime.now', 'datetime.now', ({}, {}), '()', False, 'from datetime import datetime\n'), ((23, 16, 23, 37), 'psutil.process_iter', 'psutil.process_iter', ({}, {}), '()', False, 'import psutil\n'), ((36, 10, 36, 59), 'socket.socket', 'socket.socket', ({(36, 24, 36, 38): 'socket.AF_INET', (36, 40, 36, 58): 'socket.SOCK_STREAM'}, {}), '(socket.AF_INET, socket.SOCK_STREAM)', False, 'import socket\n'), ((45, 11, 45, 59), 'tg.config.get', 'tg.config.get', ({(45, 25, 45, 58): '"""workbox.vagrantfiles.basefolder"""'}, {}), "('workbox.vagrantfiles.basefolder')", False, 'import tg\n'), ((50, 11, 50, 44), 'tg.config.get', 'tg.config.get', ({(50, 25, 50, 43): '"""workbox.hostname"""'}, {}), "('workbox.hostname')", False, 'import tg\n'), ((31, 11, 31, 34), 'psutil.virtual_memory', 'psutil.virtual_memory', ({}, {}), '()', False, 'import psutil\n')]