summaryrefslogtreecommitdiffstats
path: root/py3status/modules/external_script.py
blob: 4b3f2e8bf8a920adcdb8d7116a1ef843d8d4a83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""
Display output of given script.

Display output of any executable script set by 'script_path'.
Pay attention. The output must be one liner, or will break your i3status !
The script should not have any parameters, but it could work.

Configuration parameters:
    - cache_timeout : how often we refresh this module in seconds
    - color         : color of printed text
    - format        : see placeholders below
    - script_path   : script you want to show output of (compulsory)

Format of status string placeholders:
    {output} - output of script given by "script_path"

i3status.conf example:

external_script {
    color = "#00FF00"
    format = "my name is {output}"
    script_path = "/usr/bin/whoami"

@author frimdo ztracenastopa@centrum.cz
"""

import subprocess
from time import time


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 15
    color = None
    format = '{output}'
    script_path = None

    def external_script(self, i3s_output_list, i3s_config):
        if self.script_path:
            return_value = subprocess.check_output(self.script_path,
                                                   shell=True,
                                                   universal_newlines=True)
            response = {
                'cached_until': time() + self.cache_timeout,
                'color': self.color,
                'full_text': self.format.format(output=return_value.rstrip())
            }
        else:
            response = {
                'cached_until': time() + self.cache_timeout,
                'full_text': ''
            }
        return response


if __name__ == "__main__":
    from time import sleep
    x = Py3status()
    config = {
        'color_good': '#00FF00',
        'color_bad': '#FF0000',
    }
    while True:
        print(x.external_script([], config))
        sleep(1)