utils.py 7.21 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

#
#  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/>.
#


import datetime
import time

30
31
from enum import Enum
from modules.base.enum import Channel, ChannelType
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46



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
        """
David Trattnig's avatar
David Trattnig committed
47
48
        if uri.startswith("https"):
            return ChannelType.HTTPS
49
        if uri.startswith("http"):
David Trattnig's avatar
David Trattnig committed
50
            return ChannelType.HTTP
51
52
        if uri.startswith("pool") or uri.startswith("playlist") or uri.startswith("file"):
            return ChannelType.FILESYSTEM
53
        if uri.startswith("line://"):
54
55
            return ChannelType.LIVE

David Trattnig's avatar
David Trattnig committed
56

57
58
59
60
61
62
    @staticmethod
    def get_entries_string(entries):
        """
        Returns a list of entries as String for logging purposes.
        """
        s = ""
63
64
65
66
67
68
        if isinstance(entries, list):
            for entry in entries:
                s += str(entry)
                if entry != entries[-1]: s += ", "
        else:
            s = str(entries)
69
70
71
        return s


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    @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
88

David Trattnig's avatar
David Trattnig committed
89
90
91
92
93
94
95
96
97
98
99
100
    @staticmethod
    def engine_info(component, version):
        """
        Prints the engine logo and version info.
        """
        return """\n
             █████╗ ██╗   ██╗██████╗  █████╗     ███████╗███╗   ██╗ ██████╗ ██╗███╗   ██╗███████╗
            ██╔══██╗██║   ██║██╔══██╗██╔══██╗    ██╔════╝████╗  ██║██╔════╝ ██║████╗  ██║██╔════╝
            ███████║██║   ██║██████╔╝███████║    █████╗  ██╔██╗ ██║██║  ███╗██║██╔██╗ ██║█████╗  
            ██╔══██║██║   ██║██╔══██╗██╔══██║    ██╔══╝  ██║╚██╗██║██║   ██║██║██║╚██╗██║██╔══╝  
            ██║  ██║╚██████╔╝██║  ██║██║  ██║    ███████╗██║ ╚████║╚██████╔╝██║██║ ╚████║███████╗
            ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝    ╚══════╝╚═╝  ╚═══╝ ╚═════╝ ╚═╝╚═╝  ╚═══╝╚══════╝
101
102
            %s v%s - Ready to play!
        \n""" % (component, version)                                                                           
David Trattnig's avatar
David Trattnig committed
103
104
105



David Trattnig's avatar
David Trattnig committed
106
107
class SimpleUtil:
    """
108
    A container class for simple utility methods.
David Trattnig's avatar
David Trattnig committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    """


    @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
127
    def timestamp(date_and_time=None):
David Trattnig's avatar
David Trattnig committed
128
129
130
131
132
133
134
135
136
137
        """
        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.
        """
138
139
        if not date_and_time:
            date_and_time = datetime.datetime.now()
David Trattnig's avatar
David Trattnig committed
140
        return time.mktime(date_and_time.timetuple())
141
142
143
144
145
146
147
148
149
150
151
152
153
154


    @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 = ""
David Trattnig's avatar
David Trattnig committed
155
        for c in str(text):
David Trattnig's avatar
David Trattnig committed
156
            result += c + TerminalColors.STRIKE.value
157
158
        return result

David Trattnig's avatar
David Trattnig committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    @staticmethod
    def bold(text):
        """
        Creates a bold version of the given text.
        """
        return TerminalColors.BOLD.value + text + TerminalColors.ENDC.value

    @staticmethod
    def underline(text):
        """
        Creates a underlined version of the given text.
        """
        return TerminalColors.UNDERLINE.value + text + TerminalColors.ENDC.value

    @staticmethod
    def blue(text):
        """
        Creates a blue version of the given text.
        """
        return TerminalColors.BLUE.value + text + TerminalColors.ENDC.value
179

180
181
182
183
184
    @staticmethod
    def red(text):
        """
        Creates a red version of the given text.
        """
185
        return TerminalColors.RED.value + text + TerminalColors.ENDC.value
186
187
188
189
190
191
192
193

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

David Trattnig's avatar
David Trattnig committed
194
195
196
197
198
199
    @staticmethod
    def yellow(text):
        """
        Creates a yellow version of the given text.
        """
        return TerminalColors.YELLOW.value + text + TerminalColors.ENDC.value
200

201
202
203
204
205
206
207
    @staticmethod
    def green(text):
        """
        Creates a red version of the given text.
        """
        return TerminalColors.GREEN.value + text + TerminalColors.ENDC.value

David Trattnig's avatar
David Trattnig committed
208
209
210
211
212
213
    @staticmethod
    def cyan(text):
        """
        Creates a cyan version of the given text.
        """
        return TerminalColors.CYAN.value + text + TerminalColors.ENDC.value
214
215
        

216

217
218
219
220
221
222
223
224
225
class TerminalColors(Enum):
    """
    Colors for formatting terminal output.
    """

    HEADER = "\033[95m"

    RED = "\033[31m"
    GREEN = "\033[32m"
David Trattnig's avatar
David Trattnig committed
226
    YELLOW = "\033[33m"
227
228
229
230
231
232
233
234
235
    BLUE = "\033[34m"
    PINK = "\033[35m"
    CYAN = "\033[36m"

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

    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"
David Trattnig's avatar
David Trattnig committed
236
    STRIKE = "\u0336"
237
238

    ENDC = "\033[0m"