Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/util/__init__.py @ 166:ed45abb4940f
FIX: in util.normalize_filename(): use the 'u"' prefix in the else part for non-byte strings
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 09 Jan 2025 14:06:01 +0100 |
| parents | a813094ae4f5 |
| children | 804a823c63f5 |
| rev | line source |
|---|---|
|
115
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
|
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
2 # :- |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
3 # :Copyright: (c) 2020-2025 Franz Glasner |
|
115
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
4 # :License: BSD-3-Clause |
|
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
5 # :- |
|
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
6 r"""Utility package. |
|
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
7 |
|
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
8 """ |
|
e15b3d1ff0d9
New subpackage with a "contextlib.nullcontext" for older Python versions
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
9 |
|
164
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
163
diff
changeset
|
10 __all__ = ["PY2", |
|
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
163
diff
changeset
|
11 "normalize_filename", |
|
118
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
12 "argv2algo", |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
13 "algotag2algotype", |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
14 "get_blake2b", |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
15 "get_blake2b_256", |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
16 "get_blake2s", |
|
163
fa7dd54e9715
FIX: Add "fsencode" to public functions in cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
124
diff
changeset
|
17 "fsencode", |
|
118
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
18 ] |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
19 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
20 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
21 import argparse |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
22 import hashlib |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
23 import os |
|
164
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
163
diff
changeset
|
24 import sys |
|
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
163
diff
changeset
|
25 |
|
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
163
diff
changeset
|
26 |
|
a813094ae4f5
Move PY2 from cutils.util.constants into cutils.util
Franz Glasner <fzglas.hg@dom66.de>
parents:
163
diff
changeset
|
27 PY2 = sys.version_info[0] < 3 |
|
118
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
28 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
29 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
30 def get_blake2b(): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
31 """Get the factory for blake2b""" |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
32 try: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
33 return hashlib.blake2b |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
34 except AttributeError: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
35 import pyblake2 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
36 return pyblake2.blake2b |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
37 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
38 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
39 def get_blake2b_256(): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
40 """Get the factory for blake2b-256""" |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
41 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
42 try: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
43 hashlib.blake2b |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
44 except AttributeError: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
45 import pyblake2 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
46 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
47 def _get_blake(): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
48 return pyblake2.blake2b(digest_size=32) |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
49 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
50 else: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
51 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
52 def _get_blake(): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
53 return hashlib.blake2b(digest_size=32) |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
54 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
55 return _get_blake |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
56 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
57 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
58 def get_blake2s(): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
59 """Get the factory for blake2s""" |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
60 try: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
61 return hashlib.blake2s |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
62 except AttributeError: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
63 import pyblake2 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
64 return pyblake2.blake2s |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
65 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
66 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
67 def argv2algo(s): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
68 """Convert a command line algorithm specifier into a tuple with the |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
69 type/factory of the digest and the algorithms tag for output purposes. |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
70 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
71 :param str s: the specifier from the commane line |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
72 :return: the internal digest specification |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
73 :rtype: a tuple (digest_type_or_factory, name_in_output) |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
74 :raises argparse.ArgumentTypeError: for unrecognized algorithms or names |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
75 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
76 String comparisons are done case-insensitively. |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
77 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
78 """ |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
79 s = s.lower() |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
80 if s in ("1", "sha1"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
81 return (hashlib.sha1, "SHA1") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
82 elif s in ("224", "sha224"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
83 return (hashlib.sha224, "SHA224") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
84 elif s in ("256", "sha256"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
85 return (hashlib.sha256, "SHA256") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
86 elif s in ("384", "sha384"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
87 return (hashlib.sha384, "SHA384") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
88 elif s in ("512", "sha512"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
89 return (hashlib.sha512, "SHA512") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
90 elif s in ("3-224", "sha3-224"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
91 return (hashlib.sha3_224, "SHA3-224") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
92 elif s in ("3-256", "sha3-256"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
93 return (hashlib.sha3_256, "SHA3-256") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
94 elif s in ("3-384", "sha3-384"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
95 return (hashlib.sha3_384, "SHA3-384") |
|
120
a548783381b6
More accurate description of --algorithm.
Franz Glasner <fzglas.hg@dom66.de>
parents:
118
diff
changeset
|
96 elif s in ("3", "3-512", "sha3-512"): |
|
118
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
97 return (hashlib.sha3_512, "SHA3-512") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
98 elif s in ("blake2b", "blake2b-512", "blake2", "blake2-512"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
99 return (get_blake2b(), "BLAKE2b") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
100 elif s in ("blake2s", "blake2s-256"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
101 return (get_blake2s(), "BLAKE2s") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
102 elif s in ("blake2-256", "blake2b-256"): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
103 return (get_blake2b_256(), "BLAKE2b-256") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
104 elif s == "md5": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
105 return (hashlib.md5, "MD5") |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
106 else: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
107 raise argparse.ArgumentTypeError( |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
108 "`{}' is not a recognized algorithm".format(s)) |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
109 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
110 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
111 def algotag2algotype(s): |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
112 """Convert the algorithm specifier in a BSD-style digest file to the |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
113 type/factory of the corresponding algorithm. |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
114 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
115 :param str s: the tag (i.e. normalized name) or the algorithm |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
116 :return: the digest type or factory for `s` |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
117 :raises ValueError: on unknown and/or unhandled algorithms |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
118 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
119 All string comparisons are case-sensitive. |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
120 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
121 """ |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
122 if s == "SHA1": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
123 return hashlib.sha1 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
124 elif s == "SHA224": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
125 return hashlib.sha224 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
126 elif s == "SHA256": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
127 return hashlib.sha256 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
128 elif s == "SHA384": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
129 return hashlib.sha384 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
130 elif s == "SHA512": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
131 return hashlib.sha512 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
132 elif s == "SHA3-224": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
133 return hashlib.sha3_224 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
134 elif s == "SHA3-256": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
135 return hashlib.sha3_256 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
136 elif s == "SHA3-384": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
137 return hashlib.sha3_384 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
138 elif s == "SHA3-512": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
139 return hashlib.sha3_512 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
140 elif s in ("BLAKE2b", "BLAKE2b-512", "BLAKE2b512"): # compat for openssl |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
141 return get_blake2b() |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
142 elif s in ("BLAKE2s", "BLAKE2s-256", "BLAKE2s256"): # compat for openssl |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
143 return get_blake2s() |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
144 elif s in ("BLAKE2b-256", "BLAKE2b256"): # also compat for openssl dgst |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
145 return get_blake2b_256() |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
146 elif s == "MD5": |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
147 return hashlib.md5 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
148 else: |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
149 raise ValueError("unknown algorithm: {}".format(s)) |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
150 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
151 |
|
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
152 def normalize_filename(filename, strip_leading_dot_slash=False): |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
153 if isinstance(filename, bytes): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
154 filename = filename.replace(b"\\", b"/") |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
155 if strip_leading_dot_slash: |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
156 while filename.startswith(b"./"): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
157 filename = filename[2:] |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
158 else: |
|
166
ed45abb4940f
FIX: in util.normalize_filename(): use the 'u"' prefix in the else part for non-byte strings
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
159 filename = filename.replace(u"\\", u"/") |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
160 if strip_leading_dot_slash: |
|
166
ed45abb4940f
FIX: in util.normalize_filename(): use the 'u"' prefix in the else part for non-byte strings
Franz Glasner <fzglas.hg@dom66.de>
parents:
164
diff
changeset
|
161 while filename.startswith(u"./"): |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
162 filename = filename[2:] |
|
118
12339ac2148d
Move some functions into cutils.util (i.e. algorithms and their aliases)
Franz Glasner <fzglas.hg@dom66.de>
parents:
115
diff
changeset
|
163 return filename |
|
124
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
164 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
165 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
166 def fsencode(what): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
167 """A somewhat compatibility function for :func:`os.fsencode`. |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
168 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
169 If `what` is of type :class:`bytes` no :func:`os.fsencode` is required. |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
170 |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
171 """ |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
172 if isinstance(what, bytes): |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
173 return what |
|
3bd3f32b5e60
A first version of "treesum" is working
Franz Glasner <fzglas.hg@dom66.de>
parents:
120
diff
changeset
|
174 return os.fsencode(what) |
