Skip to content

Commit

Permalink
refactor: type variables
Browse files Browse the repository at this point in the history
  • Loading branch information
antwxne committed Jun 29, 2022
1 parent d51ff2d commit ecf1649
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 64 deletions.
41 changes: 22 additions & 19 deletions sources/document_generator.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#!/bin/python3

from os import getenv

from docx.table import Table

from sources.fetch_github import Issue, get_issues
from sources.json_file import get_json_from_file
from sources.document_utils import (add_title_center, add_picture_center, add_title,
save, add_paragraph_indent, page_break, create_array)
save, add_paragraph_indent, page_break, create_array, blank_line, add_paragraph)
from datetime import date

CONFIG_PATH = getenv("CONFIG_PATH")
RESOURCES_FOLDER = getenv("RESOURCES_FOLDER")
CONFIG_PATH: str = getenv("CONFIG_PATH")
RESOURCES_FOLDER: str = getenv("RESOURCES_FOLDER")


def create_cover_page(config: dict) -> None:
cover_image = f'{RESOURCES_FOLDER}/{config["cover-image"]["path"]}'
size = config["cover-image"]["size"]
cover_image: str = f'{RESOURCES_FOLDER}/{config["cover-image"]["path"]}'
size: dict[str, float] = config["cover-image"]["size"]
add_picture_center(cover_image, width=size["width"], height=size["height"])
add_title_center(config["title"], level=0)
add_title_center(config["sub-title"], level=1)
Expand All @@ -22,7 +25,7 @@ def create_cover_page(config: dict) -> None:

def create_document_description(description: dict) -> None:
add_title("Description du document")
array = create_array(9, 2)
array: Table = create_array(9, 2)
array.rows[0].cells[0].text = "Titre"
array.rows[0].cells[1].text = description["Titre"]
array.rows[1].cells[0].text = "Objet"
Expand All @@ -46,8 +49,8 @@ def create_document_description(description: dict) -> None:

def create_revision_table() -> None:
add_title("Description du document")
array = create_array(2, 5)
header_values = ["Date", "Version", "Auteur", "Section(s)", "Commentaires"]
array: Table = create_array(2, 5)
header_values: list[str] = ["Date", "Version", "Auteur", "Section(s)", "Commentaires"]
for index in range(len(array.rows[0].cells)):
array.rows[0].cells[index].text = header_values[index]
array.rows[1].cells[0].text = date.today().strftime("%d/%m/%Y")
Expand All @@ -67,8 +70,8 @@ def create_project_presentation(presentation: list[str]) -> None:

def create_organigramme_livrable(image_infos: dict) -> None:
add_title("Organigramme des livrables")
image_path = f'{RESOURCES_FOLDER}/{image_infos["path"]}'
size = image_infos["size"]
image_path: str = f'{RESOURCES_FOLDER}/{image_infos["path"]}'
size: dict[str, float] = image_infos["size"]
add_picture_center(image_path, size["width"], size["height"])
page_break()

Expand All @@ -77,14 +80,14 @@ def create_livrable_map(livrable_maps: list[dict]) -> None:
add_title("Carte des livrables")
for livrable in livrable_maps:
add_title(livrable["name"], 2)
image_path = f'{RESOURCES_FOLDER}/{livrable["path"]}'
size = livrable["size"]
image_path: str = f'{RESOURCES_FOLDER}/{livrable["path"]}'
size: dict[str, float] = livrable["size"]
add_picture_center(image_path, size["width"], size["height"])


def create_card(issue: Issue) -> None:
array = create_array(7, 2)
content = issue.body
array: Table = create_array(7, 2)
content: dict = issue.body
array.rows[0].cells[0].text = issue.title
array.rows[0].cells[0].merge(array.rows[0].cells[1])
array.rows[1].cells[0].text = "En tant que:"
Expand All @@ -103,17 +106,17 @@ def create_card(issue: Issue) -> None:

def create_stories_cards() -> None:
add_title("Tableau des stories")
all_issues = get_issues()
for sub_title in all_issues:
all_issues: dict[str, list[Issue]] = get_issues()
for sub_title, issues in all_issues.items():
add_title(sub_title, 2)
for issue in all_issues[sub_title]:
for issue in issues:
create_card(issue)
page_break()


