Skip to content

Commit

Permalink
Ready for 1st release
Browse files Browse the repository at this point in the history
  • Loading branch information
Violeta-Tejera committed Jan 26, 2024
1 parent b2dea12 commit 8efe21d
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 49 deletions.
Binary file modified api/__pycache__/repo.cpython-311.pyc
Binary file not shown.
Binary file modified api/__pycache__/user.cpython-311.pyc
Binary file not shown.
22 changes: 15 additions & 7 deletions api/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Module providing functions for printing statistics of a GitHub repository.
Functions:
- print_statistics_repo: Prints various statistics for a given repository, such as license, contributors, languages,
downloads, forks, issues, commits, stargazers, and releases.
- print_statistics_repo: Prints various statistics for a given
repository, such as license, contributors, languages, downloads,
forks, issues, commits, stargazers, and releases.
Example:
# Import necessary modules
Expand All @@ -25,28 +26,35 @@
print_statistics_repo(user_data, repo)
"""

from github import Github, Repository
from github import Repository
from api.user import UserData
import datetime


def print_statistics_repo(user: UserData, repo: Repository):
"""Prints all statistics for a provided repo and user
Args:
user (UserData): User we want to analyze from the point of view of this repo
repo (Repository): Repo
"""
print(f"Showing repository statistics for {repo.full_name} in {user.year}")

# License
# License
try:
license = repo.get_license()
except BaseException:
print("This repository has no license")
else:
print("License: ", license.license.name)

# Contributors
# Contributors
contributors = repo.get_contributors()
print(f"This repository has {contributors.totalCount} contributors:")
for c in contributors:
print(f"- {c.login}")

# Languages
# Languages
languages = repo.get_languages().keys()
print(
f"This repository is written in {len(languages)} different languages: ")
Expand Down Expand Up @@ -79,7 +87,7 @@ def print_statistics_repo(user: UserData, repo: Repository):
commits_made_by_user = user.repo_commit[repo]["total_count_author"]
print(
f"This repository has had {commits_this_year} commits on {user.year}, of which {commits_made_by_user} were made by you")

# Stargazers
stargazers = repo.get_stargazers_with_dates()
stargazers_this_year = 0
Expand Down
117 changes: 106 additions & 11 deletions api/user.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""
Module providing a UserData class for handling GitHub user data and related functionalities.
Module: user_data_handler
This module provides a UserData class for handling GitHub user data and related functionalities.
Classes:
UserData: A class for managing GitHub user data, repositories, commits, and more.
Functions:
- __init__: Initializes an instance of the UserData class.
- get_created_repos: Returns a list of repositories created by the user in a certain year.
- get_contributed_repos: Returns a list of repositories contributed to by the user in a certain year.
- get_languages_user: Returns language statistics for the user's contributions.
- get_commit_data: Returns data related to the user's commit history.
Methods:
- __init__: Initializes an instance of the UserData class.
- get_created_repos: Returns a list of repositories created by the user in a certain year.
- get_contributed_repos: Returns a list of repositories contributed to by the user in a certain year.
- get_languages_user: Returns language statistics for the user's contributions.
- get_commit_data: Returns data related to the user's commit history.
Example:
# Creating an instance of UserData
Expand All @@ -33,6 +35,7 @@
# Getting repositories contributed to by the user in a certain year
contributed_repos = user_data.get_contributed_repos()
"""

from github import Github
Expand All @@ -42,14 +45,18 @@


class UserData:
"""
Class to store the data from the users
"""

def __init__(
self,
github_instance: Github,
username: str,
year: int,
show_private: bool,
show_repo_info: bool):

