def test_override_dict(self): new_dict = override_dict(self.parent, self.child) self.assertEqual(new_dict, { "test": "test", "test1": "child", "test2": "child2" }) self.assertIsNot(new_dict, self.parent) new_dict = override_dict({}, self.child) self.assertEqual(new_dict, self.child) self.assertIsNot(new_dict, self.parent) new_dict = override_dict(self.parent, {}) self.assertEqual(new_dict, self.parent) self.assertIsNot(new_dict, self.parent)
def test_invalid_input(self): try: override_dict(self.parent, "child") self.fail( "override_dict did not fail when receiving other type than dict as child" ) except AssertionError: pass try: override_dict(["parent"], self.child) self.fail( "override_dict did not fail when receiving other type than dict as parent" ) except AssertionError: pass
class WikipediaCategories(WikipediaQuery): PARAMETERS = override_dict( WikipediaQuery.PARAMETERS, { "cllimit": 500, "clshow": "", # gets set at runtime through "wiki_show_categories" config with "!hidden" by default "prop": "categories" # generator style: info|pageprops|categoryinfo }) def variables(self, *args): show = args[3] if len(args) > 3 else self.PARAMETERS["clshow"] variables = super(WikipediaCategories, self).variables(*args[:3]) variables["show"] = show return variables def parameters(self, **kwargs): params = dict(self.PARAMETERS) params["clshow"] = self.config.wiki_show_categories return params class Meta: verbose_name = "Wikipedia category" verbose_name_plural = "Wikipedia categories"
class WikipediaQuery(WikipediaAPI): URI_TEMPLATE = 'https://{}.wikipedia.org/w/api.php?{}={}' PARAMETERS = override_dict(WikipediaAPI.PARAMETERS, { "action": "query", "redirects": "1", }) GET_SCHEMA = { "args": {}, "kwargs": None } WIKI_RESULTS_KEY = "pages" WIKI_QUERY_PARAM = "titles" ERROR_MESSAGE = "We did not find the page you were looking for. Perhaps you should create it?" def send(self, method, *args, **kwargs): args = (self.config.wiki_country, self.WIKI_QUERY_PARAM,) + args return super(WikipediaQuery, self).send(method, *args, **kwargs) def handle_errors(self): super(WikipediaQuery, self).handle_errors() # Check general response content_type, data = self.content if "query" not in data: raise DGInvalidResource('Wrongly formatted Wikipedia response, missing "query"', resource=self) response = data['query'][self.WIKI_RESULTS_KEY] # Wiki has response hidden under single keyed dicts :( # When searching for pages a dictionary gets returned # TODO: parse the dictionary and possibly return partial content if isinstance(response, dict) and "-1" in response and "missing" in response["-1"]: self.status = 404 raise DGHttpError40X(self.ERROR_MESSAGE, resource=self) # When making lists a list is returned elif isinstance(response, list) and not response: self.status = 404 raise DGHttpError40X(self.ERROR_MESSAGE, resource=self) @property def content(self): content_type, data = super(WikipediaQuery, self).content if "warnings" in data: del(data["warnings"]) return content_type, data def get_wikipedia_json(self): # TODO: remove this method and its uses when a partial content is possible response = json.loads(self.body) return response["query"][self.WIKI_RESULTS_KEY] def next_parameters(self): content_type, data = self.content return data.get("continue", {}) class Meta: abstract = True
def test_override_dict_deep(self): self.parent["deep"] = {"constant": True, "variable": False} self.child["deep"] = {"variable": True} new_dict = override_dict(self.parent, self.child) self.assertEqual( new_dict, { "test": "test", "test1": "child", "test2": "child2", "deep": { # NB: deletes the constant key from parent!! "variable": True } }) self.assertIsNot(new_dict, self.parent)
class WikipediaCategoryMembers(WikipediaGenerator): PARAMETERS = override_dict( WikipediaGenerator.PARAMETERS, { "generator": "categorymembers", "gcmlimit": 100, "gcmnamespace": 0, "prop": "info|pageprops|categories|revisions", "clshow": "!hidden", "cllimit": 500, "rvprop": "content", "rvslots": "main" }) WIKI_QUERY_PARAM = "gcmtitle" class Meta: verbose_name = "Wikipedia category members" verbose_name_plural = "Wikipedia category members"