Skip to content

Commit

Permalink
Switch to env variables for API settings
Browse files Browse the repository at this point in the history
  • Loading branch information
eloquence committed Aug 19, 2020
1 parent eccfba2 commit 9964ce0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/development/i18n.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ generate using this command in the base directory:
$ LOCALES=en_US make translation-test
`Obtain your API key <https://weblate.securedrop.org/accounts/profile/#api>`__
in Weblate. Export the token to the environment variable ``WEBLATE_TOKEN``.
in Weblate. Export the token to the environment variable ``WEBLATE_API_TOKEN``.
You can now run this command to perform an upload:

.. code:: sh
$ securedrop/upload-screenshots.py --token $WEBLATE_TOKEN
$ securedrop/upload-screenshots.py
If new screenshots were added as part of this run, make sure to associate them
with relevant strings in Weblate, which you can do from the
Expand Down
45 changes: 18 additions & 27 deletions securedrop/upload-screenshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from urllib.parse import urljoin

import os
import argparse
import re
import requests
import sys
Expand Down Expand Up @@ -40,23 +39,13 @@ def main():
Uses the generic WeblateUploader class below to run a SecureDrop screenshot
upload.
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--token",
help="API token for accessing the weblate API",
type=str,
required=True,
)
parser.add_argument(
"--baseURL",
help="Weblate base URL",
default=DEFAULT_BASE_URL,
type=str,
required=False,
)
args = parser.parse_args()
token = os.getenv("WEBLATE_API_TOKEN", None)
base_url = os.getenv("WEBLATE_BASE_URL", DEFAULT_BASE_URL)

if token is None:
raise BadOrMissingTokenError(
"No token provided via WEBLATE_API_TOKEN environment variable.", base_url
)

screenshot_files = glob(os.path.join(SCREENSHOTS_DIRECTORY, SCREENSHOTS_GLOB))
if len(screenshot_files) == 0:
Expand All @@ -69,8 +58,8 @@ def main():
sys.exit(1)

uploader = WeblateUploader(
token=args.token,
base_url=args.baseURL,
token=token,
base_url=base_url,
project=PROJECT_SLUG,
component=COMPONENT_SLUG,
files=screenshot_files,
Expand Down Expand Up @@ -98,11 +87,8 @@ def __init__(
):

if len(token) != 40:
msg = (
"API token is not in expected 40 character format.\n"
"Obtain token via {}".format(urljoin(base_url, "accounts/profile/#api"))
)
raise BadTokenError(msg)
reason = "API token is not in expected 40 character format."
raise BadOrMissingTokenError(reason, base_url)

self.base_url = base_url
self.screenshots_endpoint = urljoin(base_url, "/api/screenshots/")
Expand Down Expand Up @@ -204,8 +190,13 @@ def upload(self, check_existing_screenshots=True):
print("Upload complete. Visit {} to review the results.".format(result_url))


class BadTokenError(Exception):
pass
class BadOrMissingTokenError(Exception):
def __init__(self, reason="Bad or missing token.", base_url=None):
if base_url is not None:
reason += " Obtain token via {}".format(
urljoin(base_url, "accounts/profile/#api")
)
super().__init__(reason)


class RequestLimitError(Exception):
Expand Down

0 comments on commit 9964ce0

Please sign in to comment.