Skip to content

Commit 4dddd87

Browse files
Niloth-ptimabbott
authored andcommitted
google-calendar: Add --provision argument to install dependencies.
Leveraged the provisioning support provided by `init_from_options`. Enabled the `--provision` argument by setting `allow_provisiong` to True in `add_default_arguments`. Re-ordered the script such that the imports are executed after `init_from_options`. Updated the import error message to recommend using the command with the --provision argument to install the dependencies.
1 parent 2a287e3 commit 4dddd87

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

zulip/integrations/google/google-calendar

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ from typing import List, Optional, Set, Tuple
1111
import dateutil.parser
1212
import pytz
1313

14-
try:
15-
from google.oauth2.credentials import Credentials
16-
from googleapiclient.discovery import build
17-
except ImportError:
18-
logging.exception("Install the required python packages from requirements.txt first.")
19-
sys.exit(1)
20-
2114
sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))
2215
import zulip
2316

@@ -49,7 +42,7 @@ usage = r"""google-calendar --user EMAIL [--interval MINUTES] [--calendar CALEND
4942
For more information, see https://zulip.com/integrations/doc/google-calendar.
5043
"""
5144

52-
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage))
45+
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage=usage), allow_provisioning=True)
5346
parser.add_argument(
5447
"--interval",
5548
default=30,
@@ -64,11 +57,24 @@ parser.add_argument(
6457
)
6558
options = parser.parse_args()
6659

60+
zulip_client = zulip.init_from_options(options)
61+
62+
# Import dependencies only after parsing command-line args,
63+
# as the --provision flag can be used to install the dependencies.
64+
try:
65+
from google.oauth2.credentials import Credentials
66+
from googleapiclient.discovery import build
67+
except ImportError:
68+
logging.exception(
69+
"You have unsatisfied dependencies. Install all missing dependencies with %(command)s --provision",
70+
{"command": sys.argv[0]},
71+
)
72+
sys.exit(1)
73+
74+
6775
if not options.zulip_email:
6876
parser.error("You must specify --user")
6977

70-
zulip_client = zulip.init_from_options(options)
71-
7278

7379
def get_credentials() -> Credentials:
7480
"""Gets valid user credentials from storage.

0 commit comments

Comments
 (0)