Skip to content

Commit d9bbf42

Browse files
authored
Merge pull request #2 from NF-coder/dev
Move settings from python-file to yaml-config
2 parents b1c812c + fbafe61 commit d9bbf42

File tree

9 files changed

+148
-53
lines changed

9 files changed

+148
-53
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Jinja2==3.1.6
2-
requests==2.32.4
2+
requests==2.32.4
3+
PyYAML==6.0.3

settings.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
general:
2+
top_k: 4
3+
plane:
4+
height: 140
5+
width: 250
6+
coloring:
7+
type: "oklch"
8+
chroma: 0.099
9+
lightness: 0.636
10+
excluded_languages:
11+
- Jupyter Notebook
12+
13+
legend:
14+
margin_x: 140
15+
margin_y: 30
16+
space_between_captions: 22
17+
font_color: "#c1c1c1"
18+
19+
diagram:
20+
outer_radius: 55
21+
thickness: 12
22+
margin_x: 20
23+
margin_y: 15

src/main.py

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,72 @@
33
from renderer.renderer import RenderBuilder
44
from oklchUtils.OKLCHUtils import OKLCHUtils
55
from calc import Calc
6-
6+
from settings import Settings
77
from fetcher.fetcher import FetchLangStats
88

99
# Fetch info
1010
USERNAME = os.environ['USERNAME']
1111
TOKEN = os.environ['GITHUB_TOKEN']
12-
13-
# Plane settings
14-
HEIGHT: int = 140
15-
WIDTH: int = 250
16-
17-
# Captions settings
18-
LEGEND_MARGIN_X: int = 140
19-
LEGEND_MARGIN_Y: int = 30
20-
SPACE_BETWEEN_CAPTIONS: int = 22
21-
FONT_COLOR: str = "#c1c1c1"
22-
23-
# Color settings
24-
OKLCH_CHROMA: float = 0.099
25-
OKLCH_LIGHTNESS: float = 0.636
26-
OTHER_COLOR_CHROMA: float = 0.000
27-
28-
# Round sizes settings
29-
OUTER_RADIUS: int = 55
30-
THICKNESS: int = 12
31-
32-
# Margins settings
33-
MARGIN_X: int = 20
34-
MARGIN_Y: int = 15
35-
36-
# Arrays settings
37-
TOP_K: int = 4
38-
39-
# Output filepath
4012
OUTPUT_FILE: str = "./out.svg"
13+
SETTINGS_FILE: str = "./settings.yaml"
14+
15+
SETTINGS = Settings.from_yaml(SETTINGS_FILE)
4116

42-
def get_langs_data(token: str):
17+
def get_langs_data(
18+
token: str,
19+
username: str,
20+
exclude_langs: list[str] = SETTINGS.GENERAL_SETTINGS.EXCLUDED_LANGUAGES
21+
) -> dict[str, float]:
4322
info = {}
44-
4523
total_size = 0
4624

47-
for elem in FetchLangStats(token).fetch_user(USERNAME):
25+
for elem in FetchLangStats(token).fetch_user(username):
26+
if elem.name in exclude_langs: continue
27+
4828
total_size += elem.size
4929
if elem.name not in info: info[elem.name] = elem.size
5030
else: info[elem.name] += elem.size
5131

52-
return info.keys(), map(lambda x: x/total_size, info.values())
32+
return {k: info[k]/total_size for k in info}
5333

5434
def truncate(langs_arr: list[tuple[float, str]], k: int):
5535
if len(langs_arr) <= k: return langs_arr
5636
return langs_arr[:k-1] + [(sum(map(lambda x: x[0], langs_arr[k-1:])), "Other")]
5737

5838
def main():
59-
NAMES_ARR, PERCENT_ARR = get_langs_data(TOKEN)
39+
languages_stats = get_langs_data(TOKEN, USERNAME)
6040

61-
sorted_percents = sorted([(percent, name) for percent,name in zip(PERCENT_ARR, NAMES_ARR)], key=lambda x: x[0], reverse=True)
41+
sorted_percents = sorted(
42+
[(percent, name) for percent,name in zip(languages_stats.values(), languages_stats.keys())],
43+
key=lambda x: x[0],
44+
reverse=True
45+
)
6246

63-
sorted_percents = truncate(sorted_percents, TOP_K)
47+
sorted_percents = truncate(sorted_percents, SETTINGS.GENERAL_SETTINGS.TOP_K)
6448

