Browse Source

Add contexts.describe() for debugging

tags/v0.6
Ben Kurtovic 4 years ago
parent
commit
2a4e1f4316
2 changed files with 21 additions and 2 deletions
  1. +14
    -1
      mwparserfromhell/parser/contexts.py
  2. +7
    -1
      tests/test_pytokenizer.py

+ 14
- 1
mwparserfromhell/parser/contexts.py View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2017 Ben Kurtovic <ben.kurtovic@gmail.com>
# Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -193,3 +193,16 @@ UNSAFE = (TEMPLATE_NAME + WIKILINK_TITLE + EXT_LINK_TITLE +
DOUBLE = TEMPLATE_PARAM_KEY + TAG_CLOSE + TABLE_ROW_OPEN
NO_WIKILINKS = TEMPLATE_NAME + ARGUMENT_NAME + WIKILINK_TITLE + EXT_LINK_URI
NO_EXT_LINKS = TEMPLATE_NAME + ARGUMENT_NAME + WIKILINK_TITLE + EXT_LINK

def describe(context):
"""Return a string describing the given context value, for debugging."""
flags = []
for name, value in globals().items():
if not isinstance(value, int) or name.startswith("GL_"):
continue
if bin(value).count("1") != 1:
continue # Hacky way to skip aggregate contexts
if context & value:
flags.append((name, value))
flags.sort(key=lambda it: it[1])
return "|".join(it[0] for it in flags)

+ 7
- 1
tests/test_pytokenizer.py View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
# Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -23,6 +23,7 @@
from __future__ import unicode_literals
import unittest

from mwparserfromhell.parser import contexts
from mwparserfromhell.parser.tokenizer import Tokenizer

from ._test_tokenizer import TokenizerTestCase
@@ -40,5 +41,10 @@ class TestPyTokenizer(TokenizerTestCase, unittest.TestCase):
self.assertFalse(Tokenizer.USES_C)
self.assertFalse(Tokenizer().USES_C)

def test_describe_context(self):
self.assertEqual("", contexts.describe(0))
ctx = contexts.describe(contexts.TEMPLATE_PARAM_KEY|contexts.HAS_TEXT)
self.assertEqual("TEMPLATE_PARAM_KEY|HAS_TEXT", ctx)

if __name__ == "__main__":
unittest.main(verbosity=2)

Loading…
Cancel
Save