Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Oct 24, 2023
0 parents commit 922d867
Show file tree
Hide file tree
Showing 22 changed files with 300 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
charset=utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
1 change: 1 addition & 0 deletions .github/.templateMarker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KOLANICH/python_project_boilerplate.py
13 changes: 13 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
__pycache__
*.pyc
*.pyo
/*.egg-info
*.srctrlbm
*.srctrldb
build
dist
.eggs
monkeytype.sqlite3
/tests/testSavedDataRootDir
/.ipynb_checkpoints
47 changes: 47 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No codes of conduct!
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include UNLICENSE
include *.md
include tests
include .editorconfig
10 changes: 10 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions UNLICENSE
Original file line number Diff line number Diff line change
@@ -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 <https://unlicense.org/>
111 changes: 111 additions & 0 deletions piechart.py
Original file line number Diff line number Diff line change
@@ -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('<path d="' + makeSectorPath(center, r, share, offset) + '" fill="' + color + '"/>')
offset += share

if placeText:
items += '<text x="' + num2str(center[0]) + '" y="' + num2str(center[0]) + '" text-anchor="middle" dominant-baseline="middle" font-size="' + num2str(size / 3) + 'px" font-family="monospace">' + placeText + "</text>"

return r'<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" width="' + num2str(size) + '" height="' + num2str(size) + '"><circle cx="' + num2str(r) + '" cy="' + num2str(r) + '" r="' + num2str(r) + '" fill="' + podColor + '"/>' + "".join(items) + "</svg>"


if __name__ == "__main__":
print(piechart(tuple((float(n) for n in sys.argv[1:]))), file=sys.stdout)
30 changes: 30 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 2 additions & 0 deletions tests/generate_images.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/images/0.1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.4.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.6.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/images/0.9.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 922d867

Please sign in to comment.