from i3pystatus import IntervalModule
from i3pystatus.core.command import run_through_shell

__author__ = 'facetoe'


class OpenVPN(IntervalModule):
    """
    Monitor OpenVPN connections.
    Currently only supports systems that use Systemd.

    Formatters:

    * {vpn_name} — Same as setting.
    * {status} — Unicode up or down symbol.
    * {output} — Output of status_command.
    * {label} — Label for this connection, if defined.

    """

    colour_up = "#00ff00"
    colour_down = "#FF0000"
    format = "{label} {vpn_name} {status}"
    status_command = "bash -c \"systemctl show openvpn@%(vpn_name)s | grep -oP 'ActiveState=\K(\w+)'\""

    label = ''
    vpn_name = ''

    settings = (
        ("format", "Format string"),
        ("colour_up", "VPN is up"),
        ("colour_down", "VPN is down"),
        ("vpn_name", "Name of VPN"),
        ("label", "Set a label for this connection")
    )

    def init(self):
        if not self.vpn_name:
            raise Exception("vpn_name is required")

    def run(self):
        command_result = run_through_shell(self.status_command % {'vpn_name': self.vpn_name}, enable_shell=True)
        output = command_result.out.strip()

        if output == 'active':
            color = self.colour_up
            status = '▲'
        else:
            color = self.colour_down
            status = '▼'

        vpn_name = self.vpn_name
        label = self.label

        self.output = {
            "full_text": self.format.format(**locals()),
            'color': color,
        }

@@ -493,24 +493,66 @@
 = %22#FF0000%22%0A
+    status_up = '%E2%96%B2'%0A    status_down = '%E2%96%BC'%0A
     format =
@@ -553,24 +553,16 @@
 rmat = %22
-%7Blabel%7D 
 %7Bvpn_nam
@@ -857,87 +857,142 @@
   (%22
-vpn_name%22, %22Name of VPN%22),%0A        (%22label%22, %22Set a label for this connection
+status_down%22, %22Symbol to display when down%22),%0A        (%22status_up%22, %22Symbol to display when up%22),%0A        (%22vpn_name%22, %22Name of VPN
 %22)
+,
 %0A   
@@ -1362,19 +1362,30 @@
 tatus = 
-'%E2%96%B2'
+self.status_up
 %0A       
@@ -1449,19 +1449,32 @@
 tatus = 
-'%E2%96%BC'
+self.status_down
 %0A%0A      

Make symbol configurable