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

Code Cleanup #371

Merged
merged 27 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
243e048
empty colors file removed
pseusys Feb 16, 2023
3e7044d
NodeJS mentions removed
pseusys Feb 16, 2023
ee1b2c0
.env file read with Make
pseusys Feb 16, 2023
904cb7b
.env.example tokens combined
pseusys Feb 16, 2023
c9905b5
dockerfile build environment clarified
pseusys Feb 16, 2023
52318d9
generated files moved to 'assets' dir, graph drawing fixed
pseusys Feb 16, 2023
e8a1770
generated files moved to 'assets' dir
pseusys Feb 16, 2023
8e675ea
code style applied to main
pseusys Feb 17, 2023
c378e68
codestyle applying done
pseusys Feb 17, 2023
df515b2
coroutine cancellation added
pseusys Feb 17, 2023
28c4c66
codestyle added
pseusys Feb 17, 2023
94da25c
python version updated
pseusys Feb 17, 2023
b36e374
ubuntu version changed to compatible with python 3.6
pseusys Feb 17, 2023
2e494aa
documentation added, `black` linter added
pseusys Feb 17, 2023
a432057
py3.6 pygithub version compatibility
pseusys Feb 17, 2023
4afa664
required python version updated to 3.8 (3.6 is pretty old though)
pseusys Feb 17, 2023
1778166
actions version updated
pseusys Feb 17, 2023
e66fc67
small fixes
pseusys Feb 17, 2023
99f85cb
sorting, reversed
pseusys Feb 17, 2023
028bf23
github manager fix
pseusys Feb 17, 2023
ee33148
keeping up-to-date
pseusys Feb 17, 2023
11f02c5
Merge branch 'master' into feat/code_cleanup
pseusys Feb 17, 2023
36fafbd
query limitations info added
pseusys Feb 18, 2023
1dc8b30
timezone emoji updated
pseusys Feb 18, 2023
0b4f9ef
keeping up to date
pseusys Feb 21, 2023
a97119e
linted
pseusys Feb 21, 2023
02948ca
Keeping up to date
pseusys Feb 22, 2023
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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
INPUT_WAKATIME_API_KEY=YOUR_WAKATIME_API_KEY
INPUT_GH_TOKEN=YOUR_GITHUB_TOKEN_KEY
INPUT_PUSH_BRANCH_NAME=main
INPUT_SECTION_NAME=waka
INPUT_SHOW_TIMEZONE=True
INPUT_SHOW_PROJECTS=True
INPUT_SHOW_EDITORS=True
INPUT_SHOW_OS=True
INPUT_SHOW_LANGUAGE=True
INPUT_GH_TOKEN=YOUR_GITHUB_TOKEN_KEY
INPUT_SYMBOL_VERSION=1
INPUT_SHOW_LINES_OF_CODE=True
INPUT_SHOW_LOC_CHART=True
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
type=semver,pattern={{major}}.{{minor}}

- name: Build and push Docker image 🏗️
uses: docker/build-push-action@v3
uses: docker/build-push-action@v4
with:
push: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/releases') }}
tags: ${{ steps.meta.outputs.tags }}
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CODESTYLE

on:
push:

jobs:
lint:
name: Run codestyle check
runs-on: ubuntu-latest

steps:
- name: Checkout 🛎️
uses: actions/checkout@v3

- name: Setup Python 3.8 🐍
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Install Dependencies 📥
run: pip install -r requirements.txt

- name: Run Codestyle ✔️
run: flake8 --max-line-length=160 --exclude venv,assets . && black --line-length=160 --check --exclude='/venv/|/assets/' .
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
*.env

# Generated graph images:
*.png
assets/

# Library roots:
node_modules/
venv/

# Python caches:
__pycache__/

# Package manager configuration files:
package.json
package-lock.json

# IDE configuration files:
.vscode
.idea
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Once you've worked on your feature/bugfix etc, you can open a pull request using

### Setting up development environment

This project is written in Python, requires **Python 3.6 or higher**, and uses `pip` .
This project is written in Python, requires **Python 3.8 or higher**, and uses `pip` .

To set it up, just fork + clone it, install all the dependencies:

Expand Down
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ FROM python:3.9-alpine
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

WORKDIR /waka-readme-stats
RUN mkdir -p /waka-readme-stats/assets

ADD requirements.txt ./requirements.txt
RUN apk add --no-cache g++ jpeg-dev zlib-dev libjpeg make && pip3 install -r requirements.txt
ADD requirements.txt /waka-readme-stats/requirements.txt
RUN apk add --no-cache g++ jpeg-dev zlib-dev libjpeg make && pip3 install -r /waka-readme-stats/requirements.txt

