From 52fea63106fcd49a9011b6d5178529491ed2c53b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 28 Jul 2016 16:01:27 +0200 Subject: [PATCH] Allow to use the library without requests installed --- pytaxonomies/api.py | 9 ++++++++- tests/tests.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pytaxonomies/api.py b/pytaxonomies/api.py index 4f3c77c..d4cc0c9 100644 --- a/pytaxonomies/api.py +++ b/pytaxonomies/api.py @@ -2,11 +2,16 @@ # -*- coding: utf-8 -*- import json -import requests import os import collections import re +try: + import requests + HAS_REQUESTS = True +except ImportError: + HAS_REQUESTS = False + class Entry(): @@ -132,6 +137,8 @@ class Taxonomies(collections.Mapping): return json.load(f) def __load_url(self, url): + if not HAS_REQUESTS: + raise Exception("Python module 'requests' isn't installed, unable to fetch the taxonomies.") return requests.get(url).json() def __make_uri(self, taxonomy_name): diff --git a/tests/tests.py b/tests/tests.py index 3785fbf..ad96932 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -3,6 +3,7 @@ import unittest from pytaxonomies import Taxonomies +import pytaxonomies.api class TestPyTaxonomies(unittest.TestCase): @@ -44,6 +45,13 @@ class TestPyTaxonomies(unittest.TestCase): def test_amountEntries(self): list(self.taxonomies.values())[0].amount_entries() + def test_missingDependency(self): + pytaxonomies.api.HAS_REQUESTS = False + with self.assertRaises(Exception): + Taxonomies() + Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json") + pytaxonomies.api.HAS_REQUESTS = True + if __name__ == "__main__": unittest.main()