Debian Codegraph @

src/globus_sdk/_internal/utils.py

raw
1 from __future__ import annotations
2
3 import hashlib
4 import platform
5
6
7 def sha256_string(s: str) -> str:
8 return hashlib.sha256(s.encode("utf-8")).hexdigest()
9
10
11 def get_nice_hostname() -> str | None:
12 """
13 Get the current hostname, with the following added behavior:
14
15 - if it ends in '.local', strip that suffix, as this is a frequent macOS behavior
16 'DereksCoolMacbook.local' -> 'DereksCoolMacbook'
17
18 - if the hostname is undiscoverable, return None
19 """
20 name = platform.node()
21 if name.endswith(".local"):
22 return name[: -len(".local")]
23 return name or None
24
25
26 def slash_join(a: str, b: str | None) -> str:
27 """
28 Join a and b with a single slash, regardless of whether they already
29 contain a trailing/leading slash or neither.
30
31 :param a: the first path component
32 :param b: the second path component
33 """
34 if not b: # "" or None, don't append a slash
35 return a
36 if a.endswith("/"):
37 if b.startswith("/"):
38 return a[:-1] + b
39 return a + b
40 if b.startswith("/"):
41 return a + b
42 return a + "/" + b