ADD sources/* ./
ENTRYPOINT python3 /waka-readme-stats/main.py
ADD sources/* /waka-readme-stats/
ENTRYPOINT cd /waka-readme-stats/ && python3 main.py
29 changes: 17 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
.ONESHELL:
.DEFAULT_GOAL = help
SHELL = /bin/bash
.EXPORT_ALL_VARIABLES:

PATH := venv/bin:node_modules/.bin:$(PATH)
PATH := venv/bin:$(PATH)

ENV = .env.example
include $(ENV)


help:
@ # Print help commands
echo "Welcome to 'waka-readme-stats' GitHub Actions!"
echo "The action can be tested locally with: 'make run'."
echo "NB! For local testing Python version 3.6+ and NodeJS version between 14 and 16 are required."
echo "NB! For local testing Python version 3.8+ is required."
echo "The action image can be built locally with: 'make container'."
echo "NB! For local container building Docker version 20+ is required."
echo "The action directory and image can be cleaned with: 'make clean'."
Expand All @@ -21,28 +24,30 @@ venv:
pip install --upgrade pip
pip install -r requirements.txt

node_modules:
@ # Install NodeJS dependencies
npm i npm@next-8
npm i vega vega-lite vega-cli canvas


run-locally: venv node_modules
run-locally: venv
@ # Run action locally
source <(cat .env.example | sed 's/=/=/' | sed 's/^/export /') && python3 ./sources/main.py
mkdir ./assets/ 2>/dev/null || true
python3 ./sources/main.py
.PHONY: run-locally

run-container:
@ # Run action in container
docker build -t waka-readme-stats -f Dockerfile .
docker run --env-file .env.example waka-readme-stats
docker run --env-file $(ENV) -v ./assets/:/waka-readme-stats/assets/ waka-readme-stats
.PHONY: run-container


lint: venv
@ # Run flake8 and black linters
flake8 --max-line-length=160 --exclude venv,assets .
black --line-length=160 --exclude='/venv/|/assets/' .
.PHONY: lint

clean:
@ # Clean all build files, including: libraries, package manager configs, docker images and containers
rm -rf venv
rm -rf node_modules
rm -rf assets
rm -f package*.json
docker rm -f waka-readme-stats 2>/dev/null || true
docker rmi $(docker images | grep "waka-readme-stats") 2> /dev/null || true
Expand Down
28 changes: 14 additions & 14 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,20 @@ inputs:
description: "Shows the short facts"
default: "True"

LOCALE:
SHOW_UPDATED_DATE:
required: false
description: "Show stats in your own language"
default: "en"
description: "Show updated date"
default: "True"

SHOW_TOTAL_CODE_TIME:
required: false
description: "Show Total Time you have coded"
default: "True"

COMMIT_BY_ME:
required: false
description: "Git commit with your own name and email"
default: "False"

IGNORED_REPOS:
required: false
description: "Repos you don't want to be counted"
default: ""

COMMIT_MESSAGE:
required: false
Expand All @@ -112,20 +112,20 @@ inputs:
description: "Git commit custom email"
default: ""

SHOW_UPDATED_DATE:
LOCALE:
required: false
description: "Show updated date"
default: "True"
description: "Show stats in your own language"
default: "en"

UPDATED_DATE_FORMAT:
required: false
description: "Updated date format"
default: "%d/%m/%Y %H:%M:%S"

SHOW_TOTAL_CODE_TIME:
IGNORED_REPOS:
required: false
description: "Show Total Time you have coded"
default: "True"
description: "Repos you don't want to be counted"
default: ""

SYMBOL_VERSION:
required: false
Expand Down
26 changes: 18 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
PyGithub==1.54.1
matplotlib==3.6.3
numpy==1.24.2
python-dotenv==0.17.0
pytz==2021.1
humanize==3.3.0
httpx==0.23.3
PyYAML==6.0
# GitHub integration modules:
PyGithub~=1.57

# Markdown visualization modules:
pytz~=2022.7
humanize~=4.6

# Graphs drawing modules:
matplotlib~=3.7
numpy~=1.24

# Request making and response parsing modules:
httpx~=0.23
PyYAML~=6.0

# Codestyle checking modules:
flake8~=6.0
black~=23.1
Empty file removed sources/colors.json
Empty file.
136 changes: 67 additions & 69 deletions sources/make_bar_graph.py → sources/graphics_chart_drawer.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,67 @@
from typing import Dict
from os.path import join, dirname
from json import load

import numpy as np
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

from download_manager import DownloadManager


MAX_LANGUAGES = 5


async def build_graph(yearly_data: Dict) -> str:
"""
Draws graph of lines of code written by user by quarters of years.
Picks top `MAX_LANGUAGES` languages from each quarter only.

:param yearly_data: GitHub user yearly data.
:return: String, path to graph file.
"""
colors = await DownloadManager.get_remote_yaml("linguist")

languages_all_loc = dict()
years = len(yearly_data.keys())
year_indexes = np.arange(years)

for i, y in enumerate(sorted(yearly_data.keys())):
for q in yearly_data[y].keys():
langs = sorted(yearly_data[y][q].keys(), key=lambda l: yearly_data[y][q][l], reverse=True)[0:MAX_LANGUAGES]

for lang in langs:
if lang not in languages_all_loc:
languages_all_loc[lang] = np.array([[0] * years] * 4)
languages_all_loc[lang][q - 1][i] = yearly_data[y][q][lang]

fig = plt.figure()
ax = fig.add_axes([0, 0, 1.5, 1])

language_handles = []
cumulative = np.array([[0] * years] * 4)

for key, value in languages_all_loc.items():
color = colors[key]["color"] if colors[key]["color"] is not None else "w"
language_handles += [mpatches.Patch(color=color, label=key)]

for quarter in range(4):
ax.bar(year_indexes + quarter * 0.21, value[quarter], 0.2, bottom=cumulative[quarter], color=color)
cumulative[quarter] = np.add(cumulative[quarter], value[quarter])

ax.set_ylabel("LOC added", fontdict=dict(weight="bold"))
ax.set_xticks(np.array([np.arange(i, i + 0.84, step=0.21) for i in year_indexes]).flatten(), labels=["Q1", "Q2", "Q3", "Q4"] * years)

sax = ax.secondary_xaxis("top")
sax.set_xticks(year_indexes + 0.42, labels=sorted(yearly_data.keys()))
sax.spines["top"].set_visible(False)

ax.legend(title="Language", handles=language_handles, loc="upper left", bbox_to_anchor=(1, 1), framealpha=0, title_fontproperties=dict(weight="bold"))

sax.tick_params(axis="both", length=0)
sax.spines["top"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)

plt.ylim(0, 1.05 * np.amax(cumulative))
plt.savefig("bar_graph.png", bbox_inches="tight")
plt.close(fig)
return "bar_graph.png"
from typing import Dict

from numpy import arange, array, add, amax
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

from manager_download import DownloadManager as DM


MAX_LANGUAGES = 5 # Number of top languages to add to chart, for each year quarter
GRAPH_PATH = "assets/bar_graph.png" # Chart saving path.


async def create_loc_graph(yearly_data: Dict, save_path: str):
"""
Draws graph of lines of code written by user by quarters of years.
Picks top `MAX_LANGUAGES` languages from each quarter only.

:param yearly_data: GitHub user yearly data.
:param save_path: Path to save the graph file.
"""
colors = await DM.get_remote_yaml("linguist")

years = len(yearly_data.keys())
year_indexes = arange(years)

languages_all_loc = dict()
for i, y in enumerate(sorted(yearly_data.keys())):
for q in yearly_data[y].keys():
langs = sorted(yearly_data[y][q].keys(), key=lambda n: yearly_data[y][q][n], reverse=True)[0:MAX_LANGUAGES]

for lang in langs:
if lang not in languages_all_loc:
languages_all_loc[lang] = array([[0] * years] * 4)
languages_all_loc[lang][q - 1][i] = yearly_data[y][q][lang]

fig = plt.figure()
ax = fig.add_axes([0, 0, 1.5, 1])

language_handles = []
cumulative = array([[0] * years] * 4)

for key, value in languages_all_loc.items():
color = colors[key]["color"] if colors[key]["color"] is not None else "w"
language_handles += [mpatches.Patch(color=color, label=key)]

for quarter in range(4):
ax.bar(year_indexes + quarter * 0.21, value[quarter], 0.2, bottom=cumulative[quarter], color=color)
cumulative[quarter] = add(cumulative[quarter], value[quarter])

ax.set_ylabel("LOC added", fontdict=dict(weight="bold"))
ax.set_xticks(array([arange(i, i + 0.84, step=0.21) for i in year_indexes]).flatten(), labels=["Q1", "Q2", "Q3", "Q4"] * years)

sax = ax.secondary_xaxis("top")
sax.set_xticks(year_indexes + 0.42, labels=sorted(yearly_data.keys()))
sax.spines["top"].set_visible(False)

ax.legend(title="Language", handles=language_handles, loc="upper left", bbox_to_anchor=(1, 1), framealpha=0, title_fontproperties=dict(weight="bold"))

sax.tick_params(axis="both", length=0)
sax.spines["top"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)

plt.ylim(0, 1.05 * amax(cumulative))
plt.savefig(save_path, bbox_inches="tight")
plt.close(fig)
Loading