def create_rapport() -> None:
add_title("Rapports d’avancement du projet")
array = create_array(5, 2)
array: Table = create_array(5, 2)
array.rows[0].cells[0].text = "Avancement global pour ce rendez-vous"
array.rows[0].cells[0].merge(array.rows[0].cells[1])
array.rows[1].cells[
Expand All @@ -127,7 +130,7 @@ def create_rapport() -> None:


def create_resume() -> None:
array = create_array(6, 1)
array: Table = create_array(6, 1)
array.rows[0].cells[0].text = "Points bloquants"
array.rows[1].cells[0].text = "Rappel de points et tickets en souffrance"
array.rows[2].cells[0].text = ""
Expand Down
7 changes: 6 additions & 1 deletion sources/document_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from docx.shared import Inches, Cm
from docx.table import Table

DOCUMENT = Document()
DOCUMENT: Document = Document()


def center() -> None:
Expand Down Expand Up @@ -50,6 +50,11 @@ def create_array(rows: int, cols: int) -> Table:
return DOCUMENT.add_table(rows=rows, cols=cols)


def blank_line() -> None:
DOCUMENT.paragraphs[-1].add_run().add_break()
DOCUMENT.paragraphs[-1].add_run().add_break()


def page_break() -> None:
DOCUMENT.add_page_break()

Expand Down
69 changes: 30 additions & 39 deletions sources/fetch_github.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
#!/bin/python3
from os import getenv
from typing import Union

import requests
from bs4 import BeautifulSoup
from marko.ext.gfm import gfm as GithubMarkdownParser
from os import getenv
import requests
from requests import Response

from sources.json_file import get_json_from_file, write_json
from sources.json_file import get_json_from_file


def load_name_association() -> dict:
dest = {}
file_path = getenv("USERS_CONFIG_PATH")
try:
json_list = get_json_from_file(file_path)
for elem in json_list:
dest[elem["user_name"]] = elem["name"]
except (ValueError, KeyError) as e:
print(e)
return dest
def load_name_association() -> dict[str, str]:
file_path: str = getenv("USERS_CONFIG_PATH")
json_list: list[dict] = get_json_from_file(file_path)
return {
elem["user_name"]: elem["name"]
for elem in json_list
}


def get_repository_list() -> list[str]:
file_path = getenv("REPOSITORY_LIST_PATH")
repo = []
try:
json_list = get_json_from_file(file_path)
repo = [elem["repository"] for elem in json_list]
except (ValueError, KeyError) as e:
print(e)
return repo


def get_issues() -> dict:
repositories = get_repository_list()
file_path: str = getenv("REPOSITORY_LIST_PATH")
json_list: list[dict] = get_json_from_file(file_path)
return [elem["repository"] for elem in json_list]


def get_issues() -> dict[str, list["Issue"]]:
repositories: list[str] = get_repository_list()
dest: dict[str, list[Issue]] = {}
for repository in repositories:
issues = fetch_issues(repository)
issues: list[Issue] = fetch_issues(repository)
for issue in issues:
if issue.labels[0] in dest:
dest[issue.labels[0]].append(issue)
Expand All @@ -44,18 +38,16 @@ def get_issues() -> dict:
return dest


NAME_CONVERTER = load_name_association()
NAME_CONVERTER: dict[str, str] = load_name_association()


class Issue:
def __init__(self, json_response: dict):
# print("json response == ", json_response)
self.title = json_response["title"]
self.labels = [label["name"] for label in json_response["labels"]]
self.labels.remove("PLD")
self.assignees = [NAME_CONVERTER[assignee["login"]] for assignee in json_response["assignees"]]
self.milestone = json_response["milestone"]
self.body = self.parse_md_body(json_response["body"])
self.title: str = json_response["title"]
self.labels: list[str] = [label["name"] for label in json_response["labels"] if label["name"] != "PLD"]
self.assignees: list[str] = [NAME_CONVERTER[assignee["login"]] for assignee in json_response["assignees"]]
self.milestone: str = json_response["milestone"]
self.body: dict[str, Union[str, list[str]]] = self.parse_md_body(json_response["body"])

def to_json(self) -> dict:
"""
Expand All @@ -73,14 +65,14 @@ def to_json(self) -> dict:
}

@staticmethod
def parse_md_body(body: str) -> dict:
def parse_md_body(body: str) -> dict[str, Union[str, list[str]]]:
"""
`parse_md_body` takes a string and returns a dictionary
Args:
body (str): The body of the markdown file.
"""
dest = {}
dest: dict[str, Union[str, list[str]]] = {}
md_converted = GithubMarkdownParser(body)
soup = BeautifulSoup(md_converted, features="lxml")
for key in soup.findAll("h2"):
Expand All @@ -103,12 +95,11 @@ def fetch_issues(repo: str) -> list[Issue]:
Returns:
A list of Issue objects
"""
res = requests.get(
res: Response = requests.get(
f"https://api.github.com/repos/{repo}/issues?labels=PLD",
headers={
"Accept": "application/vnd.github.v3+json",
"Authorization": f'token {getenv("OAUTH_TOKEN")}'
}
)
issues = [Issue(issue) for issue in res.json()]
return issues
return [Issue(issue) for issue in res.json()]
11 changes: 6 additions & 5 deletions sources/json_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from json import load, dump
from os import path
from typing import Union


def write_json(json_data, filename):
def write_json(json_data: Union[dict, list[dict]], filename: str):
""" Write supposed json data into a file
Args:
Expand All @@ -15,7 +16,8 @@ def write_json(json_data, filename):
dump(json_data, file, indent=4)
file.close()

def get_json_from_file(filename: str) -> dict:

def get_json_from_file(filename: str) -> Union[dict, list[dict]]:
""" Parse a file containing a json
Args:
Expand All @@ -28,9 +30,8 @@ def get_json_from_file(filename: str) -> dict:
Any: json parsed
"""
if path.exists(filename) and path.isfile(filename):
file = open(filename, "r")
json_data = load(file)
file.close()
with open(filename, "r") as f:
json_data: Union[dict, list[dict]] = load(fp=f)
return json_data
else:
raise ValueError("Wrong filename: " + filename + " doesn't exist")

0 comments on commit ecf1649

Please sign in to comment.