DRYer: Get hostname

This commit is contained in:
pagedown 2022-11-26 15:28:32 +08:00
parent 63a08dc6cc
commit 6a1b456bac
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
4 changed files with 20 additions and 15 deletions

View File

@ -4,12 +4,13 @@
import os import os
import re import re
import signal import signal
import socket
import subprocess import subprocess
import sys import sys
from typing import Callable, cast from typing import Callable, cast
from urllib.parse import quote_from_bytes from urllib.parse import quote_from_bytes
from kitty.utils import get_hostname
def write_hyperlink(write: Callable[[bytes], None], url: bytes, line: bytes, frag: bytes = b'') -> None: def write_hyperlink(write: Callable[[bytes], None], url: bytes, line: bytes, frag: bytes = b'') -> None:
text = b'\033]8;;' + url text = b'\033]8;;' + url
@ -76,7 +77,7 @@ def main() -> None:
num_pat = re.compile(br'^(\d+)([:-])') num_pat = re.compile(br'^(\d+)([:-])')
in_result: bytes = b'' in_result: bytes = b''
hostname = socket.gethostname().encode('utf-8') hostname = get_hostname().encode('utf-8')
try: try:
for line in p.stdout: for line in p.stdout:

View File

@ -4,7 +4,6 @@
import os import os
import re import re
import shlex import shlex
import socket
import sys import sys
from collections import deque from collections import deque
from enum import Enum, auto from enum import Enum, auto
@ -305,6 +304,12 @@ def env(x: str) -> str:
role_map['envvar'] = role_map['env'] role_map['envvar'] = role_map['env']
@run_once
def hostname() -> str:
from .utils import get_hostname
return get_hostname(fallback='localhost')
def hyperlink_for_url(url: str, text: str) -> str: def hyperlink_for_url(url: str, text: str) -> str:
if sys.stdout.isatty(): if sys.stdout.isatty():
return f'\x1b]8;;{url}\x1b\\\x1b[4:3;58:5:4m{text}\x1b[4:0;59m\x1b]8;;\x1b\\' return f'\x1b]8;;{url}\x1b\\\x1b[4:3;58:5:4m{text}\x1b[4:0;59m\x1b]8;;\x1b\\'
@ -315,7 +320,7 @@ def hyperlink_for_path(path: str, text: str) -> str:
path = os.path.abspath(path).replace(os.sep, "/") path = os.path.abspath(path).replace(os.sep, "/")
if os.path.isdir(path): if os.path.isdir(path):
path += path.rstrip("/") + "/" path += path.rstrip("/") + "/"
return hyperlink_for_url(f'file://{socket.gethostname()}{path}', text) return hyperlink_for_url(f'file://{hostname()}{path}', text)
@role @role
@ -337,12 +342,6 @@ def doc(x: str) -> str:
return ref_hyperlink(x, 'doc-') return ref_hyperlink(x, 'doc-')
@run_once
def hostname() -> str:
import socket
return socket.gethostname() or 'localhost'
def ref_hyperlink(x: str, prefix: str = '') -> str: def ref_hyperlink(x: str, prefix: str = '') -> str:
t, q = text_and_target(x) t, q = text_and_target(x)
url = f'kitty+doc://{hostname()}/#ref={prefix}{q}' url = f'kitty+doc://{hostname()}/#ref={prefix}{q}'

View File

@ -595,6 +595,14 @@ def natsort_ints(iterable: Iterable[str]) -> List[str]:
return sorted(iterable, key=alphanum_key) return sorted(iterable, key=alphanum_key)
def get_hostname(fallback: str = '') -> str:
import socket
try:
return socket.gethostname() or fallback
except Exception:
return fallback
def resolve_editor_cmd(editor: str, shell_env: Mapping[str, str]) -> Optional[str]: def resolve_editor_cmd(editor: str, shell_env: Mapping[str, str]) -> Optional[str]:
import shlex import shlex
editor_cmd = shlex.split(editor) editor_cmd = shlex.split(editor)

View File

@ -853,11 +853,8 @@ class Window:
return return
if (not purl.scheme or purl.scheme == 'file'): if (not purl.scheme or purl.scheme == 'file'):
if purl.netloc: if purl.netloc:
from socket import gethostname from .utils import get_hostname
try: hostname = get_hostname()
hostname = gethostname()
except Exception:
hostname = ''
remote_hostname = purl.netloc.partition(':')[0] remote_hostname = purl.netloc.partition(':')[0]
if remote_hostname and remote_hostname != hostname and remote_hostname != 'localhost': if remote_hostname and remote_hostname != hostname and remote_hostname != 'localhost':
self.handle_remote_file(purl.netloc, unquote(purl.path)) self.handle_remote_file(purl.netloc, unquote(purl.path))