Mercurial > libervia-backend
annotate sat/tools/common/utils.py @ 3608:6d03e8ebf1a6
doc (cli/common): explain the new `-C, --no-cache` option
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 29 Jul 2021 22:51:01 +0200 |
parents | 584379473925 |
children | 524856bd7b19 |
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 | 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 |
3524
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
22 size_units = { |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
23 "b": 1, |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
24 "kb": 1000, |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
25 "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
|
26 "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
|
27 "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
|
28 "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
|
29 "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
|
30 "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
|
31 "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
|
32 "o": 1, |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
33 "ko": 1000, |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
34 "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
|
35 "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
|
36 "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
|
37 "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
|
38 "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
|
39 "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
|
40 "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
|
41 "kib": 1024, |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
42 "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
|
43 "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
|
44 "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
|
45 "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
|
46 "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
|
47 "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
|
48 "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
|
49 "kio": 1024, |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
50 "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
|
51 "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
|
52 "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
|
53 "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
|
54 "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
|
55 "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
|
56 "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
|
57 } |
3265
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
58 |
3045
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def per_luminance(red, green, blue): |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 """Caculate the perceived luminance of a RGB color |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 @param red(int): 0-1 normalized value of red |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 @param green(int): 0-1 normalized value of green |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 @param blue(int): 0-1 normalized value of blue |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 @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
|
67 """ |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 # cf. https://stackoverflow.com/a/1855903, thanks Gacek |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
6af44ca3beac
tools (common): helping method to calculate luminance:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 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
|
71 |
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 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
|
74 """Recursively update a dictionary""" |
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
75 # 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
|
76 for k, v in update.items(): |
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
77 if isinstance(v, collections.abc.Mapping): |
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
78 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
|
79 else: |
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
80 ori[k] = v |
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
81 return ori |
1649bbe8d07e
tools (common/utils): new `recursive_update` method for dicts
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
82 |
3292
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
83 class OrderedSet(collections.abc.MutableSet): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
84 """A mutable sequence which doesn't keep duplicates""" |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
85 # TODO: complete missing set methods |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
86 |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
87 def __init__(self, values=None): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
88 self._dict = {} |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
89 if values is not None: |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
90 self.update(values) |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
91 |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
92 def __len__(self): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
93 return len(self._dict) |
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 __iter__(self): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
96 return iter(self._dict.keys()) |
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 __contains__(self, item): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
99 return item in self._dict |
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 add(self, item): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
102 self._dict[item] = None |
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 discard(self, item): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
105 try: |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
106 del self._dict[item] |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
107 except KeyError: |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
108 pass |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
109 |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
110 def update(self, items): |
84f77f04aa08
tools (common/utils): new OrderedSet class
Goffi <goffi@goffi.org>
parents:
3265
diff
changeset
|
111 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
|
112 |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
113 |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
114 def parseSize(size): |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
115 """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
|
116 try: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
117 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
|
118 except ValueError: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
119 number = [] |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
120 symbol = [] |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
121 try: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
122 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
|
123 if c == " ": |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
124 continue |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 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
|
129 else: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 if symbol: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
134 try: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
135 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
|
136 except KeyError: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
137 raise ValueError( |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
138 "unknown size multiplier symbole: {symbol!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
|
139 else: |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
140 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
|
141 return number |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
142 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
|
143 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
|
144 |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
145 |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
146 def getSizeMultiplier(size, suffix="o"): |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
147 """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
|
148 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
|
149 # 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
156 |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
157 def getHumanSize(size, suffix="o", sep=" "): |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
158 size, symbol = getSizeMultiplier(size, suffix) |
584379473925
tools (common/utils): new methods to parse and generate file size with symbols:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
159 return f"{size:.2f}{sep}{symbol}" |