2018-12-17 19:52:08 +01:00
|
|
|
# This file is used by both Python 2.7 (thumbor) and 3 (zulip).
|
2017-11-09 16:31:57 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import os
|
2018-03-08 09:37:09 +01:00
|
|
|
import re
|
2017-11-09 16:31:57 +01:00
|
|
|
import sys
|
2018-03-08 09:37:09 +01:00
|
|
|
from typing import Any, Text, Tuple, Optional
|
2017-11-09 16:31:57 +01:00
|
|
|
|
2020-04-12 19:44:42 +02:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2017-11-09 16:31:57 +01:00
|
|
|
sys.path.append(ZULIP_PATH)
|
|
|
|
|
|
|
|
# Piece of code below relating to secrets conf has been duplicated with that of
|
|
|
|
# django settings in zproject/settings.py
|
|
|
|
import six.moves.configparser
|
|
|
|
|
|
|
|
DEPLOY_ROOT = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..', '..')
|
|
|
|
|
|
|
|
config_file = six.moves.configparser.RawConfigParser()
|
|
|
|
config_file.read("/etc/zulip/zulip.conf")
|
|
|
|
|
|
|
|
# Whether this instance of Zulip is running in a production environment.
|
|
|
|
PRODUCTION = config_file.has_option('machine', 'deploy_type')
|
|
|
|
DEVELOPMENT = not PRODUCTION
|
|
|
|
|
|
|
|
secrets_file = six.moves.configparser.RawConfigParser()
|
|
|
|
if PRODUCTION:
|
|
|
|
secrets_file.read("/etc/zulip/zulip-secrets.conf")
|
|
|
|
else:
|
|
|
|
secrets_file.read(os.path.join(DEPLOY_ROOT, "zproject/dev-secrets.conf"))
|
|
|
|
|
2018-08-09 18:55:06 +02:00
|
|
|
def get_secret(key, default_value=None, development_only=False):
|
|
|
|
# type: (str, Optional[Any], bool) -> Optional[Any]
|
|
|
|
if development_only and PRODUCTION:
|
|
|
|
return default_value
|
2017-11-09 16:31:57 +01:00
|
|
|
if secrets_file.has_option('secrets', key):
|
|
|
|
return secrets_file.get('secrets', key)
|
2018-08-09 18:55:06 +02:00
|
|
|
return default_value
|
2017-11-09 16:31:57 +01:00
|
|
|
|
|
|
|
THUMBOR_EXTERNAL_TYPE = 'external'
|
|
|
|
THUMBOR_S3_TYPE = 's3'
|
|
|
|
THUMBOR_LOCAL_FILE_TYPE = 'local_file'
|
|
|
|
|
2018-03-08 09:37:09 +01:00
|
|
|
def separate_url_and_source_type(url):
|
|
|
|
# type: (Text) -> Tuple[Text, Text]
|
|
|
|
THUMBNAIL_URL_PATT = re.compile('^(?P<actual_url>.+)/source_type/(?P<source_type>.+)')
|
|
|
|
matches = THUMBNAIL_URL_PATT.match(url)
|
|
|
|
return (matches.group('source_type'), matches.group('actual_url'))
|