kitty/__main__.py

118 lines
3.0 KiB
Python
Raw Normal View History

2016-10-14 10:03:27 +03:00
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
import sys
2018-05-31 20:32:38 +03:00
import os
2016-10-14 10:03:27 +03:00
def icat(args):
from kittens.runner import run_kitten
sys.argv = args
run_kitten('icat')
def list_fonts(args):
2017-11-10 08:32:32 +03:00
from kitty.fonts.list import main
main(args)
2018-01-07 12:47:29 +03:00
def remote_control(args):
from kitty.remote_control import main
main(args)
def runpy(args):
sys.argv = ['kitty'] + args[2:]
exec(args[1])
def hold(args):
import subprocess
ret = subprocess.Popen(args[1:]).wait()
sys.stdin.read()
raise SystemExit(ret)
2018-08-20 07:54:09 +03:00
def complete(args):
from kitty.complete import main
main(args[1:], entry_points, namespaced_entry_points)
def launch(args):
import runpy
sys.argv = args[1:]
exe = args[1]
if exe.startswith(':'):
import shutil
exe = shutil.which(exe[1:])
if not exe:
raise SystemExit('{} not found in PATH'.format(args[1][1:]))
runpy.run_path(exe, run_name='__main__')
def run_kitten(args):
try:
kitten = args[1]
except IndexError:
from kittens.runner import list_kittens
list_kittens()
raise SystemExit(1)
sys.argv = args[1:]
2018-04-12 06:53:48 +03:00
from kittens.runner import run_kitten
run_kitten(kitten)
2018-01-07 12:47:29 +03:00
def namespaced(args):
2018-01-12 08:57:23 +03:00
func = namespaced_entry_points[args[1]]
2018-01-07 12:47:29 +03:00
func(args[1:])
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,
'runpy': runpy,
'launch': launch,
'kitten': run_kitten,
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
2018-01-07 12:47:29 +03:00
2018-05-31 20:32:38 +03:00
def setup_openssl_environment():
# Workaround for Linux distros that have still failed to get their heads
# out of their asses and implement a common location for SSL certificates.
# It's not that hard people, there exists a wonderful tool called the symlink
# See http://www.mobileread.com/forums/showthread.php?t=256095
if 'SSL_CERT_FILE' not in os.environ and 'SSL_CERT_DIR' not in os.environ:
if os.access('/etc/pki/tls/certs/ca-bundle.crt', os.R_OK):
os.environ['SSL_CERT_FILE'] = '/etc/pki/tls/certs/ca-bundle.crt'
elif os.path.isdir('/etc/ssl/certs'):
os.environ['SSL_CERT_DIR'] = '/etc/ssl/certs'
def main():
2018-05-31 20:32:38 +03:00
if getattr(sys, 'frozen', False) and 'darwin' not in sys.platform.lower():
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:
from kitty.main import main
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()