From 863df67150cce1999485d5693669fbe508243ad3 Mon Sep 17 00:00:00 2001 From: Rohitt Vashishtha Date: Fri, 27 Jan 2017 03:30:29 +0530 Subject: [PATCH] api: Add support for Environment Variables. This adds support for controlling the basic configuration (user, API key, etc.) of the Zulip API bindings via environment variables. Fixes #3364. Tweaked by tabbott to update variable names and document in README.md. --- README.md | 7 ++++++- zulip/__init__.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b262d4..91935b6 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,12 @@ Alternatively, you may explicitly use "--user", "--api-key", and `--site` in our examples, which is especially useful when testing. If you are running several bots which share a home directory, we recommend using `--config` to specify the path to the `zuliprc` file -for a specific bot. +for a specific bot. Finally, you can control the defaults for all of +these variables using the environment variables `ZULIP_CONFIG`, +`ZULIP_API_KEY`, `ZULIP_EMAIL`, `ZULIP_SITE`, `ZULIP_CERT`, +`ZULIP_CERT_KEY`, and `ZULIP_CERT_BUNDLE`. Command-line options take +precedence over environment variables take precedence over the config +files. The command line equivalents for other configuration options are: diff --git a/zulip/__init__.py b/zulip/__init__.py index 488c045..27d6969 100644 --- a/zulip/__init__.py +++ b/zulip/__init__.py @@ -186,8 +186,25 @@ class Client(object): if client is None: client = _default_client() + # Fill values from Environment Variables if not available in Constructor + if config_file is None: + config_file = os.environ.get("ZULIP_CONFIG") + if api_key is None: + api_key = os.environ.get("ZULIP_API_KEY") + if email is None: + email = os.environ.get("ZULIP_EMAIL") + if site is None: + site = os.environ.get("ZULIP_SITE") + if client_cert is None: + client_cert = os.environ.get("ZULIP_CERT") + if client_cert_key is None: + client_cert_key = os.environ.get("ZULIP_CERT_KEY") + if cert_bundle is None: + cert_bundle = os.environ.get("ZULIP_CERT_BUNDLE") + if config_file is None: config_file = get_default_config_filename() + if os.path.exists(config_file): config = SafeConfigParser() with open(config_file, 'r') as f: