From ff4316bc932a4fde155a41444e6840f0f831972f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Jan 2020 09:52:25 +0530 Subject: [PATCH] Make it easier to set calibre environment variables on macOS --- bypy/macos/site.py | 24 ++++++++++++++++++++++++ manual/customize.rst | 12 +++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/bypy/macos/site.py b/bypy/macos/site.py index 89ed6c11d7..3ea1c138a1 100644 --- a/bypy/macos/site.py +++ b/bypy/macos/site.py @@ -121,12 +121,36 @@ def run_entry_point(): return getattr(pmod, func)() +def read_user_env_vars(): + try: + with open(os.path.expanduser('~/Library/Preferences/calibre/macos-env.txt'), 'rb') as f: + raw = f.read().decode('utf-8', 'replace') + except EnvironmentError as err: + return + for line in raw.splitlines(): + if line.startswith('#'): + continue + parts = line.split('=', 1) + if len(parts) == 2: + key, val = parts + os.environ[key] = os.path.expandvars(os.path.expanduser(val)) + + def add_calibre_vars(base): sys.frameworks_dir = os.path.join(os.path.dirname(base), 'Frameworks') sys.resources_location = os.path.abspath(os.path.join(base, 'resources')) sys.extensions_location = os.path.join(sys.frameworks_dir, 'plugins') sys.binaries_path = os.path.join(os.path.dirname(base), 'MacOS') + try: + read_user_env_vars() + except Exception as err: + try: + sys.stderr.write('Failed to read user env vars with error: {}\n'.format(err)) + sys.stderr.flush() + except Exception: + pass + dv = os.environ.get('CALIBRE_DEVELOP_FROM', None) if dv and os.path.exists(dv): sys.path.insert(0, os.path.abspath(dv)) diff --git a/manual/customize.rst b/manual/customize.rst index 155414873e..ba161e09a4 100644 --- a/manual/customize.rst +++ b/manual/customize.rst @@ -43,10 +43,16 @@ Environment variables * ``CALIBRE_USE_DARK_PALETTE`` - Set it to ``1`` to have calibre use dark colors and ``0`` for normal colors (ignored on macOS). On Windows 10 in the absence of this variable, the windows system preference for dark colors is used. * ``SYSFS_PATH`` - Use if sysfs is mounted somewhere other than /sys - * ``http_proxy``, ``https_proxy`` - Used on linux to specify an HTTP(S) proxy + * ``http_proxy``, ``https_proxy`` - Used on Linux to specify an HTTP(S) proxy + +See `How to set environment variables in Windows `_. If you are on macOS +you can set environment variables by creating the :file:`~/Library/Preferences/calibre/macos-env.txt` and putting +the environment variables one per line in it, for example:: + + CALIBRE_DEVELOP_FROM=$HOME/calibre-src/src + CALIBRE_NO_NATIVE_FILEDIALOGS=1 + CALIBRE_CONFIG_DIRECTORY=~/.config/calibre -See `How to set environment variables in Windows `_ or -`How to set environment variables in macOS `_. Tweaks ------------