Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
 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',
     )
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
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',
     )
Ejemplo n.º 15
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,
     )
Ejemplo n.º 16
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],
     )
Ejemplo n.º 17
0
 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',
     )
Ejemplo n.º 18
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],
     )
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
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],
     )
Ejemplo n.º 21
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],
     )
Ejemplo n.º 22
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,
     )
Ejemplo n.º 23
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',
     )
Ejemplo n.º 24
0
 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,
     )
Ejemplo n.º 25
0
 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,
     )
Ejemplo n.º 26
0
 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)
Ejemplo n.º 27
0
 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)
Ejemplo n.º 28
0
 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',
     )
Ejemplo n.º 29
0
 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',
     )
Ejemplo n.º 30
0
    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)
Ejemplo n.º 31
0
    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)
Ejemplo n.º 32
0
 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',
     )
Ejemplo n.º 33
0
 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',
     )
Ejemplo n.º 34
0
 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]
Ejemplo n.º 35
0
 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))
Ejemplo n.º 36
0
 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)
Ejemplo n.º 37
0
 def test_node_location_with_value_not_found_node(self):
     config = loads_config('{}')
     self.assertRaises(JSONConfigValueNotFoundError, node_location,
                       config.a)
Ejemplo n.º 38
0
 def test_object_and_standard_json_datatype_loading(self):
     obj = loads_config(TEST_JSON_STRING)
     self.assertDictEqual(obj(), TEST_JSON_VALUE)
Ejemplo n.º 39
0
 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)
Ejemplo n.º 40
0
 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)
Ejemplo n.º 41
0
 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)
Ejemplo n.º 42
0
 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)
Ejemplo n.º 43
0
 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)
Ejemplo n.º 44
0
 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))
Ejemplo n.º 45
0
 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))
Ejemplo n.º 46
0
 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))
Ejemplo n.º 47
0
 def test_node_location_with_value_not_found_node(self):
     config = loads_config('{}')
     self.assertRaises(JSONConfigValueNotFoundError, node_location, config.a)
Ejemplo n.º 48
0
 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)
Ejemplo n.º 49
0
 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))
Ejemplo n.º 50
0
 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)
Ejemplo n.º 51
0
 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))
Ejemplo n.º 52
0
 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))
Ejemplo n.º 53
0
 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))
Ejemplo n.º 54
0
 def test_root_is_array(self):
     lst = loads_config('[0, 1, 2]', JSONParserParams(root_is_array=True))
     self.assertListEqual(lst(), [0, 1, 2])
Ejemplo n.º 55
0
 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)
Ejemplo n.º 56
0
 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)
Ejemplo n.º 57
0
 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)
Ejemplo n.º 58
0
 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)
Ejemplo n.º 59
0
 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)
Ejemplo n.º 60
0
 def __init__(self, json_string):
     self.logs = []
     self.cfg = loads_config(json_string)