def load_config(page: pywikibot.Page, **kwargs: Any) -> ConfigJSONObject: """Load JSON config from the page.""" if page.isRedirectPage(): pywikibot.log(f"{page!r} is a redirect.") page = page.getRedirectTarget() _empty = jsoncfg.loads_config("{}") if not page.exists(): pywikibot.log(f"{page!r} does not exist.") return _empty try: return jsoncfg.loads_config(page.get(**kwargs).strip()) except pywikibot.exceptions.PageRelatedError: return _empty
def test_require_string(self): config = loads_config(TEST_JSON_STRING) self.assertEqual(config.str(require_string), 'strval') self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.true), require_string) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.true), None, require_string)
def test_require_integer(self): config = loads_config(TEST_JSON_STRING) self.assertIsInstance(config.int(require_integer), int) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), require_integer) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), None, require_integer)
def test_require_array(self): config = loads_config(TEST_JSON_STRING) self.assertIsInstance(config.array(require_array), list) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), require_array) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), None, require_array)
def analyze_policy_string( policy_str, filepath=None, ignore_private_auditors=False, private_auditors_custom_path=None, include_community_auditors=False, config=None, ): """Given a string reperesenting a policy, convert it to a Policy object with findings""" try: # TODO Need to write my own json parser so I can track line numbers. See https://stackoverflow.com/questions/7225056/python-json-decoding-library-which-can-associate-decoded-items-with-original-li policy_json = jsoncfg.loads_config(policy_str) except jsoncfg.parser.JSONConfigParserException as e: policy = Policy(None) policy.add_finding("MALFORMED_JSON", detail="json parsing error: {}".format(e), location={ 'line': e.line, 'column': e.column }) return policy policy = Policy(policy_json, filepath, config) policy.analyze( ignore_private_auditors, private_auditors_custom_path, include_community_auditors, ) return policy
def test_require_number(self): config = loads_config(TEST_JSON_STRING) self.assertIsInstance(config.int(require_number), int) self.assertIsInstance(config.float(require_number), float) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), require_number) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), None, require_number)
def test_expect_object(self): config = loads_config('{a:0}') self.assertIs(config, expect_object(config)) self.assertRaises(JSONConfigValueNotFoundError, expect_object, config.b) self.assertRaises(JSONConfigNodeTypeError, expect_object, config.a) self.assertRaises(TypeError, expect_object, None)
def test_expect_array(self): config = loads_config('{a:[0]}') self.assertIs(config.a, expect_array(config.a)) self.assertRaises(JSONConfigValueNotFoundError, expect_array, config.not_found) self.assertRaises(JSONConfigNodeTypeError, expect_array, config.a[0]) self.assertRaises(TypeError, expect_array, None)
def test_expect_scalar(self): config = loads_config('{a:[0]}') self.assertIs(config.a[0], expect_scalar(config.a[0])) self.assertRaises(JSONConfigValueNotFoundError, expect_scalar, config.not_found) self.assertRaises(JSONConfigNodeTypeError, expect_scalar, config) self.assertRaises(TypeError, expect_scalar, None)
def test_require_bool(self): config = loads_config(TEST_JSON_STRING) self.assertTrue(config.true(require_bool)) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), require_bool) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), None, require_bool)
def test_require_object(self): config = loads_config(TEST_JSON_STRING) self.assertIsInstance(config.obj(require_object), dict) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), require_object) self.assertRaises(JSONConfigValueMapperError, WrapCallable(config.str), None, require_object)
def test_getattr(self): config = loads_config(TEST_JSON_STRING) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to get an item from an array as if it was an object\.', config.array.__getattr__, 'item', )
def test_indexing(self): config = loads_config('{}') not_found_node = config.woof self.assertIsInstance(not_found_node, ValueNotFoundNode) case0 = not_found_node[100][100] self.assertRaises(JSONConfigValueNotFoundError, case0) case1 = not_found_node['a']['b'] self.assertRaises(JSONConfigValueNotFoundError, case1)
def test_getattr(self): config = loads_config('[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to get an item from a scalar as if it was an object\.', config[0].__getattr__, 'woof', )
def test_contains(self): config = loads_config('[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to access the __contains__ magic method of a scalar config object\.', config[0].__contains__, 0, )
def test_len(self): config = loads_config('[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to access the __len__ of a scalar config object\.', len, config[0], )
def test_iter(self): config = loads_config('[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to iterate a scalar value\.', iter, config[0], )
def test_len(self): config = loads_config( '[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to access the __len__ of a scalar config object\.', len, config[0], )
def test_iter(self): config = loads_config( '[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to iterate a scalar value\.', iter, config[0], )
def test_contains(self): config = loads_config( '[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to access the __contains__ magic method of a scalar config object\.', config[0].__contains__, 0, )
def test_getattr(self): config = loads_config( '[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to get an item from a scalar as if it was an object\.', config[0].__getattr__, 'woof', )
def test_getitem_with_negative_index(self): config = loads_config(TEST_JSON_STRING) self.assertDictEqual(config.array[-1](), {'b': 1}) self.assertDictEqual(config.array[-2](), {'a': 0}) self.assertRaises( JSONConfigIndexError, config.array.__getitem__, -3, ) self.assertRaises( JSONConfigIndexError, config.array.__getitem__, 2, )
def test_getitem(self): config = loads_config(TEST_JSON_STRING) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to index into an object as if it was an array\.', config.__getitem__, 0, ) self.assertRaisesRegexp( TypeError, r'You are allowed to index only with string or integer\.', config.__getitem__, None, ) self.assertIsInstance(config.not_found, ValueNotFoundNode)
def test_getitem(self): config = loads_config(TEST_JSON_STRING) self.assertEquals(config.array[0].a(), 0) self.assertEquals(config.array[1].b(), 1) self.assertRaisesRegexp( TypeError, r'You are allowed to index only with string or integer\.', config.array.__getitem__, None, ) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to get an item from an array as if it was an object\.', config.array.__getitem__, 'item', )
def test_value_fetch(self): config = loads_config('{a:0, b:{}, c:[]}') scalar = config.a obj = config.b array = config.c not_found = config.d self.assertIsInstance(scalar, ConfigJSONScalar) self.assertIsInstance(obj, ConfigJSONObject) self.assertIsInstance(array, ConfigJSONArray) self.assertIsInstance(not_found, ValueNotFoundNode) default = object() self.assertEqual(scalar(default), 0) self.assertEqual(obj(default), {}) self.assertEqual(array(default), []) self.assertEqual(not_found(default), default)
def test_getitem(self): config = loads_config('[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( TypeError, r'You are allowed to index only with string or integer\.', config[0].__getitem__, None, ) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to index into a scalar as if it was an array\.', config[0].__getitem__, 0, ) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to get an item from a scalar as if it was an object\.', config[0].__getitem__, 'item', )
def test_getitem(self): config = loads_config( '[0]', parser_params=JSONParserParams(root_is_array=True)) self.assertRaisesRegexp( TypeError, r'You are allowed to index only with string or integer\.', config[0].__getitem__, None, ) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to index into a scalar as if it was an array\.', config[0].__getitem__, 0, ) self.assertRaisesRegexp( JSONConfigNodeTypeError, r'You are trying to get an item from a scalar as if it was an object\.', config[0].__getitem__, 'item', )
def site_config(self) -> SiteConfig | None: """Return the site configuration.""" site = self.current_page.site if site in self._config: return self._config[site] try: config_json = load_config(Page(site, self.opt.local_config)) config_dict1 = process_local_config(config_json) config_dict2 = {k: v for k, v in config_dict1.items() if v} config_str = json.dumps({**self.opt.config, **config_dict2}) config_json = jsoncfg.loads_config(config_str) self._config[site] = process_site_config(config_json, site) except ( ValueError, jsoncfg.JSONConfigException, pywikibot.exceptions.Error, ) as e: pywikibot.error(f"Invalid config for {site}.") pywikibot.log(e) self._config[site] = None return self._config[site]
def test_node_exists(self): config = loads_config('{k0:0}') self.assertTrue(node_exists(config)) self.assertTrue(node_exists(config.k0)) self.assertFalse(node_exists(config.not_found))
def test_node_location(self): config = loads_config('\n{k0:0}') location = node_location(config) self.assertEquals(location, (2, 1)) self.assertEquals(location.line, 2) self.assertEquals(location.column, 1)
def test_node_location_with_value_not_found_node(self): config = loads_config('{}') self.assertRaises(JSONConfigValueNotFoundError, node_location, config.a)
def test_object_and_standard_json_datatype_loading(self): obj = loads_config(TEST_JSON_STRING) self.assertDictEqual(obj(), TEST_JSON_VALUE)
def test_value_fetch_not_json_value_mapper_instance(self): config = loads_config('{a:0}') self.assertRaisesRegexp(TypeError, r'1 isn\'t a JSONValueMapper instance!', WrapCallable(config.a), 0, 1)
def test_ensure_exists(self): config = loads_config('{}') self.assertIs(config, ensure_exists(config)) self.assertRaises(JSONConfigValueNotFoundError, ensure_exists, config.a) self.assertRaises(TypeError, ensure_exists, None)
def test_node_is_object(self): config = loads_config('{k0:0}') self.assertTrue(node_is_object(config)) self.assertFalse(node_is_object(config.k0)) self.assertFalse(node_is_object(config.not_found))
def test_node_is_scalar(self): config = loads_config('{a:[0]}') self.assertFalse(node_is_scalar(config.a)) self.assertTrue(node_is_scalar(config.a[0])) self.assertFalse(node_is_scalar(config.not_found))
def test_iter(self): config = loads_config('{}') not_found_node = config.woof self.assertIsInstance(not_found_node, ValueNotFoundNode) self.assertRaises(JSONConfigValueNotFoundError, lambda: iter(not_found_node))
def test_root_is_array(self): lst = loads_config('[0, 1, 2]', JSONParserParams(root_is_array=True)) self.assertListEqual(lst(), [0, 1, 2])
def __init__(self, json_string): self.logs = [] self.cfg = loads_config(json_string)