utils.py 5.09 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
from enum import Enum
David Trattnig's avatar
David Trattnig committed
24

25
                                                                    
David Trattnig's avatar
David Trattnig committed
26

David Trattnig's avatar
David Trattnig committed
27
28
class SimpleUtil:
    """
29
    A container class for simple utility methods.
David Trattnig's avatar
David Trattnig committed
30
31
32
    """


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    @staticmethod
    def clean_dictionary(data):
        """
        Delete keys with the value `None` in a dictionary, recursively.
        This alters the input so you may wish to `copy` the dict first.

        Args:
            data (dict):    The dicationary

        Returns:
            (dict): 
        """
        for key, value in list(data.items()):
            if value is None:
                del data[key]
            elif isinstance(value, dict):
                SimpleUtil.clean_dictionary(value)
        return data


David Trattnig's avatar
David Trattnig committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    @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')


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    @staticmethod
    def nano_to_seconds(nanoseconds):
        """
        Converts nano-seconds to senconds

        Args:
            (Integer) nanoseconds

        Returns:
            (Float): seconds
        """
        return float(nanoseconds / 1000000000) 


    @staticmethod
    def seconds_to_nano(seconds):
        """
        Converts senconds to nano-seconds

        Args:
            (Integer) seconds

        Returns:
            (Float): nanoseconds
        """
        return int(seconds * 1000000000)


David Trattnig's avatar
David Trattnig committed
95
    @staticmethod
96
    def timestamp(date_and_time=None):
David Trattnig's avatar
David Trattnig committed
97
98
99
100
101
102
103
104
105
106
        """
        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.
        """
107
108
        if not date_and_time:
            date_and_time = datetime.datetime.now()
David Trattnig's avatar
David Trattnig committed
109
        return time.mktime(date_and_time.timetuple())
110
111
112
113
114
115
116
117
118
119
120
121
122
123


    @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
124
        for c in str(text):
David Trattnig's avatar
David Trattnig committed
125
            result += c + TerminalColors.STRIKE.value
126
127
        return result

David Trattnig's avatar
David Trattnig committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    @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
148

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

    @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
163
164
165
166
167
168
    @staticmethod
    def yellow(text):
        """
        Creates a yellow version of the given text.
        """
        return TerminalColors.YELLOW.value + text + TerminalColors.ENDC.value
169

170
171
172
173
174
175
176
    @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
177
178
179
180
181
182
    @staticmethod
    def cyan(text):
        """
        Creates a cyan version of the given text.
        """
        return TerminalColors.CYAN.value + text + TerminalColors.ENDC.value
183
184
        

185

186
187
188
189
190
191
192
193
194
class TerminalColors(Enum):
    """
    Colors for formatting terminal output.
    """

    HEADER = "\033[95m"

    RED = "\033[31m"
    GREEN = "\033[32m"
David Trattnig's avatar
David Trattnig committed
195
    YELLOW = "\033[33m"
196
197
198
199
200
201
202
203
204
    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
205
    STRIKE = "\u0336"
206
207

    ENDC = "\033[0m"