Skip to content

Commit

Permalink
feat: Add csv export option
Browse files Browse the repository at this point in the history
It's a nice feature AND it provides a consistent export which is critical for the e2e snapshot test (json.dump doesn't guarantee key ordering)
  • Loading branch information
thehale committed Nov 10, 2024
1 parent 4eef733 commit 2c125ba
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions git_authorship/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def run(args: Args):
repo_authorship = authorship.for_repo(repo)
export.as_treemap(repo_authorship)
export.as_json(repo_authorship)
export.as_csv(repo_authorship)


def main(argv=None):
Expand Down
24 changes: 23 additions & 1 deletion git_authorship/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import csv
import json
from pathlib import Path

Expand Down Expand Up @@ -69,4 +70,25 @@ def as_json(
json.dump({str(path): authors for path, authors in authorship.items()}, f)


__all__ = ["as_treemap", "as_json"]
def as_csv(
authorship: RepoAuthorship, output: Writeable = Path("build/authorship.csv")
):
"""
Exports the authorship in CSV format
Args:
authorship (RepoAuthorship): The authorship to export
output (Union[PathLike, IO]): The output file path or handle.
If a path, it will be open and closed. Handles are left open.
"""
with io_handle(output) as f:
writer = csv.writer(f)
writer.writerow(["path", "author", "lines"])
for path, authors in authorship.items():
for author, lines in sorted(
authors.items(), key=lambda x: x[1], reverse=True
):
writer.writerow([path, author, lines])


__all__ = ["as_treemap", "as_json", "as_csv"]
40 changes: 40 additions & 0 deletions test/snapshots/test_e2e/test_full_workflow/authorship.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
path,author,lines
.,Joseph Hale <[email protected]>,241532
.,dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>,116
.,Joseph Hale <[email protected]>,74
git_authorship,Joseph Hale <[email protected]>,171
git_authorship/__main__.py,Joseph Hale <[email protected]>,166
git_authorship/__init__.py,Joseph Hale <[email protected]>,5
.gitignore,Joseph Hale <[email protected]>,169
LICENSE,Joseph Hale <[email protected]>,373
README.md,Joseph Hale <[email protected]>,139
README.md,Joseph Hale <[email protected]>,5
.devcontainer,Joseph Hale <[email protected]>,41
.devcontainer/devcontainer.json,Joseph Hale <[email protected]>,32
.devcontainer/Dockerfile,Joseph Hale <[email protected]>,9
pyproject.toml,Joseph Hale <[email protected]>,45
pyproject.toml,dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>,7
docs,Joseph Hale <[email protected]>,240056
docs/git-authorship-demo-cubingjs.gif,Joseph Hale <[email protected]>,238279
docs/git-authorship-social-preview.svg,Joseph Hale <[email protected]>,1111
docs/git-authorship-social-preview.png,Joseph Hale <[email protected]>,666
config,Joseph Hale <[email protected]>,12
config/.gitignore,Joseph Hale <[email protected]>,8
config/pseudonyms.txt.dist,Joseph Hale <[email protected]>,2
config/author-licenses.txt.dist,Joseph Hale <[email protected]>,2
poetry.lock,Joseph Hale <[email protected]>,433
poetry.lock,dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>,109
.github,Joseph Hale <[email protected]>,69
.github,Joseph Hale <[email protected]>,13
.github/dependabot.yml,Joseph Hale <[email protected]>,22
.github/workflows,Joseph Hale <[email protected]>,47
.github/workflows,Joseph Hale <[email protected]>,13
.github/workflows/auto-merge-dependabot.yml,Joseph Hale <[email protected]>,23
.github/workflows/precommit.yml,Joseph Hale <[email protected]>,24
.github/workflows/precommit.yml,Joseph Hale <[email protected]>,13
test,Joseph Hale <[email protected]>,9
test/test_authorship_analyzer.py,Joseph Hale <[email protected]>,9
.vscode,Joseph Hale <[email protected]>,16
.vscode/launch.json,Joseph Hale <[email protected]>,16
Makefile,Joseph Hale <[email protected]>,13
.pre-commit-config.yaml,Joseph Hale <[email protected]>,42
1 change: 0 additions & 1 deletion test/snapshots/test_e2e/test_full_workflow/authorship.json

This file was deleted.

4 changes: 2 additions & 2 deletions test/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def test_full_workflow(snapshot):
)
)

with open("build/authorship.json", "r") as f:
snapshot.assert_match(f.read(), "authorship.json")
with open("build/authorship.csv", "r") as f:
snapshot.assert_match(f.read(), "authorship.csv")

0 comments on commit 2c125ba

Please sign in to comment.