Skip to content

Commit

Permalink
chore: update codebase for modern python (#244)
Browse files Browse the repository at this point in the history
* chore: update codebase for modern python

We're on Python 3.7 and higher, so we can clean up any artifacts left
from previously-supported Python eras.

Most of the heavy lifting was done with `pyupgrade` followed by unused
improts removal (thanks to `flake8` check)

Refs: https://pypi.org/project/pyupgrade/

Closes #203

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* refactor: replace `os.path`/`glob` with `pathlib`

Since Python 3.4, `pathlib` provides a nicer interface to manipulate
paths, removing the need to split and reassemble strings.

The addition of `pytest.param(..., id=fn.name)` is to set the test name
output to a human-friendly string in the event of failure or `--verbose`

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore: replace deprecated setuptools entry

As of setuptools v56.0.0, the `license_file` entry is deprecated,
raising `SetuptoolsDeprecationWarning` during the `packaging` step.

Refs: pypa/setuptools#2620

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman committed Jul 6, 2022
1 parent 209bcae commit bb88dab
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 59 deletions.
1 change: 0 additions & 1 deletion readme_renderer/__about__.py
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function

__all__ = [
"__title__",
Expand Down
1 change: 0 additions & 1 deletion readme_renderer/__init__.py
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function
1 change: 0 additions & 1 deletion readme_renderer/__main__.py
@@ -1,4 +1,3 @@
from __future__ import absolute_import, print_function
import argparse
from readme_renderer.rst import render
import sys
Expand Down
1 change: 0 additions & 1 deletion readme_renderer/clean.py
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function

import functools
from typing import Any, Dict, Iterator, List, Optional
Expand Down
3 changes: 1 addition & 2 deletions readme_renderer/markdown.py
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function

import re
import warnings
Expand Down Expand Up @@ -117,7 +116,7 @@ def replacer(match: Match[Any]) -> str:

highlighted = pygments.highlight(code, lexer, formatter)

return '<pre>{}</pre>'.format(highlighted)
return f'<pre>{highlighted}</pre>'

result = code_expr.sub(replacer, html)

Expand Down
3 changes: 1 addition & 2 deletions readme_renderer/rst.py
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function

import io
from typing import Any, Dict, IO, Optional, Union
Expand Down Expand Up @@ -43,7 +42,7 @@ def emptytag(
if "height" in node:
attributes["height"] = node["height"]

return super(ReadMeHTMLTranslator, self).emptytag(
return super().emptytag(
node, tagname, suffix, **attributes
)

Expand Down
10 changes: 1 addition & 9 deletions readme_renderer/txt.py
Expand Up @@ -11,20 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function

import sys
from typing import Any, Optional

from .clean import clean

if sys.version_info >= (3,):
from html import escape as html_escape
else:
from cgi import escape

def html_escape(s):
return escape(s, quote=True).replace("'", '&#x27;')
from html import escape as html_escape


def render(raw: str, **kwargs: Any) -> Optional[str]:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
@@ -1,2 +1,2 @@
[metadata]
license_file = LICENSE
license_files = LICENSE
9 changes: 4 additions & 5 deletions setup.py
Expand Up @@ -11,19 +11,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function

import os
import pathlib

import setuptools

base_dir = os.path.dirname(__file__)
base_dir = pathlib.Path(__file__).parent

with open(os.path.join(base_dir, "readme_renderer", "__about__.py")) as f:
with open(base_dir.joinpath("readme_renderer", "__about__.py")) as f:
about = {}
exec(f.read(), about)

with open(os.path.join(base_dir, "README.rst")) as f:
with open(base_dir.joinpath("README.rst")) as f:
long_description = f.read()


Expand Down
27 changes: 8 additions & 19 deletions tests/test_markdown.py
@@ -1,36 +1,25 @@
import io
import glob
import os
from pathlib import Path

import pytest

from readme_renderer.markdown import render, variants


MD_FIXTURES = [
(fn, os.path.splitext(fn)[0] + ".html", variant)
for variant in variants
for fn in glob.iglob(
os.path.join(
os.path.dirname(__file__),
"fixtures",
"test_" + variant + "*.md"
)
)
]


@pytest.mark.parametrize(
("md_filename", "html_filename", "variant"),
MD_FIXTURES,
[
(pytest.param(fn, fn.with_suffix(".html"), variant, id=fn.name))
for variant in variants
for fn in Path(__file__).parent.glob(f"fixtures/test_{variant}*.md")
],
)
def test_md_fixtures(md_filename, html_filename, variant):
# Get our Markup
with io.open(md_filename, encoding='utf-8') as f:
with open(md_filename, encoding='utf-8') as f:
md_markup = f.read()

# Get our expected
with io.open(html_filename, encoding="utf-8") as f:
with open(html_filename, encoding="utf-8") as f:
expected = f.read()

assert render(md_markup, variant=variant) == expected
Expand Down
13 changes: 5 additions & 8 deletions tests/test_rst.py
@@ -1,6 +1,5 @@
import io
import glob
import os.path
from pathlib import Path

import pytest

Expand All @@ -10,19 +9,17 @@
@pytest.mark.parametrize(
("rst_filename", "html_filename"),
[
(fn, os.path.splitext(fn)[0] + ".html")
for fn in glob.glob(
os.path.join(os.path.dirname(__file__), "fixtures", "test_*.rst")
)
(pytest.param(fn, fn.with_suffix(".html"), id=fn.name))
for fn in Path(__file__).parent.glob("fixtures/test_*.rst")
],
)
def test_rst_fixtures(rst_filename, html_filename):
# Get our Markup
with io.open(rst_filename, encoding='utf-8') as f:
with open(rst_filename, encoding='utf-8') as f:
rst_markup = f.read()

# Get our expected
with io.open(html_filename, encoding="utf-8") as f:
with open(html_filename, encoding="utf-8") as f:
expected = f.read()

out = render(rst_markup)
Expand Down
14 changes: 5 additions & 9 deletions tests/test_txt.py
@@ -1,6 +1,4 @@
import io
import glob
import os.path
from pathlib import Path

import pytest

Expand All @@ -10,19 +8,17 @@
@pytest.mark.parametrize(
("txt_filename", "html_filename"),
[
(fn, os.path.splitext(fn)[0] + ".html")
for fn in glob.glob(
os.path.join(os.path.dirname(__file__), "fixtures", "test_*.txt")
)
(pytest.param(fn, fn.with_suffix(".html"), id=fn.name))
for fn in Path(__file__).parent.glob("fixtures/test_*.txt")
],
)
def test_txt_fixtures(txt_filename, html_filename):
# Get our Markup
with io.open(txt_filename, encoding='utf-8') as f:
with open(txt_filename, encoding='utf-8') as f:
txt_markup = f.read()

# Get our expected
with io.open(html_filename, encoding="utf-8") as f:
with open(html_filename, encoding="utf-8") as f:
expected = f.read()

out = render(txt_markup)
Expand Down

0 comments on commit bb88dab

Please sign in to comment.