annotate libervia/backend/tools/common/utils.py @ 4303:a7ec325246fb

component email-gateway: first draft: Initial implementation of the Email Gateway. This component uses XEP-0100 for registration. Upon registration and subsequent startups, a connection is made to registered IMAP services, and incoming emails (in `INBOX` mailboxes) are immediately forwarded as XMPP messages. In the opposite direction, an SMTP connection is established to send emails on incoming XMPP messages. rel 449
author Goffi <goffi@goffi.org>
date Fri, 06 Sep 2024 18:07:17 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3045
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
3480
7550ae9cfbac Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
3 # Libervia: an XMPP client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3292
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
3045
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 """Misc utils for both backend and frontends"""
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
3265
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
21 import collections.abc
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
22
3524
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
23 size_units = {
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
24 "b": 1,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
25 "kb": 1000,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
26 "mb": 1000**2,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
27 "gb": 1000**3,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
28 "tb": 1000**4,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
29 "pb": 1000**5,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
30 "eb": 1000**6,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
31 "zb": 1000**7,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
32 "yb": 1000**8,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
33 "o": 1,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
34 "ko": 1000,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
35 "mo": 1000**2,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
36 "go": 1000**3,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
37 "to": 1000**4,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
38 "po": 1000**5,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
39 "eo": 1000**6,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
40 "zo": 1000**7,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
41 "yo": 1000**8,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
42 "kib": 1024,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
43 "mib": 1024**2,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
44 "gib": 1024**3,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
45 "tib": 1024**4,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
46 "pib": 1024**5,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
47 "eib": 1024**6,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
48 "zib": 1024**7,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
49 "yib": 1024**8,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
50 "kio": 1024,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
51 "mio": 1024**2,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
52 "gio": 1024**3,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
53 "tio": 1024**4,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
54 "pio": 1024**5,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
55 "eio": 1024**6,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
56 "zio": 1024**7,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
57 "yio": 1024**8,
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
58 }
3265
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
59
3045
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def per_luminance(red, green, blue):
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 """Caculate the perceived luminance of a RGB color
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @param red(int): 0-1 normalized value of red
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 @param green(int): 0-1 normalized value of green
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @param blue(int): 0-1 normalized value of blue
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @return (float): 0-1 value of luminance (<0.5 is dark, else it's light)
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # cf. https://stackoverflow.com/a/1855903, thanks Gacek
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
6af44ca3beac tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return 0.299 * red + 0.587 * green + 0.114 * blue
3265
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
72
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
73
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
74 def recursive_update(ori: dict, update: dict):
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
75 """Recursively update a dictionary"""
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
76 # cf. https://stackoverflow.com/a/3233356, thanks Alex Martelli
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
77 for k, v in update.items():
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
78 if isinstance(v, collections.abc.Mapping):
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
79 ori[k] = recursive_update(ori.get(k, {}), v)
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
80 else:
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
81 ori[k] = v
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
82 return ori
1649bbe8d07e tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
83
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
84
3292
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
85 class OrderedSet(collections.abc.MutableSet):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
86 """A mutable sequence which doesn't keep duplicates"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
87
3292
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
88 # TODO: complete missing set methods
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
89
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
90 def __init__(self, values=None):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
91 self._dict = {}
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
92 if values is not None:
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
93 self.update(values)
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
94
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
95 def __len__(self):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
96 return len(self._dict)
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
97
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
98 def __iter__(self):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
99 return iter(self._dict.keys())
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
100
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
101 def __contains__(self, item):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
102 return item in self._dict
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
103
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
104 def add(self, item):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
105 self._dict[item] = None
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
106
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
107 def discard(self, item):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
108 try:
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
109 del self._dict[item]
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
110 except KeyError:
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
111 pass
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
112
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
113 def update(self, items):
84f77f04aa08 tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents: 3265
diff changeset
114 self._dict.update({i: None for i in items})
3524
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
115
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
116
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3524
diff changeset
117 def parse_size(size):
3524
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
118 """Parse a file size with optional multiple symbole"""
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
119 try:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
120 return int(size)
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
121 except ValueError:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
122 number = []
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
123 symbol = []
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
124 try:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
125 for c in size:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
126 if c == " ":
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
127 continue
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
128 if c.isdigit():
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
129 number.append(c)
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
130 elif c.isalpha():
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
131 symbol.append(c)
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
132 else:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
133 raise ValueError("unexpected char in size: {c!r} (size: {size!r})")
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
134 number = int("".join(number))
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
135 symbol = "".join(symbol)
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
136 if symbol:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
137 try:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
138 multiplier = size_units[symbol.lower()]
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
139 except KeyError:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
140 raise ValueError(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
141 "unknown size multiplier symbole: {symbol!r} (size: {size!r})"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
142 )
3524
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
143 else:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
144 return number * multiplier
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
145 return number
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
146 except Exception as e:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
147 raise ValueError(f"invalid size: {e}")
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
148
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
149
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
150 def get_size_multiplier(size: int | str, suffix="o"):
3524
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
151 """Get multiplier of a file size"""
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
152 size = int(size)
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
153 #  cf. https://stackoverflow.com/a/1094933 (thanks)
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
154 for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
155 if abs(size) < 1024.0:
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
156 return size, f"{unit}{suffix}"
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
157 size /= 1024.0
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
158 return size, f"Yi{suffix}"
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
159
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
160
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
161 def get_human_size(size: int | str, suffix: str = "o", sep: str = " ") -> str:
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
162 """Return data size in a human readable format."""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3524
diff changeset
163 size, symbol = get_size_multiplier(size, suffix)
3524
584379473925 tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
164 return f"{size:.2f}{sep}{symbol}"