예제 #1
0
파일: page.py 프로젝트: JJMC89/bsiconsbot
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
예제 #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)
예제 #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)
예제 #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)
예제 #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
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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',
     )
예제 #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)
예제 #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',
     )
예제 #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,
     )
예제 #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],
     )
예제 #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',
     )
예제 #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],
     )
예제 #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)
예제 #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],
     )
예제 #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],
     )
예제 #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,
     )
예제 #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',
     )
예제 #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,
     )
예제 #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,
     )
예제 #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)
예제 #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)
예제 #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',
     )
예제 #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',
     )
예제 #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)
예제 #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)
예제 #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',
     )
예제 #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',
     )
예제 #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]
예제 #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))
예제 #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)
예제 #37
0
 def test_node_location_with_value_not_found_node(self):
     config = loads_config('{}')
     self.assertRaises(JSONConfigValueNotFoundError, node_location,
                       config.a)
예제 #38
0
 def test_object_and_standard_json_datatype_loading(self):
     obj = loads_config(TEST_JSON_STRING)
     self.assertDictEqual(obj(), TEST_JSON_VALUE)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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))
예제 #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))
예제 #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))
예제 #47
0
 def test_node_location_with_value_not_found_node(self):
     config = loads_config('{}')
     self.assertRaises(JSONConfigValueNotFoundError, node_location, config.a)
예제 #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)
예제 #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))
예제 #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)
예제 #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))
예제 #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))
예제 #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))
예제 #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])
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #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)
예제 #60
0
 def __init__(self, json_string):
     self.logs = []
     self.cfg = loads_config(json_string)