self.__github_instance = github_instance
self.__username = username
self.__year = year
Expand All @@ -71,96 +78,165 @@ def __init__(

@property
def github_instance(self):
"""
Getter for github_instance
"""
return self.__github_instance

@github_instance.setter
def github_instance(self, value):
"""
Setter for github_instance
"""
self.__github_instance = value

@property
def show_repo_info(self):
"""
Getter for show_repo_info
"""
return self.__show_repo_info

@show_repo_info.setter
def show_repo_info(self, value):
"""
Setter for show_repo_info
"""
self.__show_repo_info = value

@property
def username(self):
"""
Getter for username
"""
return self.__username

@username.setter
def username(self, value):
"""
Setter for username
"""
self.__username = value

@property
def year(self):
"""
Getter for year
"""
return self.__year

@year.setter
def year(self, value):
"""
Setter for year
"""
self.__year = value

@property
def show_private(self):
"""
Getter for show_private
"""
return self.__show_private

@show_private.setter
def show_private(self, value):
"""
Setter for show_private
"""
self.__show_private = value

@property
def user(self):
"""
Getter for user
"""
return self.__user

@user.setter
def user(self, value):
"""
Setter for user
"""
self.__user = value

@property
def commit_years(self):
"""
Getter for commit_years
"""
return self.__commit_years

@commit_years.setter
def commit_years(self, value):
"""
Setter for commit_years
"""
self.__commit_years = value

@property
def total_count(self):
"""
Getter for total_count
"""
return self.__total_count

@total_count.setter
def total_count(self, value):
"""
Setter for total_count
"""
self.__total_count = value

@property
def public_count(self):
"""
Getter for public_count
"""
return self.__public_count

@public_count.setter
def public_count(self, value):
"""
Setter for public_count
"""
self.__public_count = value

@property
def repo_commit(self):
"""
Getter for repo_commit
"""
return self.__repo_commit

@repo_commit.setter
def repo_commit(self, value):
"""
Setter for repo_commit
"""
self.__repo_commit = value

@property
def user_repos(self):
"""
Getter for user_repos
"""
return self.__user_repos

@user_repos.setter
def user_repos(self, value):
"""
Setter for user_repos
"""
self.__user_repos = value

def __get_commit_years_basic_data(self):
"""
Gets basic data related to commits, such as the total count (and public total count)
of commits of the year; and several data related to each repository
Note:
- Method is private
"""
start = datetime(self.year, 1, 1, 0, 0, 0)
end = datetime(self.year + 1, 1, 1, 0, 0, 0)
Expand Down Expand Up @@ -200,6 +276,9 @@ def get_created_repos(self):
"""
Returns a list of the repos created by a user in a certain year. You can toggle
whether or not you want to display the private repositories.
Returns:
list: repos created by a user in a certain year
"""
repos = [r for r in self.user_repos if r.fork is False and r.created_at.year ==
self.year and r.owner == self.github_instance.get_user(self.username)]
Expand All @@ -208,7 +287,11 @@ def get_created_repos(self):

def get_contributed_repos(self):
"""
Returns a list of the repos contributed to by a user in a certain year.
Returns a list of the repos contributed to by a
user in a certain year.
Returns:
list: Repos contributed to by a user in a certain year
"""
repositories_return = []

Expand All @@ -221,15 +304,24 @@ def get_contributed_repos(self):

# TODO: Reduce time: maybe instead of fetching by file we can get languages for each repo, but it might be an imprecise info (probably will). So a small db may be the best way to improve exec time for now.
def get_languages_user(self):

"""
Gets a dictionary with all the languages coded in this year, and the
number of file changes in each language
Returns:
dict: dictionary with all the languages coded in this year, and the
number of file changes in each language
"""

language_stats = defaultdict(int)

for repo in self.get_contributed_repos():
for commit in self.repo_commit[repo]["commits_repo_author"]:
for file in commit.files:
if (repo.visibility == "private" and self.show_private) or repo.visibility == "public":
extension = '.' + file.filename.split('.')[-1]
language = get_language(extension, "languages_extensions.db")
language = get_language(
extension, "languages_extensions.db")
language_stats[language] += file.changes

return dict(
Expand All @@ -242,6 +334,9 @@ def get_commit_data(self):
"""
Returns some commit dates data related to the longest commit streak duration, start and
ending dates; and the total days with commits of the year
Returns:
dict: Dictionary with commit data
"""

commit_dates = set()
Expand Down
8 changes: 4 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"username": "Violeta-Tejera",
"token": "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",
"year": 2023,
"showPrivate": false,
"username": "your username here",
"token": "your token here",
"year": 2022,
"showPrivate": true,
"showRepoInfo": true
}
Loading

0 comments on commit 8efe21d

Please sign in to comment.