Add Builder for shields

This commit is contained in:
Lucàs
2024-01-04 15:43:18 +01:00
parent efbf7fb7cc
commit b2c6d4c070
13 changed files with 466 additions and 441 deletions
+14 -57
View File
@@ -1,80 +1,37 @@
import yaml
from src.model.skill_list import SkillList
import requests
from src.model.social_list import SocialList
def load_data(config_file_path: str) -> dict:
"""Load data from config file and return a dict"""
config_data_builder = ConfigDataBuilder(config_file_path)
return (config_data_builder
.load_config_file()
.load_user_info()
.load_skill_section()
.load_social_section()
.build())
class ConfigDataBuilder:
class Config:
config_file_path: str
config_data: dict[str, any] = None
def __init__(self, config_file_path: str):
"""
Initialize ConfigDataBuilder
:param config_file_path: Path to config file
"""
self.config_file_path = config_file_path
self.config_data = None
def load_config_file(self) -> 'ConfigDataBuilder':
"""
Load config file and return ConfigDataBuilder
:return: ConfigDataBuilder
"""
def load_config_file(self):
with open(self.config_file_path, 'r') as config_file:
self.config_data = yaml.safe_load(config_file)
return self
def load_user_info(self) -> 'ConfigDataBuilder':
"""
Load user info from GitHub API and return ConfigDataBuilder
:return: ConfigDataBuilder
"""
def handle_user_info(self):
user = self.config_data["user"]
response = requests.get(f"https://api.github.com/users/{user}")
if response.status_code != 200:
raise Exception("User not found")
self.config_data["user"] = response.json()
return self
def load_social_section(self) -> 'ConfigDataBuilder':
"""
Load social section and return ConfigDataBuilder
:return: ConfigDataBuilder
"""
self.config_data["socials"] = str(SocialList(self.config_data["socials"]))
return self
def load_skill_section(self) -> 'ConfigDataBuilder':
"""
Load skill section and return ConfigDataBuilder
:return: ConfigDataBuilder
"""
def handle_skill_section(self):
self.config_data["skills"] = str(SkillList(self.config_data["skills"]))
return self
def build(self) -> dict:
"""
Return config data
def handle_social_section(self):
self.config_data["socials"] = str(SocialList(self.config_data["socials"]))
def get_data(self):
self.load_config_file()
self.handle_user_info()
self.handle_skill_section()
self.handle_social_section()
:return: dict
"""
return self.config_data
+15 -7
View File
@@ -1,10 +1,18 @@
from src.shield.shield_builder import ShieldBuilder
class Skill:
def __init__(self, name: str, url: str):
self.name = name
self.url = url
alt: str
src: str
def __str__(self):
return f"![{self.name}]({self.url})"
def __init__(self, name: str):
self.alt = name
self.src = (
ShieldBuilder()
.set_message(name)
.set_logo(name)
.build()
)
def __repr__(self):
return f"![{self.name}]({self.url})"
def __str__(self) -> str:
return f"![{self.alt}]({self.src})"
+8 -6
View File
@@ -2,12 +2,14 @@ from src.model.skill import Skill
class SkillList:
def __init__(self, skills: list):
self.skills = [Skill(skill.get("name"), skill.get("url")) for skill in skills]
skills: list[Skill]
def __init__(self, skills: list[str]):
# Sort and remove duplicates
skills = list(set(skills))
skills.sort()
self.skills = [Skill(skill) for skill in skills]
def __str__(self) -> str:
return "\n".join([str(skill) for skill in self.skills])
def __repr__(self) -> str:
return "\n".join([repr(skill) for skill in self.skills])
+18 -9
View File
@@ -1,11 +1,20 @@
class Social:
def __init__(self, name: str, url: str, img: str):
self.name = name
self.url = url
self.img = img
from src.shield.shield_builder import ShieldBuilder
class Social:
name: str
img: str
def __init__(self, name: str, url: str):
self.name = name
self.img = (
ShieldBuilder()
.set_message(name)
.set_logo(name)
.set_link(url)
.set_style("for-the-badge")
.build()
)
def __repr__(self) -> str:
return f"[![{self.name}]({self.img})]({self.url})"
def __str__(self) -> str:
return f"[![{self.name}]({self.img})]({self.url})"
return f"![{self.name}]({self.img})"
+3 -4
View File
@@ -2,11 +2,10 @@ from src.model.social import Social
class SocialList:
socials: list[Social]
def __init__(self, socials: list):
self.socials = [Social(social.get("name"), social.get("url"), social.get("img")) for social in socials]
self.socials = [Social(social.get("name"), social.get("url")) for social in socials]
def __str__(self) -> str:
return "\n".join([str(social) for social in self.socials])
def __repr__(self) -> str:
return "\n".join([str(social) for social in self.socials])
+94
View File
@@ -0,0 +1,94 @@
from urllib.parse import urlunsplit, urlencode
class ShieldBuilder:
BASE_URL = "https://img.shields.io/static/v1"
message: str = None
style: str = None
logo: str = None
logo_color: str = None
label: str = None
label_color: str = None
color: str = None
cache_seconds: int = None
link: str = None
def __init__(self):
self.logo_color = "white"
self.label = " "
self.color = "black"
def set_message(self, message: str):
self.message = (
message
.replace("_", "__")
.replace("-", "--")
.replace(" ", "_")
)
return self
def set_style(self, style: str):
if not style in ["flat", "flat-square", "plastic", "for-the-badge", "social"]:
raise Exception("Invalid style")
self.style = style
return self
def set_logo(self, logo: str):
self.logo = logo
return self
def set_logo_color(self, logo_color: str):
self.logo_color = logo_color
return self
def set_label(self, label: str):
self.label = label
return self
def set_label_color(self, label_color: str):
self.label_color = label_color
return self
def set_color(self, color: str):
self.color = color
return self
def set_cache_seconds(self, cache_seconds: int):
self.cache_seconds = cache_seconds
return self
def set_link(self, link: str):
self.link = link
return self
def get_query(self):
query = {
"message": self.message,
"style": self.style,
"logo": self.logo,
"logoColor": self.logo_color,
"label": self.label,
"labelColor": self.label_color,
"color": self.color,
"cacheSeconds": self.cache_seconds,
"link": self.link
}
# Remove None values
return {k: v for k, v in query.items() if v is not None}
def build(self):
query = urlencode(self.get_query())
return urlunsplit(("", "", self.BASE_URL, query, ""))
if "__main__" == __name__:
shield_builder = (
ShieldBuilder()
.set_logo("HTML5")
.set_message("HTML5")
.build()
)
print(shield_builder)
+10 -4
View File
@@ -1,4 +1,10 @@
def load_template(template_file_path: str):
with open(template_file_path, 'r') as template_file:
template_content = template_file.read()
return template_content
class Template:
def __init__(self, template_path: str):
self.template_path = template_path
def render(self, **kwargs) -> str:
with open(self.template_path, 'r') as f:
template = f.read()
return template.format(**kwargs)