Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Refactoring of stats_plots #53

Merged
merged 6 commits into from
May 2, 2021
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [UNRELEASED](https://github.com/astrochun/github-stats-pages/tree/HEAD) (YYYY-MM-DD)

**Closed issues:**
- Chore: Refactoring of stats_plots [#51](http://github.com/astrochun/github-stats-pages/issues/51)

**Merged pull requests:**
- Chore: Refactoring of stats_plots [#52](http://github.com/astrochun/github-stats-pages/pull/53)


## [v0.3.9](https://github.com/astrochun/github-stats-pages/tree/v0.3.9) (2021-04-28)

**Closed issues:**
Expand Down
123 changes: 85 additions & 38 deletions github_stats_pages/stats_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ def refer_subplots(df: pd.DataFrame, y_column: str, title: str = '',
"""

x = df['source'].to_list()
'''print(x)
s.x_range = FactorRange(*x)'''

s = figure(plot_width=pw, plot_height=ph, title=title,
background_fill_color=bc, border_fill_color=bfc,
Expand All @@ -139,9 +137,6 @@ def refer_subplots(df: pd.DataFrame, y_column: str, title: str = '',
s.axis.minor_tick_line_color = None

s.grid.grid_line_color = "#ffffff"
# s.axis.minor_tick_in = 3
# s.axis.minor_tick_out = 0
# s.axis.subgroup_label_orientation = "normal"

y = df[y_column]
s.vbar(x=x, top=y, width=0.9, fill_color="#f8b739", fill_alpha=0.8,
Expand Down Expand Up @@ -213,23 +208,9 @@ def make_plots(username: str, data_dir: str, out_dir: str, csv_file: str,
for key, df in dict_df.items():
repo_names.update(set(df[columns[0]].unique()))

final_repo_names = repo_names # init

# Filter for only inclusion
if include_repos:
print(f"Only including: {include_repos.replace(',', ', ')}")
final_repo_names = repo_names & set(include_repos.split(','))

# Filter for exclusion
if exclude_repos:
print(f"Excluding: {exclude_repos.replace(',', ', ')}")
final_repo_names = repo_names - set(exclude_repos.split(','))

for exclude in exclude_repos.split(','):
p_exclude = Path(p_repos / f"{exclude}.html")
print(f"Deleting: {p_exclude}")
if p_exclude.exists():
p_exclude.unlink()
final_repo_names = get_final_repo_names(p_repos, repo_names,
include_repos=include_repos,
exclude_repos=exclude_repos)

n_final_repo_names = len(final_repo_names)
print(f"Number of GitHub repositories: {n_final_repo_names}")
Expand All @@ -246,27 +227,16 @@ def make_plots(username: str, data_dir: str, out_dir: str, csv_file: str,
# Retrieve README.md file for user
readme_html = user_readme(username)

headers = {}
if token:
headers['Authorization'] = f"token {token}"
avatar_response = get(f'https://api.github.com/users/{username}',
headers=headers).json()
jinja_dict = {
'username': username,
'avatar_url': avatar_response['avatar_url'],
'repos': sorted(final_repo_names),
'readme_html': readme_html,
}
avatar_response, jinja_dict = get_jinja_dict(username, token,
final_repo_names,
readme_html)

# Write HTML Files
template_p = main_p / 'templates'
file_loader = FileSystemLoader(template_p)
env = Environment(loader=file_loader)
for file in ['index', 'about', 'repositories']:
t_index = env.get_template(f"{file}.html")
out_file = Path(out_dir) / f"{file}.html"
with open(out_file, 'w') as f:
f.writelines(t_index.render(jinja_dict=jinja_dict))

write_common_html(env, jinja_dict, out_dir)

# Copy or symlink files
source = template_p / "styles"
Expand Down Expand Up @@ -339,3 +309,80 @@ def make_plots(username: str, data_dir: str, out_dir: str, csv_file: str,
out_file = p_repos / f"{r}.html"
with open(out_file, 'w') as f:
f.writelines(t.render(jinja_dict=jinja_dict))


def get_final_repo_names(p_repos: Path, repo_names: set,
include_repos: str = '',
exclude_repos: str = '') -> set:
"""
Filter for repositories that are specifically included/excluded

:param p_repos: Path to repository folder
:param repo_names: Set of all user/organization's repositories
:param include_repos: Comma-separated list of repositories to include
:param exclude_repos: Comma-separated list of repositories to exclude

:return: Final repository set
"""
final_repo_names = repo_names.copy()

# Filter for only inclusion
if include_repos:
print(f"Only including: {include_repos.replace(',', ', ')}")
final_repo_names = repo_names & set(include_repos.split(','))

# Filter for exclusion
if exclude_repos:
print(f"Excluding: {exclude_repos.replace(',', ', ')}")
final_repo_names = repo_names - set(exclude_repos.split(','))

for exclude in exclude_repos.split(','):
p_exclude = Path(p_repos / f"{exclude}.html")
print(f"Deleting: {p_exclude}")
if p_exclude.exists():
p_exclude.unlink()

return final_repo_names


def get_jinja_dict(username: str, token: str, final_repo_names: set,
readme_html: str) -> Tuple[dict, dict]:
"""
Provides a dictionary for Jinja templating

:param username: GitHub username or organization
:param token: GitHub Personal Access Token (this is to avoid rate limits)
:param final_repo_names: List of working GitHub repository name
:param readme_html: Contains user README profile

:return: Avatar JSON, Jinja dict
"""
headers = {}
if token:
headers['Authorization'] = f"token {token}"
avatar_response = get(f'https://api.github.com/users/{username}',
headers=headers).json()
jinja_dict = {
'username': username,
'avatar_url': avatar_response['avatar_url'],
'repos': sorted(final_repo_names),
'readme_html': readme_html,
}
return avatar_response, jinja_dict


def write_common_html(env: Environment, jinja_dict: dict,
out_dir: str):
"""
Write index, about, and repositories HTML

:param env: Jinja Environment
:param jinja_dict: Dictionary for jinja templating
:param out_dir: Output file path directory
"""

for file in ['index', 'about', 'repositories']:
t_index = env.get_template(f"{file}.html")
out_file = Path(out_dir) / f"{file}.html"
with open(out_file, 'w') as f:
f.writelines(t_index.render(jinja_dict=jinja_dict))