Skip to content

Commit cbd990d

Browse files
authored
Use Typer instead of argparse (#49)
Refactor codebase to swap CLI argument handling from argparse to Typer - Migrate settings into new settings.py dataclass - Migrate global arguments into animations.py - Move subcommand arguments into respective modules - Class name simplification - Assorted code reformating and cleanup
1 parent 77e54e9 commit cbd990d

18 files changed

+700
-647
lines changed

git_sim/__main__.py

Lines changed: 124 additions & 354 deletions
Large diffs are not rendered by default.

git_sim/animations.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import datetime
2+
import inspect
3+
import os
4+
import pathlib
5+
import subprocess
6+
import sys
7+
import time
8+
9+
import cv2
10+
import git.repo
11+
from manim import WHITE, Scene, config
12+
from manim.utils.file_ops import open_file
13+
14+
from git_sim.settings import Settings
15+
16+
17+
def handle_animations(scene: Scene) -> None:
18+
if sys.platform == "linux" or sys.platform == "darwin":
19+
repo_name = git.repo.Repo(
20+
search_parent_directories=True
21+
).working_tree_dir.split("/")[-1]
22+
elif sys.platform == "win32":
23+
repo_name = git.repo.Repo(
24+
search_parent_directories=True
25+
).working_tree_dir.split("\\")[-1]
26+
27+
config.media_dir = os.path.join(
28+
os.path.expanduser(Settings.media_dir), "git-sim_media"
29+
)
30+
config.verbosity = "ERROR"
31+
32+
# If the env variable is set and no argument provided, use the env variable value
33+
if os.getenv("git_sim_media_dir") and Settings.media_dir == ".":
34+
config.media_dir = os.path.join(
35+
os.path.expanduser(os.getenv("git_sim_media_dir")),
36+
"git-sim_media",
37+
repo_name,
38+
)
39+
40+
if Settings.low_quality:
41+
config.quality = "low_quality"
42+
43+
if Settings.light_mode:
44+
config.background_color = WHITE
45+
46+
t = datetime.datetime.fromtimestamp(time.time()).strftime("%m-%d-%y_%H-%M-%S")
47+
config.output_file = "git-sim-" + inspect.stack()[1].function + "_" + t + ".mp4"
48+
49+
scene.render()
50+
51+
if Settings.video_format == "webm":
52+
webm_file_path = str(scene.renderer.file_writer.movie_file_path)[:-3] + "webm"
53+
cmd = f"ffmpeg -y -i {scene.renderer.file_writer.movie_file_path} -hide_banner -loglevel error -c:v libvpx-vp9 -crf 50 -b:v 0 -b:a 128k -c:a libopus {webm_file_path}"
54+
print("Converting video output to .webm format...")
55+
# Start ffmpeg conversion
56+
p = subprocess.Popen(cmd, shell=True)
57+
p.wait()
58+
# if the conversion is successful, delete the .mp4
59+
if os.path.exists(webm_file_path):
60+
os.remove(scene.renderer.file_writer.movie_file_path)
61+
scene.renderer.file_writer.movie_file_path = webm_file_path
62+
63+
if not Settings.animate:
64+
video = cv2.VideoCapture(str(scene.renderer.file_writer.movie_file_path))
65+
success, image = video.read()
66+
if success:
67+
image_file_name = (
68+
"git-sim-"
69+
+ inspect.stack()[1].function
70+
+ "_"
71+
+ t
72+
+ "."
73+
+ Settings.img_format
74+
)
75+
image_file_path = os.path.join(
76+
os.path.join(config.media_dir, "images"), image_file_name
77+
)
78+
cv2.imwrite(image_file_path, image)
79+
print("Output image location:", image_file_path)
80+
else:
81+
print("Output video location:", scene.renderer.file_writer.movie_file_path)
82+
83+
if Settings.auto_open:
84+
try:
85+
if not Settings.animate:
86+
open_file(image_file_path)
87+
else:
88+
open_file(scene.renderer.file_writer.movie_file_path)
89+
except FileNotFoundError:
90+
print(
91+
"Error automatically opening media, please manually open the image or video file to view."
92+
)

git_sim/git_sim_add.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import sys
2-
from argparse import Namespace
32

43
import git
54
import manim as m
5+
import typer
66

7+
from git_sim.animations import handle_animations
78
from git_sim.git_sim_base_command import GitSimBaseCommand
9+
from git_sim.settings import Settings
810

911

10-
class GitSimAdd(GitSimBaseCommand):
11-
def __init__(self, args: Namespace):
12-
super().__init__(args=args)
13-
self.maxrefs = 2
12+
class Add(GitSimBaseCommand):
13+
def __init__(self, files: list[str]):
14+
super().__init__()
1415
self.hide_first_tag = True
1516
self.allow_no_commits = True
17+
self.files = files
1618

1719
try:
1820
self.selected_branches.append(self.repo.active_branch.name)
1921
except TypeError:
2022
pass
2123

22-
for name in self.args.name:
23-
if name not in [x.a_path for x in self.repo.index.diff(None)] + [
24+
for file in self.files:
25+
if file not in [x.a_path for x in self.repo.index.diff(None)] + [
2426
z for z in self.repo.untracked_files
2527
]:
26-
print("git-sim error: No modified file with name: '" + name + "'")
28+
print(f"git-sim error: No modified file with name: '{file}'")
2729
sys.exit()
2830

2931
def construct(self):
30-
print(
31-
"Simulating: git " + self.args.subcommand + " " + " ".join(self.args.name)
32-
)
32+
print(Settings.INFO_STRING + "add " + " ".join(self.files))
3333

3434
self.show_intro()
3535
self.get_commits()
@@ -49,12 +49,11 @@ def populate_zones(
4949
firstColumnArrowMap,
5050
secondColumnArrowMap,
5151
):
52-
5352
for x in self.repo.index.diff(None):
5453
if "git-sim_media" not in x.a_path:
5554
secondColumnFileNames.add(x.a_path)
56-
for name in self.args.name:
57-
if name == x.a_path:
55+
for file in self.files:
56+
if file == x.a_path:
5857
thirdColumnFileNames.add(x.a_path)
5958
secondColumnArrowMap[x.a_path] = m.Arrow(
6059
stroke_width=3, color=self.fontColor
@@ -71,9 +70,19 @@ def populate_zones(
7170
for z in self.repo.untracked_files:
7271
if "git-sim_media" not in z:
7372
firstColumnFileNames.add(z)
74-
for name in self.args.name:
75-
if name == z:
73+
for file in self.files:
74+
if file == z:
7675
thirdColumnFileNames.add(z)
7776
firstColumnArrowMap[z] = m.Arrow(
7877
stroke_width=3, color=self.fontColor
7978
)
79+
80+
81+
def add(
82+
files: list[str] = typer.Argument(
83+
default=None,
84+
help="The names of one or more files to add to Git's staging area",
85+
)
86+
):
87+
scene = Add(files=files)
88+
handle_animations(scene=scene)

0 commit comments

Comments
 (0)