diff options
author | Erik Johnston <erik@matrix.org> | 2016-04-13 11:57:46 +0100 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2016-04-13 13:38:09 +0100 |
commit | d0633e6dbe1e62bd9def0554e6a063a034ce3248 (patch) | |
tree | b0d59ebd9fad64057617728f2a31ff9c32ff204d /synapse/config/repository.py | |
parent | Update replication.rst (diff) | |
download | synapse-d0633e6dbe1e62bd9def0554e6a063a034ce3248.tar.xz |
Sanitize the optional dependencies for spider API
Diffstat (limited to 'synapse/config/repository.py')
-rw-r--r-- | synapse/config/repository.py | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/synapse/config/repository.py b/synapse/config/repository.py index 49922c6d03..82ff8df59c 100644 --- a/synapse/config/repository.py +++ b/synapse/config/repository.py @@ -13,10 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ._base import Config +from ._base import Config, ConfigError from collections import namedtuple -import sys + +MISSING_NETADDR = ( + "Missing netaddr library. This is required for URL preview API." +) + +MISSING_LXML = ( + "Missing lxml library. This is required for URL preview API." +) + ThumbnailRequirement = namedtuple( "ThumbnailRequirement", ["width", "height", "method", "media_type"] @@ -62,18 +70,28 @@ class ContentRepositoryConfig(Config): self.thumbnail_requirements = parse_thumbnail_requirements( config["thumbnail_sizes"] ) - self.url_preview_enabled = config["url_preview_enabled"] + self.url_preview_enabled = config.get("url_preview_enabled", False) if self.url_preview_enabled: try: + import lxml + lxml # To stop unused lint. + except ImportError: + raise ConfigError(MISSING_LXML) + + try: from netaddr import IPSet - if "url_preview_ip_range_blacklist" in config: - self.url_preview_ip_range_blacklist = IPSet( - config["url_preview_ip_range_blacklist"] - ) - if "url_preview_url_blacklist" in config: - self.url_preview_url_blacklist = config["url_preview_url_blacklist"] except ImportError: - sys.stderr.write("\nmissing netaddr dep - disabling preview_url API\n") + raise ConfigError(MISSING_NETADDR) + + if "url_preview_ip_range_blacklist" in config: + self.url_preview_ip_range_blacklist = IPSet( + config["url_preview_ip_range_blacklist"] + ) + else: + raise ConfigError("url_preview_url_blacklist is required") + + if "url_preview_url_blacklist" in config: + self.url_preview_url_blacklist = config["url_preview_url_blacklist"] def default_config(self, **kwargs): media_store = self.default_path("media_store") |