# -*- coding: utf-8 -*-
"""
Display status of a TCP port on a given host.

Configuration parameters:
    cache_timeout: how often to run the check (default 10)
    format: what to display on the bar (default '{host}:{port} {state}')
    host: check if tcp port on host is up (default 'localhost')
    port: the tcp port (default 22)

Format placeholders:
    {state} port state ('DOWN' or 'UP')

Color options:
    color_down: Unavailable, default color_bad
    color_up: Available, default color_good

@author obb, Moritz Lüdecke
"""

import socket


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    format = '{host}:{port} {state}'
    host = 'localhost'
    port = 22

    def check_tcp(self):
        response = {'cached_until': self.py3.time_in(self.cache_timeout)}

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((self.host, self.port))

        if result == 0:
            state = 'UP'
            response['color'] = self.py3.COLOR_UP or self.py3.COLOR_GOOD
        else:
            state = 'DOWN'
            response['color'] = self.py3.COLOR_DOWN or self.py3.COLOR_BAD

        response['full_text'] = self.py3.safe_format(self.format,
                                                     {'state': state})
        return response


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)

@@ -117,86 +117,96 @@
 ut: 
-how often to run the check (default 10)%0A    format: what to display on the bar
+refresh interval for this module (default 10)%0A    format: display format for this module
  (de
@@ -256,33 +256,12 @@
 eck 
-if tcp port on host is up
+host
  (de
@@ -293,21 +293,26 @@
 rt: 
-the tcp
+check
  port 
+number 
 (def
@@ -319,16 +319,136 @@
 ault 22)
+%0A    string_down: show string when available (default 'UP')%0A    string_up: show string when unavailable (default 'DOWN')
 %0A%0AFormat
@@ -488,25 +488,8 @@
 tate
- ('DOWN' or 'UP')
 %0A%0ACo
@@ -817,487 +817,526 @@
  22%0A
-%0A    def check_tcp(self):%0A        response = %7B'cached_until': self.py3.time_in(self.cache_timeout)%7D%0A%0A        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)%0A        result = sock.connect_ex((self.host, self.port))%0A%0A        if result == 0:%0A            state = 'UP'%0A            response%5B'color'%5D = self.py3.COLOR_UP or self.py3.COLOR_GOOD%0A        else:%0A            state = 'DOWN'%0A            response%5B'color'%5D = self.py3.COLOR_DOWN or self.py3.COLOR_BAD%0A%0A        response%5B
+    string_down = 'DOWN'%0A    string_up = 'UP'%0A%0A    def post_config_hook(self):%0A        self.color_on = self.py3.COLOR_UP or self.py3.COLOR_GOOD%0A        self.color_off = self.py3.COLOR_DOWN or self.py3.COLOR_BAD%0A%0A    def check_tcp(self):%0A        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)%0A        result = sock.connect_ex((self.host, self.port))%0A        state = self.string_down if result else self.string_up%0A%0A        return %7B%0A            'cached_until': self.py3.time_in(self.cache_timeout),%0A            
 'ful
@@ -1346,11 +1346,9 @@
 ext'
-%5D =
+:
  sel
@@ -1381,61 +1381,8 @@
 mat,
-%0A                                                    
  %7B's
@@ -1395,25 +1395,27 @@
  state%7D)
-%0A
+,%0A 
         
 return r
@@ -1410,23 +1410,73 @@
     
-return response
+   'color': self.color_off if result else self.color_on%0A        %7D
 %0A%0A%0Ai

remove hardcoded strings