Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,36 @@ Then you can use it with
```html
<img src="https://your-gh-pagepurl/out.svg">
```

## Configuring
Example config u can see in `setting.yaml`
```yaml
general:
top_k: 4 # how many sections will be on donut-chart (including "Other" section)
plane:
height: 140 # height of svg
width: 250 # width of svg
coloring:
type: "github" # type of coloring (github or oklch available)
other_color: "#666666" # color of "Other" section
# coloring:
# type: "oklch" # type of coloring (github or oklch available)
# chroma: 0.099 # coloring oklch chroma
# lightness: 0.636 # coloring oklch lightness
# other_color: "#666666" # color of "Other" section
excluded_languages: # list of languages that should be excluded
- Jupyter Notebook # removed because jupyter files are too large

legend:
margin_x: 140 # margin of legend (x-axis)
margin_y: 30 # margin of legend (y-axis)
space_between_captions: 22 # space between legend options
font_color: "#c1c1c1" # font color

diagram:
outer_radius: 55 # outer circle radius
thickness: 12 # size of donut-chart
margin_x: 20 # margin of diagram (x-axis)
margin_y: 15 # margin of diagram (y-axis)
```
About **oklch** u can read [here](https://oklch.com/)
9 changes: 6 additions & 3 deletions settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ general:
height: 140
width: 250
coloring:
type: "oklch"
chroma: 0.099
lightness: 0.636
type: "github"
other_color: "#666666"
# coloring:
# type: "oklch"
# chroma: 0.099
# lightness: 0.636
excluded_languages:
- Jupyter Notebook

Expand Down
66 changes: 44 additions & 22 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,82 @@
import os
from dataclasses import dataclass

from renderer.renderer import RenderBuilder
from oklchUtils.OKLCHUtils import OKLCHUtils
from calc import Calc
from settings import Settings
from fetcher.fetcher import FetchLangStats

# Fetch info
# Some settinga
USERNAME = os.environ['USERNAME']
TOKEN = os.environ['GITHUB_TOKEN']
OUTPUT_FILE: str = "./out.svg"
SETTINGS_FILE: str = "./settings.yaml"

SETTINGS = Settings.from_yaml(SETTINGS_FILE)


@dataclass
class LangData:
name: str
size: float
github_color: str

def get_langs_data(
token: str,
username: str,
exclude_langs: list[str] = SETTINGS.GENERAL_SETTINGS.EXCLUDED_LANGUAGES
) -> dict[str, float]:
info = {}
total_size = 0
) -> list[LangData]:
info: dict[str, LangData] = {}
total_size: float = 0

for elem in FetchLangStats(token).fetch_user(username):
if elem.name in exclude_langs: continue

total_size += elem.size
if elem.name not in info: info[elem.name] = elem.size
else: info[elem.name] += elem.size
if elem.name not in info: info[elem.name] = LangData(elem.name, elem.size, elem.github_color)
else: info[elem.name].size += elem.size

return {k: info[k]/total_size for k in info}
return [LangData(
info[k].name,
info[k].size/total_size,
info[k].github_color
) for k in info]

def truncate(langs_arr: list[tuple[float, str]], k: int):
def truncate(langs_arr: list[LangData], k: int):
if len(langs_arr) <= k: return langs_arr
return langs_arr[:k-1] + [(sum(map(lambda x: x[0], langs_arr[k-1:])), "Other")]
return langs_arr[:k-1] + [LangData("Other", sum(map(lambda x: x.size, langs_arr[k-1:])), SETTINGS.GENERAL_SETTINGS.COLORING.OTHER_COLOR)]

def main():
languages_stats = get_langs_data(TOKEN, USERNAME)
def coloring(sorted_percents: list[LangData]) -> list[str]:
coloring_cfg = SETTINGS.GENERAL_SETTINGS.COLORING

if coloring_cfg.TYPE == "oklch":
if hasattr(coloring_cfg, "CHROMA") and hasattr(coloring_cfg, "LIGHTNESS"):
return OKLCHUtils.create_colors_array(
length=len(sorted_percents),
chroma=getattr(coloring_cfg, "CHROMA"),
lightness=getattr(coloring_cfg, "LIGHTNESS")
)
raise ValueError("Invalid oklch coloring config")
elif coloring_cfg.TYPE == "github":
return [elem.github_color for elem in sorted_percents]

