156 lines
3.4 KiB
Python
156 lines
3.4 KiB
Python
import sys
|
|
import click
|
|
|
|
from izzylib import signal_handler
|
|
from os import environ as env
|
|
|
|
from . import __shortname__, __software__, __version__
|
|
from .component.application import Application
|
|
from .database.base import default_config
|
|
from .dbus import Client
|
|
from .functions import install_desktop_file
|
|
|
|
|
|
context_settings = dict(
|
|
show_default = True
|
|
)
|
|
|
|
|
|
@click.group('cli', context_settings=context_settings, invoke_without_command=True)
|
|
@click.version_option(version=__version__, prog_name=__shortname__)
|
|
@click.option('--profile', '-p', default='DEFAULT', help='The browser profile to use')
|
|
@click.pass_context
|
|
def cli(ctx, profile):
|
|
## Disable scrollbar autohide
|
|
env['GTK_OVERLAY_SCROLLING'] = '0'
|
|
|
|
ctx.obj = DotDict(
|
|
app = Application(profile),
|
|
dbus = Client()
|
|
)
|
|
|
|
if (deskfile := install_desktop_file(overwrite=False)):
|
|
logging.verbose(f'Installed new desktop file: {deskfile}')
|
|
|
|
if not ctx.invoked_subcommand:
|
|
cli_run.callback([])
|
|
|
|
|
|
@cli.command('run')
|
|
@click.argument('urls', nargs=-1)
|
|
@click.pass_context
|
|
def cli_run(ctx, urls):
|
|
v = sys.version_info
|
|
|
|
# Set gui scale
|
|
with ctx.obj.app.db.session as s:
|
|
env['GDK_SCALE'] = str(s.get_config('scale'))
|
|
|
|
## Workaround to get webextensions working
|
|
#env['PYTHONPATH'] = ctx.obj.app.path.script
|
|
#env['LD_PRELOAD'] = f'/usr/lib/x86_64-linux-gnu/libpython{v.major}.{v.minor}.so.1'
|
|
|
|
with ctx.obj.dbus as dbus:
|
|
if dbus.connected:
|
|
if urls:
|
|
click.echo('Opening tabs in running browser')
|
|
cli_tab_new.callback(urls)
|
|
|
|
else:
|
|
click.echo('Browser already running')
|
|
|
|
return
|
|
|
|
Notify.init(__software__)
|
|
GLib.set_application_name(__software__)
|
|
GLib.set_prgname(__shortname__)
|
|
|
|
ctx.obj.app.startup_urls = urls
|
|
ctx.obj.app.run([])
|
|
|
|
|
|
@cli.command('install')
|
|
@click.pass_context
|
|
def cli_install(ctx):
|
|
if (deskfile := install_desktop_file(overwrite=True)):
|
|
click.echo(f'Installed new desktop file')
|
|
|
|
else:
|
|
click.echo('Failed to install new desktop file')
|
|
|
|
|
|
@cli.group('tab')
|
|
@click.pass_context
|
|
def cli_tab(ctx):
|
|
if not ctx.obj.dbus.connect():
|
|
ctx.obj.dbus.disconnect()
|
|
click.echo('Failed to connect to dbus. Is the browser running?')
|
|
|
|
sys.exit()
|
|
|
|
|
|
@cli_tab.command('list')
|
|
@click.pass_context
|
|
def cli_tab_list(ctx):
|
|
for tab in ctx.obj.dbus.GetTabs():
|
|
click.echo(tab)
|
|
|
|
ctx.obj.dbus.disconnect()
|
|
|
|
|
|
@cli_tab.command('new')
|
|
@click.argument('urls', nargs=-1)
|
|
@click.pass_context
|
|
def cli_tab_new(ctx, urls):
|
|
for url in urls:
|
|
ctx.obj.dbus.NewTab(url, urls[-1] == url)
|
|
ctx.obj.dbus.Present()
|
|
click.echo(f'Opening url in browser: {url}')
|
|
|
|
ctx.obj.dbus.disconnect()
|
|
|
|
|
|
@cli.group('config')
|
|
@click.pass_context
|
|
def cli_config(ctx):
|
|
pass
|
|
|
|
|
|
@cli_config.command('list')
|
|
@click.pass_context
|
|
def cli_config_list(ctx):
|
|
click.echo('Current config:')
|
|
|
|
with ctx.obj.app.db.session as s:
|
|
for key in default_config.keys():
|
|
if key == 'version':
|
|
continue
|
|
|
|
value = s.get_config(key)
|
|
click.echo(f'- {key.replace("_", "-").ljust(25)}: {value}')
|
|
|
|
|
|
@cli_config.command('get')
|
|
@click.argument('key')
|
|
@click.pass_context
|
|
def cli_config_get(ctx, key):
|
|
with ctx.obj.app.db.session as s:
|
|
value = s.get_config(key.replace('-', '_'))
|
|
|
|
click.echo(f'{key} = {value}')
|
|
|
|
|
|
@cli_config.command('set')
|
|
@click.argument('key')
|
|
@click.argument('value')
|
|
@click.pass_context
|
|
def cli_config_set(ctx, key, value):
|
|
with ctx.obj.app.db.session as s:
|
|
value = s.put_config(key.replace('-', '_'), value)
|
|
|
|
click.echo(f'{key} = {value}')
|
|
|
|
|
|
def main():
|
|
cli(prog_name=__shortname__)
|