kitty/__main__.py

148 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2016-10-14 10:03:27 +03:00
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
2018-05-31 20:32:38 +03:00
import os
2021-06-07 12:31:30 +03:00
import sys
2020-03-12 11:21:51 +03:00
from typing import List
2016-10-14 10:03:27 +03:00
2020-03-12 11:21:51 +03:00
def icat(args: List[str]) -> None:
from kittens.runner import run_kitten as rk
sys.argv = args
2020-03-12 11:21:51 +03:00
rk('icat')
2020-03-12 11:21:51 +03:00
def list_fonts(args: List[str]) -> None:
2020-03-06 06:05:23 +03:00
from kitty.fonts.list import main as list_main
list_main(args)
2020-03-12 11:21:51 +03:00
def remote_control(args: List[str]) -> None:
2020-03-06 06:05:23 +03:00
from kitty.remote_control import main as rc_main
rc_main(args)
2018-01-07 12:47:29 +03:00
2020-03-12 11:21:51 +03:00
def runpy(args: List[str]) -> None:
if len(args) < 2:
raise SystemExit('Usage: kitty +runpy "some python code"')
sys.argv = ['kitty'] + args[2:]
exec(args[1])
2020-03-12 11:21:51 +03:00
def hold(args: List[str]) -> None:
import subprocess
ret = subprocess.Popen(args[1:]).wait()
2022-01-07 09:43:20 +03:00
from kitty.utils import hold_till_enter
hold_till_enter()
raise SystemExit(ret)
2020-03-12 11:21:51 +03:00
def complete(args: List[str]) -> None:
2020-03-06 06:05:23 +03:00
from kitty.complete import main as complete_main
complete_main(args[1:], entry_points, namespaced_entry_points)
2018-08-20 07:54:09 +03:00
2020-03-12 11:21:51 +03:00
def launch(args: List[str]) -> None:
import runpy
sys.argv = args[1:]
2021-06-28 17:08:51 +03:00
try:
exe = args[1]
except IndexError:
raise SystemExit(
'usage: kitty +launch script.py [arguments to be passed to script.py ...]\n\n'
'script.py will be run with full access to kitty code. If script.py is '
'prefixed with a : it will be searched for in PATH'
)
if exe.startswith(':'):
import shutil
2020-03-12 11:21:51 +03:00
q = shutil.which(exe[1:])
if not q:
2021-06-28 17:08:51 +03:00
raise SystemExit(f'{exe[1:]} not found in PATH')
2020-03-12 11:21:51 +03:00
exe = q
2021-06-28 17:08:51 +03:00
if not os.path.exists(exe):
raise SystemExit(f'{exe} does not exist')
runpy.run_path(exe, run_name='__main__')
2020-03-12 11:21:51 +03:00
def run_kitten(args: List[str]) -> None:
try:
kitten = args[1]
except IndexError:
from kittens.runner import list_kittens
list_kittens()
raise SystemExit(1)
sys.argv = args[1:]
2020-03-12 11:21:51 +03:00
from kittens.runner import run_kitten as rk
rk(kitten)
2020-11-04 14:00:29 +03:00
def edit_config_file(args: List[str]) -> None:
2021-06-07 12:27:38 +03:00
from kitty.cli import create_default_opts
2021-06-07 12:31:30 +03:00
from kitty.fast_data_types import set_options
from kitty.utils import edit_config_file as f
2021-06-07 12:27:38 +03:00
set_options(create_default_opts())
2020-11-04 14:00:29 +03:00
f()
2020-03-12 11:21:51 +03:00
def namespaced(args: List[str]) -> None:
try:
func = namespaced_entry_points[args[1]]
except KeyError:
pass
else:
func(args[1:])
return
raise SystemExit(f'{args[1]} is not a known entry point. Choices are: ' + ', '.join(namespaced_entry_points))
2018-01-07 12:47:29 +03:00
entry_points = {
2018-01-12 08:57:23 +03:00
# These two are here for backwards compat
2018-01-07 12:47:29 +03:00
'icat': icat,
'list-fonts': list_fonts,
2018-01-12 08:57:23 +03:00
2018-01-07 12:47:29 +03:00
'@': remote_control,
'+': namespaced,
}
namespaced_entry_points = {k: v for k, v in entry_points.items() if k[0] not in '+@'}
namespaced_entry_points['hold'] = hold
2018-08-20 07:54:09 +03:00
namespaced_entry_points['complete'] = complete
namespaced_entry_points['runpy'] = runpy
namespaced_entry_points['launch'] = launch
namespaced_entry_points['kitten'] = run_kitten
namespaced_entry_points['edit-config'] = edit_config_file
2018-01-07 12:47:29 +03:00
2020-03-12 11:21:51 +03:00
def setup_openssl_environment() -> None:
# Use our bundled CA certificates instead of the system ones, since
# many systems come with no certificates in a useable form or have various
# locations for the certificates.
2021-08-16 19:53:24 +03:00
d = os.path.dirname
ext_dir: str = getattr(sys, 'kitty_extensions_dir')
2021-08-16 19:53:24 +03:00
if 'darwin' in sys.platform.lower():
cert_file = os.path.join(d(d(d(ext_dir))), 'cacert.pem')
2021-08-16 19:53:24 +03:00
else:
cert_file = os.path.join(d(ext_dir), 'cacert.pem')
os.environ['SSL_CERT_FILE'] = cert_file
setattr(sys, 'kitty_ssl_env_var', 'SSL_CERT_FILE')
2018-05-31 20:32:38 +03:00
2020-03-12 11:21:51 +03:00
def main() -> None:
if getattr(sys, 'frozen', False) and getattr(sys, 'kitty_extensions_dir', ''):
2018-05-31 20:32:38 +03:00
setup_openssl_environment()
first_arg = '' if len(sys.argv) < 2 else sys.argv[1]
2018-01-07 12:47:29 +03:00
func = entry_points.get(first_arg)
if func is None:
if first_arg.startswith('@'):
remote_control(['@', first_arg[1:]] + sys.argv[2:])
2018-01-12 08:57:23 +03:00
elif first_arg.startswith('+'):
namespaced(['+', first_arg[1:]] + sys.argv[2:])
else:
2020-03-06 06:05:23 +03:00
from kitty.main import main as kitty_main
kitty_main()
2018-01-07 12:47:29 +03:00
else:
func(sys.argv[1:])
if __name__ == '__main__':
2017-10-03 18:50:18 +03:00
main()