Skip to content

Commit

Permalink
Merge pull request #18 from evtn/dev
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
evtn authored May 4, 2024
2 parents f75b813 + e5aea6f commit 77e6cbb
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 98 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ jobs:
with:
python-version: 3.9

- name: Install Poetry
uses: abatilo/actions-poetry@v2

- name: Build and Publish
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: |
python -m pip install poetry
poetry install
poetry build
poetry config pypi-token.pypi $PYPI_TOKEN
poetry publish
- run: echo "VERSION=$(poetry version -s)" >> $GITHUB_ENV
id: version-check

- name: Release on GitHub
uses: softprops/action-gh-release@v1
Expand Down
51 changes: 23 additions & 28 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test that everything works
name: Test

on:
workflow_dispatch:
Expand All @@ -16,27 +16,32 @@ jobs:
check_types:
runs-on: ubuntu-latest
name: Check Types
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- name: git-checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
python-version: ${{ matrix.python-version }}

- run: python -m pip install mypy
- name: Install Poetry
uses: abatilo/actions-poetry@v2

- run: python -m mypy soda/*.py --disallow-any-expr --pretty --show-error-codes
- run: poetry install --with dev
- run: poetry run pyright soda/*.py

run-tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

name: Run Tests

steps:
- name: git-checkout
uses: actions/checkout@v3
Expand All @@ -46,30 +51,20 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

# I couldn't get poetry install to work on CI ¯\_(ツ)_/¯
- run: python -m pip install pytest coverage coveralls typing-extensions
- name: Install Poetry
uses: abatilo/actions-poetry@v2

- name: Test
run: coverage run -m pytest test/

update-coverage:
runs-on: ubuntu-latest
name: Update Coverage

steps:
- name: git-checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4

- run: python -m pip install pytest coverage coveralls typing-extensions
- run: poetry install --with dev

- name: Test
run: coverage run --include "soda/*" -m pytest test/
run: poetry run coverage run --include "soda/*" -m pytest test/

- name: Coveralls update
- name: Convert coverage data to XML
run: poetry run coverage xml

run: coveralls --service=github
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Coveralls Update
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: ${{ matrix.python-version }}
coverage_format: cobertura
57 changes: 26 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/soda-svg)
[![Coveralls](https://img.shields.io/coverallsCoverage/github/evtn/soda?label=test%20coverage)](https://coveralls.io/github/evtn/soda?branch=lord)
![License](https://img.shields.io/github/license/evtn/soda)
![Badge Count](https://img.shields.io/badge/badges-8-important)

[![wordstreamer badge](https://img.shields.io/badge/renderable-what?label=wordstreamer&color=%2333bb33)](https://github.com/evtn/wordstreamer)
![Badge Count](https://img.shields.io/badge/badges-9-important)

Here's some basic usage:

> [!NOTE]
> Since soda 2.0.0, a Tag instance is also a [wordstreamer.Renderable](https://github.com/evtn/wordstreamer), which makes it possible to use things like:
>
> ```python
> from wordstreamer import Renderer
> from soda import Tag
>
> byte_stream = Renderer().byte_stream(Tag.rect()) # Iterable[bytes]
> ```
```python
from soda import Tag, Root
Expand All @@ -26,7 +36,7 @@ print(root.render(pretty=True))
## Installation

Install `soda-svg` from PyPI, like `python -m pip install soda-svg`.
Install `soda-svg` from PyPI, like `python -m pip install soda-svg`.

Note that `soda` on PyPI is a different package.

Expand Down Expand Up @@ -97,7 +107,7 @@ For convenience, leading and trailing underscores are removed by default, and un
```python
from soda import Tag

g = Tag.g(cla_ss_="test")
g = Tag.g(cla_ss_="test")

print(g.render()) # <g cla-ss="test"/>

Expand All @@ -110,7 +120,7 @@ from soda import Tag, config as soda_config

soda_config.replace_underscores = False

g = Tag.g(cla_ss_="test")
g = Tag.g(cla_ss_="test")

print(g.render()) # <g cla_ss="test"/>

Expand All @@ -123,7 +133,7 @@ from soda import Tag, config as soda_config

soda_config.strip_underscores = False

g = Tag.g(cla_ss_="test")
g = Tag.g(cla_ss_="test")

print(g.render()) # <g cla-ss_="test"/>
```
Expand All @@ -147,9 +157,9 @@ print(g2.render()) # <g cla_ss_="test"/>

## Creating a Tag from XML string

*new in 1.1.0*
_new in 1.1.0_

You can use `Tag.from_str(xml_string)` to parse an XML document in that string.
You can use `Tag.from_str(xml_string)` to parse an XML document in that string.
Note that currently this doesn't preserve any comments or declarations of original document.

```python
Expand Down Expand Up @@ -201,7 +211,6 @@ print(XMLDeclaration(version="2.0", encoding="UTF-8").render()) # '<?xml version

Default values for version and encoding are "1.0" and "UTF-8" respectively


XML comments are used similarly:

```python
Expand All @@ -211,7 +220,6 @@ from soda import XMLComment
print(XMLComment("comment text!!").render()) # '<!-- comment text!! -->'
```


```python
from soda import Tag, Literal

Expand Down Expand Up @@ -247,13 +255,12 @@ print(tag[0]) # prints <a href="https://github.com/evtn/soda" />

This is not necessary for most tasks as of 1.1.6 with new methods and iterator protocol:

- `Tag.insert(int, Node)` inserts a node on an index. Be aware that `tag.children` is not flattened: `Tag.g("test", ["test1", "test2"]).insert(2, elem)` will insert `elem` *after* the array.
- `Tag.append(Node)` appends one node to the tag.
- `Tag.extend(*Node)` appends several nodes to the tag. *This is not the same as `.append([*Node])`*
- `Tag.pop(int?)` pops one node from specified index. If index is not provided, pops the last one.
- `Tag.iter_raw()` returns an iterable to get every Node of the tag. This doesn't dive into nested arrays, for that behaviour iterate over `Tag`
- You can also iterate over the `Tag` itself to get every flat node of it (no arrays)

- `Tag.insert(int, Node)` inserts a node on an index. Be aware that `tag.children` is not flattened: `Tag.g("test", ["test1", "test2"]).insert(2, elem)` will insert `elem` _after_ the array.
- `Tag.append(Node)` appends one node to the tag.
- `Tag.extend(*Node)` appends several nodes to the tag. *This is not the same as `.append([*Node])`\*
- `Tag.pop(int?)` pops one node from specified index. If index is not provided, pops the last one.
- `Tag.iter_raw()` returns an iterable to get every Node of the tag. This doesn't dive into nested arrays, for that behaviour iterate over `Tag`
- You can also iterate over the `Tag` itself to get every flat node of it (no arrays)

## Fragments

Expand All @@ -271,7 +278,7 @@ print(tag) # <g><a/><a/></g>

## Paths

*new in 0.1.7*
_new in 0.1.7_

There is a builder for SVG path commands in soda:

Expand Down Expand Up @@ -389,7 +396,7 @@ print(Path.build(*commands, compact=True))

## Points

*new in 1.1.0*
_new in 1.1.0_

To work with coordinates, you can use `Point` class:

Expand Down Expand Up @@ -464,18 +471,6 @@ print(
) # 2
```

### Converting angles

To convert between radians and degrees, use `radians_to_degrees` and `degrees_to_radians`:

```python
from soda.point import radians_to_degrees, degrees_to_radians
from math import pi

print(degrees_to_radians(90) / pi) # 0.5
print(radians_to_degrees(degrees_to_radians(90))) # 90
```

### Using as attributes

`Point.as_` provides a convenient way of using points as tag attributes:
Expand Down
13 changes: 9 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "soda-svg"
packages = [{include = "soda"}]
version = "1.1.9"
version = "2.0.0"
description = "Fast SVG generation tool"
authors = ["Dmitry Gritsenko <[email protected]>"]
license = "MIT"
Expand All @@ -11,10 +11,15 @@ homepage = "https://github.com/evtn/soda"
keywords = ["soda", "svg", "xml"]

[tool.poetry.dependencies]
python = ">=3.7.0"
python = ">=3.8,<3.13"
wordstreamer = "^0.1.3"
lxml = "^5.2.1"
types-lxml = "^2024.4.14"

[tool.poetry.dev-dependencies]
pytest = "^7.3.1"
[tool.poetry.group.dev.dependencies]
pytest = "^8.0.0"
pyright = "^1.1.350"
coveralls = "^4.0.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
9 changes: 5 additions & 4 deletions soda/custom_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from base64 import b64encode
from os import PathLike
from typing import BinaryIO, Iterable
from typing import BinaryIO

from wordstreamer import Context, TokenStream
from .tags import Literal, Node, Tag
from pathlib import Path

Expand Down Expand Up @@ -33,11 +35,10 @@ class XMLComment(Tag):
def __init__(self, text: str):
self.text = text

def build(self, tab_size: int = 0, tab_level: int = 0) -> Iterable[str]:
yield " " * (tab_size * tab_level)
def stream(self, context: Context) -> TokenStream:
yield "<!--"
yield " "
yield from Literal(self.text).build(0, 0)
yield from Literal(self.text).stream(context)
yield " "
yield "-->"

Expand Down
Loading

0 comments on commit 77e6cbb

Please sign in to comment.