utils.py 7.19 KB
Newer Older
David Trattnig's avatar
David Trattnig committed
1
#
David Trattnig's avatar
David Trattnig committed
2
# Aura Engine (https://gitlab.servus.at/aura/engine)
David Trattnig's avatar
David Trattnig committed
3
#
David Trattnig's avatar
David Trattnig committed
4
# Copyright (C) 2017-2020 - The Aura Engine Team.
David Trattnig's avatar
David Trattnig committed
5
#
David Trattnig's avatar
David Trattnig committed
6
7
8
9
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
David Trattnig's avatar
David Trattnig committed
10
#
David Trattnig's avatar
David Trattnig committed
11
12
13
14
# This program 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 Affero General Public License for more details.
David Trattnig's avatar
David Trattnig committed
15
#
David Trattnig's avatar
David Trattnig committed
16
17
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
David Trattnig's avatar
David Trattnig committed
18
19
20
21
22


import datetime
import time

23
24
from enum import Enum
from modules.base.enum import Channel, ChannelType
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39



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
40
41
        if uri.startswith("https"):
            return ChannelType.HTTPS
42
        if uri.startswith("http"):
David Trattnig's avatar
David Trattnig committed
43
            return ChannelType.HTTP
44
45
        if uri.startswith("pool") or uri.startswith("playlist") or uri.startswith("file"):
            return ChannelType.FILESYSTEM
46
        if uri.startswith("line://"):
47
48
            return ChannelType.LIVE

David Trattnig's avatar
David Trattnig committed
49

50
51
52
53
54
55
    @staticmethod
    def get_entries_string(entries):
        """
        Returns a list of entries as String for logging purposes.
        """
        s = ""
56
57
58
59
60
61
        if isinstance(entries, list):
            for entry in entries:
                s += str(entry)
                if entry != entries[-1]: s += ", "
        else:
            s = str(entries)
62
63
64
        return s


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    @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
81

David Trattnig's avatar
David Trattnig committed
82
83
84
85
86
87
88
89
90
91
92
93
    @staticmethod
    def engine_info(component, version):
        """
        Prints the engine logo and version info.
        """
        return """\n
             █████╗ ██╗   ██╗██████╗  █████╗     ███████╗███╗   ██╗ ██████╗ ██╗███╗   ██╗███████╗
            ██╔══██╗██║   ██║██╔══██╗██╔══██╗    ██╔════╝████╗  ██║██╔════╝ ██║████╗  ██║██╔════╝
            ███████║██║   ██║██████╔╝███████║    █████╗  ██╔██╗ ██║██║  ███╗██║██╔██╗ ██║█████╗  
            ██╔══██║██║   ██║██╔══██╗██╔══██║    ██╔══╝  ██║╚██╗██║██║   ██║██║██║╚██╗██║██╔══╝  
            ██║  ██║╚██████╔╝██║  ██║██║  ██║    ███████╗██║ ╚████║╚██████╔╝██║██║ ╚████║███████╗
            ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝    ╚══════╝╚═╝  ╚═══╝ ╚═════╝ ╚═╝╚═╝  ╚═══╝╚══════╝
94
95
            %s v%s - Ready to play!
        \n""" % (component, version)                                                                           
David Trattnig's avatar
David Trattnig committed
96
97
98



David Trattnig's avatar
David Trattnig committed
99
100
class SimpleUtil:
    """
101
    A container class for simple utility methods.
David Trattnig's avatar
David Trattnig committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    """


    @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
120
    def timestamp(date_and_time=None):
David Trattnig's avatar
David Trattnig committed
121
122
123
124
125
126
127
128
129
130
        """
        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.
        """
131
132
        if not date_and_time:
            date_and_time = datetime.datetime.now()
David Trattnig's avatar
David Trattnig committed
133
        return time.mktime(date_and_time.timetuple())
134
135
136
137
138
139
140
141
142
143
144
145
146
147


    @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
148
        for c in str(text):
David Trattnig's avatar
David Trattnig committed
149
            result += c + TerminalColors.STRIKE.value
150
151
        return result

David Trattnig's avatar
David Trattnig committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    @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
172

173
174
175
176
177
    @staticmethod
    def red(text):
        """
        Creates a red version of the given text.
        """
178
        return TerminalColors.RED.value + text + TerminalColors.ENDC.value
179
180
181
182
183
184
185
186

    @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
187
188
189
190
191
192
    @staticmethod
    def yellow(text):
        """
        Creates a yellow version of the given text.
        """
        return TerminalColors.YELLOW.value + text + TerminalColors.ENDC.value
193

194
195
196
197
198
199
200
    @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
201
202
203
204
205
206
    @staticmethod
    def cyan(text):
        """
        Creates a cyan version of the given text.
        """
        return TerminalColors.CYAN.value + text + TerminalColors.ENDC.value
207
208
        

209

210
211
212
213
214
215
216
217
218
class TerminalColors(Enum):
    """
    Colors for formatting terminal output.
    """

    HEADER = "\033[95m"

    RED = "\033[31m"
    GREEN = "\033[32m"
David Trattnig's avatar
David Trattnig committed
219
    YELLOW = "\033[33m"
220
221
222
223
224
225
226
227
228
    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
229
    STRIKE = "\u0336"
230
231

    ENDC = "\033[0m"