From 30ccf03fa849b5c6b550ab85aabce860a16b16cf Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Wed, 15 Feb 2023 22:05:29 -0600 Subject: add more testing and fix small bugs --- pymdvar/__init__.py | 3 --- pymdvar/pymdvar.py | 31 ------------------------------- pyproject.toml | 25 +++++++++++++++++++++---- requirements_dev.txt | 5 +++++ setup.cfg | 13 +++++++++++++ src/pymdvar/__init__.py | 3 +++ src/pymdvar/py.typed | 0 src/pymdvar/pymdvar.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_pymdvar.py | 9 +++++++-- 9 files changed, 93 insertions(+), 40 deletions(-) delete mode 100644 pymdvar/__init__.py delete mode 100644 pymdvar/pymdvar.py create mode 100644 requirements_dev.txt create mode 100644 src/pymdvar/__init__.py create mode 100644 src/pymdvar/py.typed create mode 100644 src/pymdvar/pymdvar.py diff --git a/pymdvar/__init__.py b/pymdvar/__init__.py deleted file mode 100644 index b64a3b6..0000000 --- a/pymdvar/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from .pymdvar import VariableExtension - -__all__ = ['VariableExtension'] \ No newline at end of file diff --git a/pymdvar/pymdvar.py b/pymdvar/pymdvar.py deleted file mode 100644 index 1173f20..0000000 --- a/pymdvar/pymdvar.py +++ /dev/null @@ -1,31 +0,0 @@ -from markdown.extensions import Extension -from markdown.inlinepatterns import Pattern - -VARIABLE_RE = r'(\$\{)(\w+)(\})' - - -class VariablePattern(Pattern): - def __init__(self, pattern, variables, md=None): - print(type(variables)) - self.variables = variables - super().__init__(pattern, md) - - def handleMatch(self, m): - variable = m.group(3) - value = '' - if variable in self.variables: - value = self.variables[variable] - return value - - -class VariableExtension(Extension): - def __init__(self, **kwargs): - self.config = { - 'enable_env': [False, 'Enable Environment variables parsing.'], - 'variables': [dict(), 'Dictionary holding variables to be used.'] - } - super().__init__(**kwargs) - - def extendMarkdown(self, md): - variable = VariablePattern(VARIABLE_RE, variables=self.getConfig('variables')) - md.inlinePatterns.register(variable, 'variable', 75) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index e74c9ec..dcaca35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,23 @@ [build-system] -requires = [ - "setuptools>=42", - "wheel" +requires = ["pbr>=5.7.0", "setuptools>=36.6.0"] +build-backend = "pbr.build" + +[tool.pytest.ini_options] +addopts = "--cov-report term-missing --cov=pymdvar" +testpaths = [ + "tests", ] -build-backend = "setuptools.build_meta" + +[tool.mypy] +mypy_path = "src" +check_untyped_defs = true +disallow_any_generics = true +ignore_missing_imports = true +no_implicit_optional = true +show_error_codes = true +strict_equality = true +warn_redundant_casts = true +warn_return_any = true +warn_unreachable = true +warn_unused_configs = true +no_implicit_reexport = true \ No newline at end of file diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..9979866 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,5 @@ +pytest>=7.2.1 +pytest-cov>=4.0.0 +mypy>=1.0.0 +flake8>=6.0.0 +types-Markdown>=3.4.2.4 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index e44d6c2..13f4adc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,5 +24,18 @@ packages = universal = 0 [options] +package_dir = + = src packages = find: include_package_data=True + +[options.packages.find] +where = src + +[options.package_data] +pymdvar = py.typed + +[flake8] +max-line-length = 80 +per-file-ignores = + __init__.py: W292 \ No newline at end of file diff --git a/src/pymdvar/__init__.py b/src/pymdvar/__init__.py new file mode 100644 index 0000000..b64a3b6 --- /dev/null +++ b/src/pymdvar/__init__.py @@ -0,0 +1,3 @@ +from .pymdvar import VariableExtension + +__all__ = ['VariableExtension'] \ No newline at end of file diff --git a/src/pymdvar/py.typed b/src/pymdvar/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/src/pymdvar/pymdvar.py b/src/pymdvar/pymdvar.py new file mode 100644 index 0000000..b6ea344 --- /dev/null +++ b/src/pymdvar/pymdvar.py @@ -0,0 +1,44 @@ +from os import getenv +from markdown.extensions import Extension +from markdown.inlinepatterns import Pattern + +VARIABLE_RE = r'(\$\{)([a-zA-Z_]*)(\})' + + +class VariablePattern(Pattern): + # need to redefine as an extra attribute needs to be passed + def __init__(self, pattern, variables, enable_env, md=None): + self.variables = variables + self.enable_env = enable_env + super().__init__(pattern, md) + + def handleMatch(self, m): + # for some reason the group is offest by 1 + variable = m.group(3) + value = '' + + if variable in self.variables: + value = self.variables[variable] + else: + if self.enable_env: + value = getenv(variable, '') + return value + + +class VariableExtension(Extension): + def __init__(self, **kwargs): + self.config = { + 'enable_env': [False, 'Enable environment variables parsing.'], + 'variables': [dict(), 'Dictionary holding variables to be used.'] + } + super().__init__(**kwargs) + + def extendMarkdown(self, md): + variables = self.getConfig('variables', dict()) + enable_env = self.getConfig('enable_env', False) + variable_pattern = VariablePattern(VARIABLE_RE, variables, enable_env) + md.inlinePatterns.register(variable_pattern, 'variable', 175) + + +def makeExtension(*args, **kwargs): + return VariableExtension(*args, **kwargs) diff --git a/tests/test_pymdvar.py b/tests/test_pymdvar.py index 2e305ec..69ffe56 100644 --- a/tests/test_pymdvar.py +++ b/tests/test_pymdvar.py @@ -14,7 +14,10 @@ def test_empty_input(): @pytest.mark.parametrize('in_str, exp_str', [ ('foo bar', '

foo bar

'), ('foo *test* bar', '

foo test bar

'), - ('foo **test** bar', '

foo test bar

') + ('foo **test** bar', '

foo test bar

'), + ('foo $test bar', '

foo $test bar

'), + ('foo *${test* bar', '

foo ${test bar

'), + ('foo **$test}** bar', '

foo $test} bar

'), ]) def test_non_replacements(in_str, exp_str): out_str = markdown(in_str, extensions=[VariableExtension()]) @@ -24,7 +27,9 @@ def test_non_replacements(in_str, exp_str): @pytest.mark.parametrize('in_str, exp_str', [ ('foo ${test} bar', '

foo value bar

'), ('foo *${test}* bar', '

foo value bar

'), - ('foo **${test}** bar', '

foo value bar

') + ('foo **${test}** bar', '

foo value bar

'), + ('foo [link](${test}/a.html) bar', '

foo link bar

'), + ('foo ![image](${test}/a.jpg) bar', '

foo image bar

'), ]) def test_simple_replacements(in_str, exp_str): out_str = markdown(in_str, extensions=[VariableExtension(variables={'test':'value'})]) -- cgit v1.2.3-54-g00ecf