6549
_ = Calc(
66-
outer_radius=OUTER_RADIUS,
67-
thickness=THICKNESS,
50+
outer_radius=SETTINGS.DIAGRAM_SETTINGS.OUTER_RADIUS,
51+
thickness=SETTINGS.DIAGRAM_SETTINGS.THICKNESS,
6852
percent_array=[elem[0] for elem in sorted_percents],
6953
sections_colors_array=OKLCHUtils.create_colors_array(
7054
length=len(sorted_percents),
71-
chroma=OKLCH_CHROMA,
72-
lightness=OKLCH_LIGHTNESS
55+
chroma=SETTINGS.GENERAL_SETTINGS.COLORING.CHROMA,
56+
lightness=SETTINGS.GENERAL_SETTINGS.COLORING.LIGHTNESS
7357
),
7458
renderer=RenderBuilder(
75-
height=HEIGHT,
76-
width=WIDTH,
77-
outer_radius=OUTER_RADIUS,
78-
thickness=THICKNESS,
79-
margin_x=MARGIN_X,
80-
margin_y=MARGIN_Y,
81-
legend_margin_x=LEGEND_MARGIN_X,
82-
legend_margin_y=LEGEND_MARGIN_Y,
83-
space_between_captions=SPACE_BETWEEN_CAPTIONS,
84-
font_color=FONT_COLOR
59+
height=SETTINGS.GENERAL_SETTINGS.PLANE.HEIGHT,
60+
width=SETTINGS.GENERAL_SETTINGS.PLANE.WIDTH,
61+
outer_radius=SETTINGS.DIAGRAM_SETTINGS.OUTER_RADIUS,
62+
thickness=SETTINGS.DIAGRAM_SETTINGS.THICKNESS,
63+
margin_x=SETTINGS.DIAGRAM_SETTINGS.MARGIN_X,
64+
margin_y=SETTINGS.DIAGRAM_SETTINGS.MARGIN_Y,
65+
legend_margin_x=SETTINGS.LEGEND_SETTINGS.MARGIN_X,
66+
legend_margin_y=SETTINGS.LEGEND_SETTINGS.MARGIN_Y,
67+
space_between_captions=SETTINGS.LEGEND_SETTINGS.SPACE_BETWEEN_CAPTIONS,
68+
font_color=SETTINGS.LEGEND_SETTINGS.FONT_COLOR
8569
),
86-
margin_x=MARGIN_X,
87-
margin_y=MARGIN_Y,
70+
margin_x=SETTINGS.DIAGRAM_SETTINGS.MARGIN_X,
71+
margin_y=SETTINGS.DIAGRAM_SETTINGS.MARGIN_Y,
8872
names_array=[elem[1] for elem in sorted_percents]
8973
)
9074

src/settings/DiagramSettings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from dataclasses import dataclass
2+
3+
@dataclass
4+
class DiagramSettings():
5+
OUTER_RADIUS: int
6+
THICKNESS: int
7+
MARGIN_X: int
8+
MARGIN_Y: int

src/settings/GeneralSettings.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from dataclasses import dataclass
2+
from .coloring.OKLCHColoring import OKLCHColoring
3+
4+
@dataclass
5+
class GeneralSettings:
6+
TOP_K: int
7+
PLANE: "PlaneSubsettings"
8+
COLORING: OKLCHColoring
9+
EXCLUDED_LANGUAGES: list[str]
10+
11+
@dataclass
12+
class PlaneSubsettings:
13+
HEIGHT: int
14+
WIDTH: int

src/settings/LegendSettings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from dataclasses import dataclass
2+
3+
@dataclass
4+
class LegendSettings:
5+
MARGIN_X: int
6+
MARGIN_Y: int
7+
SPACE_BETWEEN_CAPTIONS: int
8+
FONT_COLOR: str
9+

src/settings/Settings.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import yaml
2+
from typing import Self
3+
4+
from .GeneralSettings import GeneralSettings, PlaneSubsettings
5+
from .LegendSettings import LegendSettings
6+
from .DiagramSettings import DiagramSettings
7+
from .coloring.OKLCHColoring import OKLCHColoring
8+
9+
class Settings:
10+
GENERAL_SETTINGS: GeneralSettings
11+
LEGEND_SETTINGS: LegendSettings
12+
DIAGRAM_SETTINGS: DiagramSettings
13+
14+
@classmethod
15+
def from_yaml(cls, path: str) -> Self:
16+
with open(path, 'r') as stream:
17+
data = yaml.safe_load(stream)
18+
19+
inst = cls()
20+
21+
inst.GENERAL_SETTINGS = GeneralSettings(
22+
data["general"]["top_k"],
23+
PlaneSubsettings(
24+
data["general"]["plane"]["height"],
25+
data["general"]["plane"]["width"]
26+
),
27+
OKLCHColoring(
28+
data["general"]["coloring"]["chroma"],
29+
data["general"]["coloring"]["lightness"]
30+
),
31+
data["general"]["excluded_languages"]
32+
)
33+
34+
inst.LEGEND_SETTINGS = LegendSettings(
35+
data["legend"]["margin_x"],
36+
data["legend"]["margin_y"],
37+
data["legend"]["space_between_captions"],
38+
data["legend"]["font_color"]
39+
)
40+
41+
inst.DIAGRAM_SETTINGS = DiagramSettings(
42+
data["diagram"]["outer_radius"],
43+
data["diagram"]["thickness"],
44+
data["diagram"]["margin_x"],
45+
data["diagram"]["margin_y"]
46+
)
47+
48+
return inst

src/settings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .Settings import Settings
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from dataclasses import dataclass
2+
3+
@dataclass
4+
class OKLCHColoring():
5+
CHROMA: float
6+
LIGHTNESS: float
7+
TYPE = "oklvh"

0 commit comments

Comments
 (0)