Skip to content

Use Typer instead of argparse #49

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

Merged
merged 27 commits into from
Feb 9, 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
3f19d5a
first draft for typer commands
paketb0te Jan 27, 2023
2d54992
make each command a separate typer app
paketb0te Feb 1, 2023
3abea68
remove implemented options from TODO comment
paketb0te Feb 1, 2023
c2c595e
add logo, show_intro, show_outro, media_dir
paketb0te Feb 3, 2023
0794f68
finish top-level options
paketb0te Feb 7, 2023
d6a06af
make log a command instead of a typr app
paketb0te Feb 7, 2023
ed313b0
move print statement into GitSimBaseCommand
paketb0te Feb 7, 2023
7ed7a0b
remove dupplicate setting "logo"
paketb0te Feb 7, 2023
c9cb296
make status a command instead of a typr app
paketb0te Feb 7, 2023
8c4c940
Revert "move print statement into GitSimBaseCommand"
paketb0te Feb 7, 2023
295865a
add "add"
paketb0te Feb 7, 2023
e4c7ce0
add "restore"
paketb0te Feb 7, 2023
a6bdff0
add "commit"
paketb0te Feb 7, 2023
e2b740f
add "stash"
paketb0te Feb 7, 2023
6e94570
add "branch"
paketb0te Feb 7, 2023
3d089c2
add "tag"
paketb0te Feb 7, 2023
c066631
update class name for branch
paketb0te Feb 7, 2023
0f95e59
add "reset"
paketb0te Feb 7, 2023
d405db3
rebase onto v0.2.2
paketb0te Feb 7, 2023
4fdce76
add "revert"
paketb0te Feb 8, 2023
6897614
add "merge"
paketb0te Feb 8, 2023
4d65620
add "rebase"
paketb0te Feb 8, 2023
49efb82
add "cherrypick"
paketb0te Feb 8, 2023
c52da38
clean up __main__.py and animations.py
paketb0te Feb 8, 2023
0b2db4c
remove obsolete self.maxrefs
paketb0te Feb 9, 2023
8a7c547
move all changes back into the original files
paketb0te Feb 9, 2023
7a747b6
update deps in setup.py
paketb0te Feb 9, 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
478 changes: 124 additions & 354 deletions git_sim/__main__.py

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions git_sim/animations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import datetime
import inspect
import os
import pathlib
import subprocess
import sys
import time

import cv2
import git.repo
from manim import WHITE, Scene, config
from manim.utils.file_ops import open_file

from git_sim.settings import Settings


def handle_animations(scene: Scene) -> None:
if sys.platform == "linux" or sys.platform == "darwin":
repo_name = git.repo.Repo(
search_parent_directories=True
).working_tree_dir.split("/")[-1]
elif sys.platform == "win32":
repo_name = git.repo.Repo(
search_parent_directories=True
).working_tree_dir.split("\\")[-1]

config.media_dir = os.path.join(
os.path.expanduser(Settings.media_dir), "git-sim_media"
)
config.verbosity = "ERROR"

# If the env variable is set and no argument provided, use the env variable value
if os.getenv("git_sim_media_dir") and Settings.media_dir == ".":
config.media_dir = os.path.join(
os.path.expanduser(os.getenv("git_sim_media_dir")),
"git-sim_media",
repo_name,
)

if Settings.low_quality:
config.quality = "low_quality"

if Settings.light_mode:
config.background_color = WHITE

t = datetime.datetime.fromtimestamp(time.time()).strftime("%m-%d-%y_%H-%M-%S")
config.output_file = "git-sim-" + inspect.stack()[1].function + "_" + t + ".mp4"

scene.render()

if Settings.video_format == "webm":
webm_file_path = str(scene.renderer.file_writer.movie_file_path)[:-3] + "webm"
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}"
print("Converting video output to .webm format...")
# Start ffmpeg conversion
p = subprocess.Popen(cmd, shell=True)
p.wait()
# if the conversion is successful, delete the .mp4
if os.path.exists(webm_file_path):
os.remove(scene.renderer.file_writer.movie_file_path)
scene.renderer.file_writer.movie_file_path = webm_file_path

if not Settings.animate:
video = cv2.VideoCapture(str(scene.renderer.file_writer.movie_file_path))
success, image = video.read()
if success:
image_file_name = (
"git-sim-"
+ inspect.stack()[1].function
+ "_"
+ t
+ "."
+ Settings.img_format
)
image_file_path = os.path.join(
os.path.join(config.media_dir, "images"), image_file_name
)
cv2.imwrite(image_file_path, image)
print("Output image location:", image_file_path)
else:
print("Output video location:", scene.renderer.file_writer.movie_file_path)

if Settings.auto_open:
try:
if not Settings.animate:
open_file(image_file_path)
else:
open_file(scene.renderer.file_writer.movie_file_path)
except FileNotFoundError:
print(
"Error automatically opening media, please manually open the image or video file to view."
)
41 changes: 25 additions & 16 deletions git_sim/git_sim_add.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import sys
from argparse import Namespace

import git
import manim as m
import typer

from git_sim.animations import handle_animations
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import Settings


class GitSimAdd(GitSimBaseCommand):
def __init__(self, args: Namespace):
super().__init__(args=args)
self.maxrefs = 2
class Add(GitSimBaseCommand):
def __init__(self, files: list[str]):
super().__init__()
self.hide_first_tag = True
self.allow_no_commits = True
self.files = files

try:
self.selected_branches.append(self.repo.active_branch.name)
except TypeError:
pass

for name in self.args.name:
if name not in [x.a_path for x in self.repo.index.diff(None)] + [
for file in self.files:
if file not in [x.a_path for x in self.repo.index.diff(None)] + [
z for z in self.repo.untracked_files
]:
print("git-sim error: No modified file with name: '" + name + "'")
print(f"git-sim error: No modified file with name: '{file}'")
sys.exit()

def construct(self):
print(
"Simulating: git " + self.args.subcommand + " " + " ".join(self.args.name)
)
print(Settings.INFO_STRING + "add " + " ".join(self.files))

self.show_intro()
self.get_commits()
Expand All @@ -49,12 +49,11 @@ def populate_zones(
firstColumnArrowMap,
secondColumnArrowMap,
):

for x in self.repo.index.diff(None):
if "git-sim_media" not in x.a_path:
secondColumnFileNames.add(x.a_path)
for name in self.args.name:
if name == x.a_path:
for file in self.files:
if file == x.a_path:
thirdColumnFileNames.add(x.a_path)
secondColumnArrowMap[x.a_path] = m.Arrow(
stroke_width=3, color=self.fontColor
Expand All @@ -71,9 +70,19 @@ def populate_zones(
for z in self.repo.untracked_files:
if "git-sim_media" not in z:
firstColumnFileNames.add(z)
for name in self.args.name:
if name == z:
for file in self.files:
if file == z:
thirdColumnFileNames.add(z)
firstColumnArrowMap[z] = m.Arrow(
stroke_width=3, color=self.fontColor
)


def add(
files: list[str] = typer.Argument(
default=None,
help="The names of one or more files to add to Git's staging area",
)
):
scene = Add(files=files)
handle_animations(scene=scene)
Loading