Skip to content

Commit

Permalink
- Generate Designer examples and training archives at doc setup time.
Browse files Browse the repository at this point in the history
- Fix contributors list generation (force file io encoding).
  • Loading branch information
FabienLelaquais committed Nov 29, 2024
1 parent 314a0ed commit 4cb0adf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 112 deletions.
36 changes: 17 additions & 19 deletions tools/_setup_generation/step_contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class ContributorsStep(SetupStep):

def __init__(self):
self.GH_TOKEN = os.getenv("GITHUB_TOKEN", None)
self.BASE_URL = "https://api.github.com"
Expand Down Expand Up @@ -52,30 +51,30 @@ def get_repo_urls(self):
print(f"WARNING - Couldn't get repositories. response.status_code: {response.status_code}", flush=True)
return
repos = response.json()
self.REPO_URLS = list(map(lambda _: _['url'], repos))
self.REPO_URLS = list(map(lambda _: _["url"], repos))

def get_avaiga_members(self):
response = self.__get(self.MEMBERS_URL, with_token=False)
response = self.__get(self.MEMBERS_URL)
if response.status_code != 200:
print(f"WARNING - Couldn't get members. response.status_code: {response.status_code}", flush=True)
return
members = response.json()
for member in members:
login = member['login']
login = member["login"]
if login not in self.MEMBERS and login not in self.ANONYMOUS:
self.MEMBERS[login] = {"avatar_url": member['avatar_url'], "html_url": member['html_url']}
self.MEMBERS[login] = {"avatar_url": member["avatar_url"], "html_url": member["html_url"]}

def get_contributors(self):
for url in self.REPO_URLS:
response = self.__get(url + "/contributors")
if response.status_code != 200:
print(f"WARNING - Couldn't get collaborators. response.status_code: {response.status_code}", flush=True)
print(f"WARNING - Couldn't get contributors. response.status_code: {response.status_code}", flush=True)
return
contributors = response.json()
for contrib in contributors:
login = contrib['login']
login = contrib["login"]
if login not in self.CONTRIBUTORS and login not in self.ANONYMOUS:
self.CONTRIBUTORS[login] = {"avatar_url": contrib['avatar_url'], "html_url": contrib['html_url']}
self.CONTRIBUTORS[login] = {"avatar_url": contrib["avatar_url"], "html_url": contrib["html_url"]}

def build_content(self, *members_pattern_tuples):
pattern_content_tuples = []
Expand All @@ -87,17 +86,19 @@ def build_content(self, *members_pattern_tuples):
random.shuffle(members_list)
for login, member_info in members_list:
if login not in self.ANONYMOUS:
content += f"\n- [<img src='{member_info['avatar_url']}' alt='{login} GitHub avatar' width='20'/>" \
f"{login}]" \
f"({member_info['html_url']})"
content += (
f"\n- [<img src='{member_info['avatar_url']}' alt='{login} GitHub avatar' width='20'/>"
f"{login}]"
f"({member_info['html_url']})"
)
content += "\n"
pattern_content_tuples.append((pattern, content))

self._replace(self.PATH, *pattern_content_tuples)

def _replace(self, path, *pattern_content_tuples):
# Read in the file
with open(path + self.TEMPLATE_SUFFIX, 'r') as file:
with open(path + self.TEMPLATE_SUFFIX, "r") as file:
file_data = file.read()

# Replace the patterns by the contents
Expand All @@ -107,17 +108,14 @@ def _replace(self, path, *pattern_content_tuples):
file_data = file_data.replace(pattern, content)

# Write the file out without the template suffix
with open(path, 'w') as file:
with open(path, "w") as file:
file.write(file_data)

def __get(self, url, with_token=True):
if with_token and self.GH_TOKEN:
headers = {
"Accept": "application/vnd.github+json",
"Authorization": "Bearer "+self.GH_TOKEN
}
# {'Authorization': f'token {self.GH_TOKEN}'}
return requests.get(url, headers=headers)
return requests.get(
url, headers={"Accept": "application/vnd.github+json", "Authorization": "Bearer " + self.GH_TOKEN}
)
else:
return requests.get(url)

Expand Down
12 changes: 6 additions & 6 deletions tools/_setup_generation/step_designer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def get_id(self) -> str:
def get_description(self) -> str:
return "Retrieve the Designer documentation files."

def setup(self, setup: Setup): ...
def setup(self, _: Setup): ...

def enter(self, setup: Setup):
if os.path.exists(os.path.join(setup.docs_dir, DesignerStep.PREFIX)):
self.DESIGNER_PATH = os.path.join(
setup.docs_dir, *DesignerStep.PREFIX.split("/")
)
self.MKDOCS_TMPL = os.path.join(self.DESIGNER_PATH, "mkdocs.yml_template")
if not os.access(self.MKDOCS_TMPL, os.R_OK):
self.MKDOCS_TEMPLATE = os.path.join(self.DESIGNER_PATH, "mkdocs.yml_template")
if not os.access(self.MKDOCS_TEMPLATE, os.R_OK):
raise FileNotFoundError(
f"FATAL - Could not read docs/{DesignerStep.PREFIX}/mkdocs.yml_template"
)
Expand All @@ -46,18 +46,18 @@ def exit(self, setup: Setup):
def _read_mkdocs_template(self) -> str:
lines = []
indentation = 0
with open(self.MKDOCS_TMPL) as file:
with open(self.MKDOCS_TEMPLATE) as file:
collect = False
for line in file:
if line.startswith("nav:"): # Start collecting navigation
collect = True
elif re.match(r"^[\w_]+\s*?:", line): # Stop collecting navigation
if collect:
navigation = StringIO()
for navline in lines:
for nav_line in lines:
# Add each line with indentation removed
navigation.write(" ")
navigation.write(navline[indentation:])
navigation.write(nav_line[indentation:])
navigation.write("\n")
return navigation.getvalue()
elif collect:
Expand Down
19 changes: 7 additions & 12 deletions tools/_setup_generation/step_file_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,19 @@ def get_description(self) -> str:

def setup(self, setup: Setup) -> None:
try:
with open(self.src_path, 'r') as file:
with open(self.src_path, "r", encoding="utf-8") as file:
content = file.read()
self._replace(self.dst_tpl_path, self.pattern, content, self.dst_path)
except Exception as e:
print(f"Error: cannot generate page: {e}")
print(f"ERROR - Cannot generate page: {e}")

@staticmethod
def _replace(in_tpl_file_path, pattern, by, into_file_path):
# Read template file
with open(in_tpl_file_path, 'r') as tpl_file:
from_file_content = tpl_file.read()

with open(in_tpl_file_path, "r", encoding="utf-8") as file:
content = file.read()
# Replace the pattern by the contents
content = from_file_content.replace(pattern, by)

content = content.replace(pattern, by)
# Write the file to_file
with open(into_file_path, 'w') as into_file:
into_file.write(content)

def exit(self, setup: Setup):
pass
with open(into_file_path, "w", encoding="utf-8") as file:
file.write(content)
Loading

0 comments on commit 4cb0adf

Please sign in to comment.