Skip to content

Commit a31a538

Browse files
committed
release: 2023
1 parent b243dbe commit a31a538

File tree

11 files changed

+125
-104
lines changed

11 files changed

+125
-104
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ repos:
2828
rev: v3.3.1
2929
hooks:
3030
- id: pyupgrade
31-
args: [--py37-plus]
31+
args: [--py38-plus]
3232
- repo: https://github.com/boidolr/pre-commit-images
3333
rev: v1.1.0
3434
hooks:

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
All major and minor version changes will be documented in this file. Details of
44
patch-level version changes can be found in [commit messages](../../commits/master).
55

6+
## 2023 - 2023/01/08
7+
8+
- Add tox config
9+
- Use playwright and install_playwright in-place of pyppeteer
10+
611
## 2022 - 2022/01/06
712

813
- update precommit

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
[![PyPI Total Downloads](https://img.shields.io/badge/dynamic/json?style=for-the-badge&label=total%20downloads&query=%24.total_downloads&url=https%3A%2F%2Fapi.pepy.tech%2Fapi%2Fprojects%2Fsvgtrace)](https://pepy.tech/project/svgtrace)
99
[![PyPI Version](https://img.shields.io/pypi/v/svgtrace.svg?style=for-the-badge)](https://pypi.org/project/svgtrace)
1010

11-
<!-- omit in TOC -->
11+
<!-- omit in toc -->
1212
# SvgTrace
1313

1414
<img src="readme-assets/icons/name.png" alt="Project Icon" width="750">
1515

16-
Leverage pyppeteer and the imagetrace.js library to trace a bitmap to SVG in
16+
Leverage playwright and the imagetrace.js library to trace a bitmap to SVG in
1717
python
1818

1919
- [Example](#example)
@@ -33,6 +33,8 @@ python
3333
- [How to run](#how-to-run)
3434
- [Windows](#windows)
3535
- [Linux/ MacOS](#linux-macos)
36+
- [Building](#building)
37+
- [Testing](#testing)
3638
- [Download Project](#download-project)
3739
- [Clone](#clone)
3840
- [Using The Command Line](#using-the-command-line)
@@ -106,8 +108,8 @@ Head to https://pypi.org/project/svgtrace/ for more info
106108

107109
### Built for
108110

109-
This program has been written for Python versions 3.7 - 3.10 and has been tested with both 3.7 and
110-
3.10
111+
This program has been written for Python versions 3.8 - 3.11 and has been tested with both 3.8 and
112+
3.11
111113

112114
## Install Python on Windows
113115

@@ -167,6 +169,36 @@ version.
167169
- File
168170
`python3.x [file]` or `./[file]`
169171

172+
## Building
173+
174+
This project uses https://github.com/FHPythonUtils/FHMake to automate most of the building. This
175+
command generates the documentation, updates the requirements.txt and builds the library artefacts
176+
177+
Note the functionality provided by fhmake can be approximated by the following
178+
179+
```sh
180+
handsdown --cleanup -o documentation/reference
181+
poetry export -f requirements.txt --output requirements.txt
182+
poetry export -f requirements.txt --with dev --output requirements_optional.txt
183+
poetry build
184+
```
185+
186+
`fhmake audit` can be run to perform additional checks
187+
188+
## Testing
189+
190+
For testing with the version of python used by poetry use
191+
192+
```sh
193+
poetry run pytest
194+
```
195+
196+
Alternatively use `tox` to run tests over python 3.8 - 3.11
197+
198+
```sh
199+
tox
200+
```
201+
170202
## Download Project
171203

172204
### Clone

documentation/reference/MODULES.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

documentation/reference/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# Svgtrace Modules
1+
# Svgtrace Index
22

3-
> Auto-generated documentation modules index.
3+
> Auto-generated documentation index.
44
5-
Full list of [Svgtrace](README.md#svgtrace-index) project modules.
5+
A full list of `Svgtrace` project modules.
66

7-
- [Svgtrace Index](README.md#svgtrace-index)
87
- [Svgtrace](svgtrace/index.md#svgtrace)
Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,18 @@
11
# Svgtrace
22

3-
> Auto-generated documentation for [svgtrace](../../../svgtrace/__init__.py) module.
4-
5-
Author FredHappyface 2020.
6-
7-
- [Svgtrace](../README.md#svgtrace-index) / [Modules](../MODULES.md#svgtrace-modules) / Svgtrace
8-
- [doTrace](#dotrace)
9-
- [trace](#trace)
10-
11-
Uses pyppeteer to leverage a headless version of Chromium
12-
Requires imagetracer.html and imagetracer.js along with the modules below
13-
14-
## doTrace
3+
[Svgtrace Index](../README.md#svgtrace-index) /
4+
Svgtrace
155

16-
[[find in source code]](../../../svgtrace/__init__.py#L16)
17-
18-
```python
19-
async def doTrace(filename: str, mode: str = 'default'):
20-
```
6+
> Auto-generated documentation for [svgtrace](../../../svgtrace/__init__.py) module.
217
22-
Call web code.
8+
- [Svgtrace](#svgtrace)
9+
- [trace](#trace)
2310

2411
## trace
2512

26-
[[find in source code]](../../../svgtrace/__init__.py#L40)
13+
[Show source in __init__.py:20](../../../svgtrace/__init__.py#L20)
2714

28-
```python
29-
def trace(
30-
filename: str,
31-
blackAndWhite: bool = False,
32-
mode: str = 'default',
33-
) -> str:
34-
```
35-
36-
Do a trace of an image on the filesystem using the pyppeteer library.
15+
Do a trace of an image on the filesystem using the playwright library.
3716

3817
#### Arguments
3918

@@ -46,3 +25,12 @@ for more information. Defaults to "default".
4625
#### Returns
4726

4827
- `str` - SVG string
28+
29+
#### Signature
30+
31+
```python
32+
def trace(filename: str, blackAndWhite: bool = False, mode: str = "default") -> str:
33+
...
34+
```
35+
36+

pyproject.toml

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[tool.poetry]
22
name = "svgtrace"
3-
version = "2022"
3+
version = "2023"
44
license = "mit"
5-
description = "Leverage pyppeteer and the imagetrace.js library to trace a bitmap to svg in python"
5+
description = "Leverage playwright and the imagetrace.js library to trace a bitmap to svg in python"
66
authors = ["FredHappyface"]
77
classifiers = [
88
"Development Status :: 5 - Production/Stable",
@@ -21,16 +21,33 @@ documentation = "https://github.com/FHPythonUtils/SvgTrace/blob/master/README.md
2121
readme = "README.md"
2222

2323
[tool.poetry.dependencies]
24-
python = "^3.7"
25-
pyppeteer = "<2,>=1.0.2"
24+
python = "^3.8"
25+
playwright = "<2,>=1.29.1"
26+
install-playwright = "<2,>=0.0.0"
2627

27-
[tool.poetry.dev-dependencies]
28+
[tool.poetry.group.dev.dependencies]
29+
nocairosvg = "^2023"
2830
pytest = "^7.1.1"
2931
pylint = "^2.13.5"
3032
handsdown = "^1.1.0"
3133
coverage = "^6.3.2"
3234
imgcompare = "^2.0.1"
33-
nocairosvg = "^2022.0.1"
35+
36+
[tool.black]
37+
line-length = 100
38+
target-version = ["py38"]
39+
40+
[tool.isort]
41+
profile = "black"
42+
indent = "Tab"
43+
44+
[tool.pydocstyle]
45+
convention = "google"
46+
ignore = "D205,D415"
47+
48+
[build-system]
49+
requires = ["poetry-core"]
50+
build-backend = "poetry.core.masonry.api"
3451

3552
[tool.pylint.basic]
3653
argument-naming-style = "camelCase"
@@ -43,28 +60,25 @@ variable-naming-style = "camelCase"
4360
indent-string = "\t"
4461

4562
[tool.pylint.master]
46-
ignore-patterns = "test_.*?py"
63+
ignore-paths = ["tests"]
4764

4865
[tool.pylint.messages_control]
4966
enable = ["F", "E", "W", "R", "C"]
50-
disable = [
51-
"pointless-string-statement",
52-
"superfluous-parens",
53-
"bad-continuation"
54-
]
55-
56-
[tool.black]
57-
line-length = 100
58-
target-version = ["py37"]
59-
60-
[tool.isort]
61-
profile = "black"
62-
indent = "Tab"
67+
disable = ["pointless-string-statement", "superfluous-parens"]
6368

64-
[tool.pydocstyle]
65-
convention = "google"
66-
ignore = "D205,D415"
69+
[tool.tox]
70+
legacy_tox_ini = """
71+
[tox]
72+
env_list =
73+
py311
74+
py310
75+
py39
76+
py38
6777
68-
[build-system]
69-
requires = ["poetry-core"]
70-
build-backend = "poetry.core.masonry.api"
78+
[testenv]
79+
deps =
80+
nocairosvg
81+
pytest
82+
imgcompare
83+
commands = pytest tests
84+
"""

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
pyppeteer<2,>=1.0.2
1+
install-playwright<2,>=0.0.0
2+
playwright<2,>=1.29.1

svgtrace/__init__.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,24 @@
11
"""Author FredHappyface 2020.
22
3-
Uses pyppeteer to leverage a headless version of Chromium
3+
Uses playwright to leverage a headless version of Chromium
44
Requires imagetracer.html and imagetracer.js along with the modules below
55
"""
66
from __future__ import annotations
77

88
import asyncio
99
from pathlib import Path
1010

11-
from pyppeteer import launch
11+
from install_playwright import install
12+
from playwright.sync_api import sync_playwright
13+
1214

1315
THISDIR = str(Path(__file__).resolve().parent)
1416

1517

16-
async def doTrace(filename: str, mode: str = "default"):
17-
"""Call web code."""
18-
browser = await launch(
19-
options={
20-
"args": [
21-
"--no-sandbox",
22-
"--headless",
23-
"--disable-web-security",
24-
"--allow-file-access-from-files",
25-
]
26-
}
27-
)
28-
page = await browser.newPage()
29-
await page.goto(f"file:///{THISDIR}/imagetracer.html")
30-
await page.evaluate(
31-
f"ImageTracer.imageToSVG('file:///{filename}',function(svgstr){{ ImageTracer.appendSVGString( svgstr, 'svg-container' ); }},'{mode}');"
32-
)
33-
element = await page.querySelector("div")
34-
svg = await page.evaluate("(element) => element.innerHTML", element)
35-
36-
await browser.close()
37-
return svg
3818

3919

4020
def trace(filename: str, blackAndWhite: bool = False, mode: str = "default") -> str:
41-
"""Do a trace of an image on the filesystem using the pyppeteer library.
21+
"""Do a trace of an image on the filesystem using the playwright library.
4222
4323
Args:
4424
filename (str): The location of the file on the filesystem, use an
@@ -52,12 +32,22 @@ def trace(filename: str, blackAndWhite: bool = False, mode: str = "default") ->
5232
"""
5333
if mode.find("black") >= 0 or blackAndWhite:
5434
mode = "posterized1"
55-
try:
56-
return asyncio.get_event_loop().run_until_complete(
57-
doTrace(filename.replace("\\", "/"), mode)
35+
36+
filename=filename.replace('\\', '/')
37+
38+
with sync_playwright() as p:
39+
install(p.chromium)
40+
browser = p.chromium.launch(
41+
args=["--no-sandbox", "--disable-web-security", "--allow-file-access-from-files"]
5842
)
59-
except ConnectionResetError:
60-
print("WARNING: ConnectionResetError - probably just a hiccup retrying")
61-
return asyncio.get_event_loop().run_until_complete(
62-
doTrace(filename.replace("\\", "/"), mode)
43+
44+
page = browser.new_page()
45+
page.goto(f"file:///{THISDIR}/imagetracer.html")
46+
page.evaluate(
47+
f"ImageTracer.imageToSVG('file:///{filename}',function(svgstr){{ ImageTracer.appendSVGString( svgstr, 'svg-container' ); }},'{mode}');"
6348
)
49+
element = page.query_selector("div")
50+
svg = page.evaluate("(element) => element.innerHTML", element)
51+
52+
browser.close()
53+
return svg

tests/data/logo-actual-bw.png

-511 Bytes
Loading

0 commit comments

Comments
 (0)