From 5421d3e0ce9b3e49a833d40454b2a37ab8bbe74d Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 19 Oct 2022 09:37:46 +0200 Subject: [proxy] use XDG_STATE_HOME to determine the directory for Haketilo to store its data in --- src/hydrilla/locales/en_US/LC_MESSAGES/messages.po | 24 +++++++++++----------- src/hydrilla/mitmproxy_launcher/launch.py | 18 ++++++++++++++-- 2 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/hydrilla/locales/en_US/LC_MESSAGES/messages.po b/src/hydrilla/locales/en_US/LC_MESSAGES/messages.po index eac53fe..defbbcc 100644 --- a/src/hydrilla/locales/en_US/LC_MESSAGES/messages.po +++ b/src/hydrilla/locales/en_US/LC_MESSAGES/messages.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: hydrilla 2.0\n" "Report-Msgid-Bugs-To: koszko@koszko.org\n" -"POT-Creation-Date: 2022-10-18 17:03+0200\n" +"POT-Creation-Date: 2022-10-19 09:36+0200\n" "PO-Revision-Date: 2022-02-12 00:00+0000\n" "Last-Translator: Wojtek Kosior \n" "Language: en_US\n" @@ -79,7 +79,7 @@ msgid "built_package_files_destination" msgstr "Destination directory to write built package files to." #: src/hydrilla/builder/build.py:499 -#: src/hydrilla/mitmproxy_launcher/launch.py:64 +#: src/hydrilla/mitmproxy_launcher/launch.py:69 #: src/hydrilla/server/serve.py:211 src/hydrilla/server/serve.py:229 #: src/hydrilla/server/serve.py:269 #, python-format @@ -164,7 +164,7 @@ msgstr "Not a valid JSON file." msgid "no_schema_number_in_instance" msgstr "JSON schema number is missing from a document." -#: src/hydrilla/mitmproxy_launcher/launch.py:53 +#: src/hydrilla/mitmproxy_launcher/launch.py:58 msgid "cli_help.haketilo" msgstr "" "Run Haketilo proxy.\n" @@ -172,25 +172,25 @@ msgstr "" "This command starts Haketilo as a local HTTP proxy which a web browser " "can then use." -#: src/hydrilla/mitmproxy_launcher/launch.py:55 +#: src/hydrilla/mitmproxy_launcher/launch.py:60 msgid "cli_opt.haketilo.listen_host" msgstr "IP address port number the proxy should listen on." -#: src/hydrilla/mitmproxy_launcher/launch.py:57 +#: src/hydrilla/mitmproxy_launcher/launch.py:62 msgid "cli_opt.haketilo.port" msgstr "TCP port number the proxy should listen on." -#: src/hydrilla/mitmproxy_launcher/launch.py:59 +#: src/hydrilla/mitmproxy_launcher/launch.py:64 msgid "cli_opt.haketilo.launch_browser" msgstr "" "Whether Haketilo should try to open its landing page in your default " "browser. Defaults to yes ('-L')." -#: src/hydrilla/mitmproxy_launcher/launch.py:62 -msgid "cli_opt.haketilo.dir" -msgstr "Data directory for Haketilo to use." +#: src/hydrilla/mitmproxy_launcher/launch.py:67 +msgid "cli_opt.haketilo.dir_defaults_to_{}" +msgstr "Data directory for Haketilo to use. Defaults to \"{}\"." -#: src/hydrilla/mitmproxy_launcher/launch.py:65 +#: src/hydrilla/mitmproxy_launcher/launch.py:70 msgid "cli_opt.haketilo.version" msgstr "Print version information and exit" @@ -200,13 +200,13 @@ msgstr "" "Attempt was made to configure Mitmproxy addon's option '{}' which has " "already been configured." -#: src/hydrilla/proxy/addon.py:201 +#: src/hydrilla/proxy/addon.py:202 msgid "warn.proxy.couldnt_launch_browser" msgstr "" "Failed to open a URL in a web browser. Do you have a default web browser " "configured?" -#: src/hydrilla/proxy/addon.py:245 +#: src/hydrilla/proxy/addon.py:246 msgid "err.proxy.unknown_error_{}_try_again" msgstr "" "Haketilo experienced an error. Try again.\n" diff --git a/src/hydrilla/mitmproxy_launcher/launch.py b/src/hydrilla/mitmproxy_launcher/launch.py index 4fe31db..2f706fd 100644 --- a/src/hydrilla/mitmproxy_launcher/launch.py +++ b/src/hydrilla/mitmproxy_launcher/launch.py @@ -32,6 +32,7 @@ import subprocess as sp import typing as t from pathlib import Path +from shutil import copytree # The following import requires at least Python 3.8. There is no point adding # a workaround for Python 3.7 because mitmproxy itself (which we're loading # here) relies on Python 3.9+. This does not affect the Hydrilla server and @@ -44,6 +45,10 @@ from .. import _version from ..translations import smart_gettext as _ +xdg_state_home = os.environ.get('XDG_STATE_HOME', '.local/state') +default_dir = str(Path.home() / xdg_state_home / 'haketilo') +old_default_dir_path = Path.home() / '.haketilo/' + addon_script_text = ''' from hydrilla.proxy.addon import HaketiloAddon @@ -57,9 +62,9 @@ addons = [HaketiloAddon()] help=_('cli_opt.haketilo.port')) @click.option('-L/-l', '--launch-browser/--no-launch-browser', default=True, help=_('cli_opt.haketilo.launch_browser')) -@click.option('-d', '--directory', default='~/.haketilo/', +@click.option('-d', '--directory', default=default_dir, type=click.Path(file_okay=False), - help=_('cli_opt.haketilo.dir')) + help=_('cli_opt.haketilo.dir_defaults_to_{}').format(default_dir)) @click.version_option(version=_version.version, prog_name='Haketilo proxy', message=_('%(prog)s_%(version)s_license'), help=_('cli_opt.haketilo.version')) @@ -67,6 +72,15 @@ def launch(listen_host: str, port: int, launch_browser: bool, directory: str) \ -> t.NoReturn: directory_path = Path(os.path.expanduser(directory)).resolve() + # Before we started using XDG_STATE_HOME, we were storing files by default + # under ~/.haketilo. Let's make sync state from there to our new default + # state directory + if directory == default_dir and \ + old_default_dir_path.exists() and \ + not directory_path.exists(): + directory_path.parent.mkdir(parents=True, exist_ok=True) + copytree(old_default_dir_path, directory_path, symlinks=True) + directory_path.mkdir(parents=True, exist_ok=True) script_path = directory_path / 'addon.py' -- cgit v1.2.3