utils.py 4.44 KB
Newer Older
David Trattnig's avatar
David Trattnig committed
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

#
#  Aura Engine
#
#  Playout Daemon for autoradio project
#
#
#  Copyright (C) 2020 David Trattnig <david.trattnig@subsquare.at>
#
#  This file is part of engine.
#
#  engine is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  engine is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with engine. If not, see <http://www.gnu.org/licenses/>.
#

# Meta
__version__ = '0.0.1'
__license__ = "GNU General Public License (GPL) Version 3"
__version_info__ = (0, 0, 1)
__author__ = 'David Trattnig <david.trattnig@subsquare.at>'


import datetime
import time

36
37
from enum import Enum
from modules.base.enum import Channel, ChannelType
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
69
70
71
72
73
74



class EngineUtil:
    """
    A class for Engine utilities.
    """

    @staticmethod
    def get_channel_type(uri):
        """
        Returns the channel type, depending on the passed URI and source.

        Args:
            uri (String):   The URI of the source


        """
        if uri.startswith("http"):
            return ChannelType.STREAM
        if uri.startswith("pool") or uri.startswith("playlist") or uri.startswith("file"):
            return ChannelType.FILESYSTEM
        if uri.startswith("live") or uri.startswith("linein"):
            return ChannelType.LIVE

            # FIXME Mix of channels and channel-types!!!
            # if source == "0":
            #     return Channel.LIVE_0
            # elif source == "1":
            #     return Channel.LIVE_1
            # elif source == "2":
            #     return Channel.LIVE_2
            # elif source == "3":
            #     return Channel.LIVE_3
            # elif source == "4":
            #     return Channel.LIVE_4

David Trattnig's avatar
David Trattnig committed
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    @staticmethod
    def lqs_annotate_cuein(uri, cue_in):
        """
        Wraps the given URI with a Liquidsoap Cue In annotation.

        Args:
            uri (String):   The path to the audio source
            cue_in (Float): The value in seconds wher the cue in should start
        
        Returns:
            (String):       The annotated URI
        """
        if cue_in > 0.0:
            uri = "annotate:liq_cue_in=\"%s\":%s" % (str(cue_in), uri)
        return uri

David Trattnig's avatar
David Trattnig committed
92
93
94

class SimpleUtil:
    """
95
    A container class for simple utility methods.
David Trattnig's avatar
David Trattnig committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    """


    @staticmethod
    def fmt_time(timestamp):
        """
        Formats a UNIX timestamp to a String displaying time in the format '%H:%M:%S'.

        Args:
            (Integer) timestamp:    Unix epoch

        Returns:
            (String): Displaying the time
        """
        return datetime.datetime.fromtimestamp(timestamp).strftime('%H:%M:%S')


    @staticmethod
    def timestamp(date_and_time=datetime.datetime.now()):
        """
        Transforms the given `datetime` into a UNIX epoch timestamp.
        If no parameter is passed, the current timestamp is returned.
        
        Args:
            (Datetime) date_and_time:    the date and time to transform.

        Returns: 
            (Integer): timestamp in seconds.
        """
        return time.mktime(date_and_time.timetuple())
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141


    @staticmethod
    def strike(text):
        """
        Creates a strikethrough version of the given text.

        Args:
            (String) text:  the text to strike.

        Returns:
            (String):       the striked text.
        """
        result = ""
        for c in text:
            result += c + '\u0336'
142
143
144
        return result


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    @staticmethod
    def red(text):
        """
        Creates a red version of the given text.
        """
        return TerminalColors.Red + text + TerminalColors.ENDC


    @staticmethod
    def pink(text):
        """
        Creates a red version of the given text.
        """
        return TerminalColors.PINK.value + text + TerminalColors.ENDC.value



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class TerminalColors(Enum):
    """
    Colors for formatting terminal output.
    """

    HEADER = "\033[95m"

    RED = "\033[31m"
    GREEN = "\033[32m"
    ORANGE = "\033[33m"
    BLUE = "\033[34m"
    PINK = "\033[35m"
    CYAN = "\033[36m"

    WARNING = "\033[31m"
    FAIL = "\033[41m"

    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"

    ENDC = "\033[0m"