raise ValueError("No such coloring config")

sorted_percents = sorted(
[(percent, name) for percent,name in zip(languages_stats.values(), languages_stats.keys())],
key=lambda x: x[0],
def main():
sorted_langs = sorted(
get_langs_data(TOKEN, USERNAME),
key=lambda x: x.size,
reverse=True
)

sorted_percents = truncate(sorted_percents, SETTINGS.GENERAL_SETTINGS.TOP_K)
sorted_percents = truncate(sorted_langs, SETTINGS.GENERAL_SETTINGS.TOP_K)

_ = Calc(
outer_radius=SETTINGS.DIAGRAM_SETTINGS.OUTER_RADIUS,
thickness=SETTINGS.DIAGRAM_SETTINGS.THICKNESS,
percent_array=[elem[0] for elem in sorted_percents],
sections_colors_array=OKLCHUtils.create_colors_array(
length=len(sorted_percents),
chroma=SETTINGS.GENERAL_SETTINGS.COLORING.CHROMA,
lightness=SETTINGS.GENERAL_SETTINGS.COLORING.LIGHTNESS
),
percent_array=[elem.size for elem in sorted_percents],
sections_colors_array=coloring(sorted_percents),
renderer=RenderBuilder(
height=SETTINGS.GENERAL_SETTINGS.PLANE.HEIGHT,
width=SETTINGS.GENERAL_SETTINGS.PLANE.WIDTH,
Expand All @@ -69,7 +91,7 @@ def main():
),
margin_x=SETTINGS.DIAGRAM_SETTINGS.MARGIN_X,
margin_y=SETTINGS.DIAGRAM_SETTINGS.MARGIN_Y,
names_array=[elem[1] for elem in sorted_percents]
names_array=[elem.name for elem in sorted_percents]
)

with open(OUTPUT_FILE, "w+", encoding="utf-8-sig") as f:
Expand Down
14 changes: 14 additions & 0 deletions src/settings/Coloring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from dataclasses import dataclass
from typing import Union

@dataclass
class OKLCHColoring():
TYPE = "oklch"
CHROMA: float
LIGHTNESS: float
OTHER_COLOR: str

@dataclass
class GithubColoring():
TYPE = "github"
OTHER_COLOR: str
4 changes: 2 additions & 2 deletions src/settings/GeneralSettings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from dataclasses import dataclass
from .coloring.OKLCHColoring import OKLCHColoring
from .Coloring import *

@dataclass
class GeneralSettings:
TOP_K: int
PLANE: "PlaneSubsettings"
COLORING: OKLCHColoring
COLORING: OKLCHColoring | GithubColoring
EXCLUDED_LANGUAGES: list[str]

@dataclass
Expand Down
20 changes: 14 additions & 6 deletions src/settings/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .GeneralSettings import GeneralSettings, PlaneSubsettings
from .LegendSettings import LegendSettings
from .DiagramSettings import DiagramSettings
from .coloring.OKLCHColoring import OKLCHColoring
from .Coloring import *

class Settings:
GENERAL_SETTINGS: GeneralSettings
Expand All @@ -18,16 +18,15 @@ def from_yaml(cls, path: str) -> Self:

inst = cls()

coloring = select_coloring(data["general"]["coloring"])

inst.GENERAL_SETTINGS = GeneralSettings(
data["general"]["top_k"],
PlaneSubsettings(
data["general"]["plane"]["height"],
data["general"]["plane"]["width"]
),
OKLCHColoring(
data["general"]["coloring"]["chroma"],
data["general"]["coloring"]["lightness"]
),
coloring,
data["general"]["excluded_languages"]
)

Expand All @@ -45,4 +44,13 @@ def from_yaml(cls, path: str) -> Self:
data["diagram"]["margin_y"]
)

return inst
return inst

def select_coloring(data: dict) -> OKLCHColoring | GithubColoring:
t = data.get("type")
if t == "oklch":
return OKLCHColoring(data["chroma"], data["lightness"], data["other_color"])
elif t == "github":
return GithubColoring(data["other_color"])

raise ValueError(f"Unknown coloring type: {t!r}")
7 changes: 0 additions & 7 deletions src/settings/coloring/OKLCHColoring.py

This file was deleted.