diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a27ac9d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +root = true + +[*] +charset=utf-8 +indent_style = tab +indent_size = 4 +insert_final_newline = true diff --git a/.github/.templateMarker b/.github/.templateMarker new file mode 100644 index 0000000..5e3a3e0 --- /dev/null +++ b/.github/.templateMarker @@ -0,0 +1 @@ +KOLANICH/python_project_boilerplate.py diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..eeae2b9 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,13 @@ +name: CI +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + build: + runs-on: ubuntu-22.04 + steps: + - name: typical python workflow + uses: KOLANICH-GHActions/typical-python-workflow@master diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f105b84 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +__pycache__ +*.pyc +*.pyo +/*.egg-info +*.srctrlbm +*.srctrldb +build +dist +.eggs +monkeytype.sqlite3 +/tests/testSavedDataRootDir +/.ipynb_checkpoints diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..d7cb4e1 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,47 @@ +#image: pypy:latest +image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest + +variables: + DOCKER_DRIVER: overlay2 + SAST_ANALYZER_IMAGE_TAG: latest + SAST_DISABLE_DIND: "true" + SAST_CONFIDENCE_LEVEL: 5 + CODECLIMATE_VERSION: latest + +include: + - template: SAST.gitlab-ci.yml + - template: Code-Quality.gitlab-ci.yml + - template: License-Management.gitlab-ci.yml + +build: + tags: + - shared + - linux + stage: build + variables: + GIT_DEPTH: "1" + PYTHONUSERBASE: "${CI_PROJECT_DIR}/python_user_packages" + + before_script: + - export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables` + - apt-get update + + script: + - python3 setup.py bdist_wheel + - pip3 install --upgrade -e ./dist/piechart-0.CI-py3-none-any.whl + - coverage run --source=piechart -m pytest --junitxml=./rspec.xml ./tests/test.py + - coverage report -m + - coveralls + - codecov + + coverage: "/^TOTAL(?:\\s+\\d+){4}\\s+(\\d+%).+/" + + cache: + paths: + - $PYTHONUSERBASE + + artifacts: + paths: + - dist + reports: + junit: ./rspec.xml diff --git a/Code_Of_Conduct.md b/Code_Of_Conduct.md new file mode 100644 index 0000000..bcaa2bf --- /dev/null +++ b/Code_Of_Conduct.md @@ -0,0 +1 @@ +No codes of conduct! \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..20f0fa8 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include UNLICENSE +include *.md +include tests +include .editorconfig diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..6312b05 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,10 @@ +piechart.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/) +=========== +~~[![GitHub Actions](https://github.com/KOLANICH-libs/piechart.py/workflows/CI/badge.svg)](https://github.com/KOLANICH-libs/piechart.py/actions/)~~ +[![Libraries.io Status](https://img.shields.io/librariesio/github/KOLANICH-libs/piechart.py.svg)](https://libraries.io/github/KOLANICH-libs/piechart.py) +[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://codeberg.org/KOLANICH-tools/antiflash.py) + +A damn damn damn small and simple SVG pie chart generator library without any dependencies, such as XML libs. Created for being used in CI pipelines for generating icons visualizing metrics like tests coverage. + + +![0.1](./tests/images/0.1.svg)![0.2](./tests/images/0.2.svg)![0.3](./tests/images/0.3.svg)![0.4](./tests/images/0.4.svg)![0.5](./tests/images/0.5.svg)![0.6](./tests/images/0.6.svg)![0.7](./tests/images/0.7.svg)![0.8](./tests/images/0.8.svg)![0.9](./tests/images/0.9.svg) diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..efb9808 --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/piechart.py b/piechart.py new file mode 100644 index 0000000..c6fb976 --- /dev/null +++ b/piechart.py @@ -0,0 +1,111 @@ +__all__ = ("piechart",) + +# pylint:disable=too-many-function-args + +import typing +import sys +import math +from codecs import encode + +fc = math.pi * 2 + +VecT = typing.Tuple[float, float] +ColorTupleT = typing.Tuple[int, int, int] +ColorT = typing.Union[ColorTupleT, str] + + +def computeCoords(center: VecT, phase: float, r: float) -> VecT: + return (center[0] + r * math.cos(phase), center[1] + r * math.sin(phase)) + + +def dumpCoords(coord: VecT) -> str: + return " ".join((num2str(coord[0]), num2str(coord[1]))) + + +def Move(coord: VecT) -> str: + return " ".join(("M", dumpCoords(coord))) + + +def Line(coord: VecT) -> str: + return " ".join(("L", dumpCoords(coord))) + + +def arc(r: float, offset: int, angle: float, end: VecT) -> str: + return " ".join(("A", dumpCoords((r, r)), num2str(offset), "0" if abs(angle) < 180 else "1", "1", dumpCoords(end))) + + +def makeSectorPath(center: VecT, r: float, percentage: float, offset: float = 0) -> str: + startPhase = offset * fc + endPhase = (offset + percentage) * fc + start = computeCoords(center, startPhase, r) + end = computeCoords(center, endPhase, r) + commands = [Move(center), Line(start), arc(r, offset * 360, percentage * 360, end), "Z"] + return "".join(commands) + + +def num2str(n: typing.Union[int, float]): + if isinstance(n, float) and n.is_integer(): + return str(int(n)) + return str(n) + + +def color2hex(args): + return "#" + str(encode(bytes(args), "hex"), encoding="ascii") + + +def rgb255(rgb10): + return tuple(int(round(el * 255)) for el in rgb10) + + +def createColors(count: int): + import colorsys # pylint:disable=import-outside-toplevel + + res = [None] * count + piece = 1 / count + for i in range(count): + res[i] = rgb255(colorsys.hsv_to_rgb(i * piece, 1, 1)) + + return res + + +def piechart(shares: typing.Tuple[float] = (0.35,), colors=None, podColor: typing.Optional[str] = "transparent", placeText: typing.Optional[typing.Union[str, bool]] = None, size: float = 30) -> str: + """Creates a pie chart. + `shares` is a tuple of shares. + `colors` is a tuple of colors. `None` means autogeneration. A color is either a tuple of RGB, or a string suitable for CSS. + `podColor` is the pie ***pod*** color. + `placeText` a text to place into the middle of the pie. By default percentage, if 1 share is displayed. Set to `False` to disable. + """ + + r = size / 2 + center = (r, r) + + if placeText is None: + placeText = len(shares) == 1 + + if placeText is True: + if len(shares) > 1: + raise ValueError("Text is only placed if only 1 value must be displayed") + + placeText = str(round(shares[0] * 100)) + "%" + + if colors is None: + colors = createColors(len(shares)) + + items = [] + + offset = 0 + for share, color in zip(shares, colors): + if isinstance(color, tuple): + color = color2hex(color) + + items.append('') + offset += share + + if placeText: + items += '' + placeText + "" + + return r'' + "".join(items) + "" + + +if __name__ == "__main__": + print(piechart(tuple((float(n) for n in sys.argv[1:]))), file=sys.stdout) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0f1a0ec --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,30 @@ +[build-system] +requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"] +build-backend = "setuptools.build_meta" + +[project] +name = "piechart" +authors = [{name = "KOLANICH"}] +description = "A damn damn damn small and simple pie chart library without any dependencies." +readme = "ReadMe.md" +keywords = ["piechart"] +license = {text = "Unlicense"} +classifiers = [ + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Development Status :: 4 - Beta", + "Environment :: Other Environment", + "Intended Audience :: Developers", + "License :: Public Domain", + "Operating System :: OS Independent", + "Topic :: Software Development :: Libraries :: Python Modules", +] +urls = {Homepage = "https://codeberg.org/KOLANICH-libs/piechart.py"} +requires-python = ">=3.4" +dynamic = ["version"] + +[tool.setuptools] +zip-safe = true +py-modules = ["piechart"] + +[tool.setuptools_scm] diff --git a/tests/generate_images.sh b/tests/generate_images.sh new file mode 100644 index 0000000..6de26b6 --- /dev/null +++ b/tests/generate_images.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env sh +for i in `seq 1 9`; do python3 ./piechart.py 0.$i 0.1 > ./tests/images/0.$i.svg;done diff --git a/tests/images/0.1.svg b/tests/images/0.1.svg new file mode 100644 index 0000000..d2d67ea --- /dev/null +++ b/tests/images/0.1.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.2.svg b/tests/images/0.2.svg new file mode 100644 index 0000000..32ada39 --- /dev/null +++ b/tests/images/0.2.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.3.svg b/tests/images/0.3.svg new file mode 100644 index 0000000..4bb83fc --- /dev/null +++ b/tests/images/0.3.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.4.svg b/tests/images/0.4.svg new file mode 100644 index 0000000..e3c68ef --- /dev/null +++ b/tests/images/0.4.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.5.svg b/tests/images/0.5.svg new file mode 100644 index 0000000..a3a6eba --- /dev/null +++ b/tests/images/0.5.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.6.svg b/tests/images/0.6.svg new file mode 100644 index 0000000..74394ab --- /dev/null +++ b/tests/images/0.6.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.7.svg b/tests/images/0.7.svg new file mode 100644 index 0000000..71b57c4 --- /dev/null +++ b/tests/images/0.7.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.8.svg b/tests/images/0.8.svg new file mode 100644 index 0000000..d59dca9 --- /dev/null +++ b/tests/images/0.8.svg @@ -0,0 +1 @@ + diff --git a/tests/images/0.9.svg b/tests/images/0.9.svg new file mode 100644 index 0000000..c2a6067 --- /dev/null +++ b/tests/images/0.9.svg @@ -0,0 +1 @@ + diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 0000000..f5dee63 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import typing +import sys +import unittest +from collections import OrderedDict +from pathlib import Path + +dict = OrderedDict + +thisDir = Path(__file__).parent +sys.path.insert(0, str(thisDir.parent)) +from piechart import piechart + +testImagesDir = thisDir / "images" + + +class Tests(unittest.TestCase): + maxDiff = None + + def test_simple(self) -> None: + count = 10 + for i in range(1, count): + share = i / count + with self.subTest(share=share): + self.assertEqual(piechart((share, 0.1)).strip(), (testImagesDir / ("0." + str(i) + ".svg")).read_text().strip()) + + +if __name__ == "__main__": + unittest.main()