Skip to content

Commit ac2bb32

Browse files
authored
Increase performance of typer autocompletion by breaking out subcommand functions into their own module
Remove autocompletion dependence on importing manim and git to improve performance Signed-off-by: Mathias Sven <mathiassven2@hotmail.com>
1 parent 6bb1b8d commit ac2bb32

22 files changed

+393
-364
lines changed

git_sim/__main__.py

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,10 @@
44
import sys
55
import datetime
66
import time
7-
import git
8-
9-
import git_sim.add
10-
import git_sim.branch
11-
import git_sim.cherrypick
12-
import git_sim.commit
13-
import git_sim.log
14-
import git_sim.merge
15-
import git_sim.rebase
16-
import git_sim.reset
17-
import git_sim.restore
18-
import git_sim.revert
19-
import git_sim.stash
20-
import git_sim.status
21-
import git_sim.tag
22-
import git_sim.switch
23-
import git_sim.checkout
24-
import git_sim.fetch
25-
import git_sim.pull
26-
import git_sim.push
27-
import git_sim.clone
7+
8+
import git_sim.commands
289

2910
from git_sim.settings import ImgFormat, VideoFormat, settings
30-
from manim import config, WHITE
3111

3212
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
3313

@@ -146,6 +126,9 @@ def main(
146126
help="Color commits by parameter, such as author",
147127
),
148128
):
129+
import git
130+
from manim import config, WHITE
131+
149132
settings.animate = animate
150133
settings.n = n
151134
settings.auto_open = auto_open
@@ -199,25 +182,25 @@ def main(
199182
config.output_file = "git-sim-" + ctx.invoked_subcommand + "_" + t + ".mp4"
200183

201184

202-
app.command()(git_sim.add.add)
203-
app.command()(git_sim.branch.branch)
204-
app.command()(git_sim.cherrypick.cherry_pick)
205-
app.command()(git_sim.commit.commit)
206-
app.command()(git_sim.log.log)
207-
app.command()(git_sim.merge.merge)
208-
app.command()(git_sim.rebase.rebase)
209-
app.command()(git_sim.reset.reset)
210-
app.command()(git_sim.restore.restore)
211-
app.command()(git_sim.revert.revert)
212-
app.command()(git_sim.stash.stash)
213-
app.command()(git_sim.status.status)
214-
app.command()(git_sim.tag.tag)
215-
app.command()(git_sim.switch.switch)
216-
app.command()(git_sim.checkout.checkout)
217-
app.command()(git_sim.fetch.fetch)
218-
app.command()(git_sim.pull.pull)
219-
app.command()(git_sim.push.push)
220-
app.command()(git_sim.clone.clone)
185+
app.command()(git_sim.commands.add)
186+
app.command()(git_sim.commands.branch)
187+
app.command()(git_sim.commands.checkout)
188+
app.command()(git_sim.commands.cherry_pick)
189+
app.command()(git_sim.commands.clone)
190+
app.command()(git_sim.commands.commit)
191+
app.command()(git_sim.commands.fetch)
192+
app.command()(git_sim.commands.log)
193+
app.command()(git_sim.commands.merge)
194+
app.command()(git_sim.commands.pull)
195+
app.command()(git_sim.commands.push)
196+
app.command()(git_sim.commands.rebase)
197+
app.command()(git_sim.commands.reset)
198+
app.command()(git_sim.commands.restore)
199+
app.command()(git_sim.commands.revert)
200+
app.command()(git_sim.commands.stash)
201+
app.command()(git_sim.commands.status)
202+
app.command()(git_sim.commands.switch)
203+
app.command()(git_sim.commands.tag)
221204

222205

223206
if __name__ == "__main__":

git_sim/add.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import sys
22
import git
33
import manim as m
4-
import typer
54

65
from typing import List
76

8-
from git_sim.animations import handle_animations
97
from git_sim.git_sim_base_command import GitSimBaseCommand
108
from git_sim.settings import settings
119

@@ -82,14 +80,3 @@ def populate_zones(
8280
firstColumnArrowMap[z] = m.Arrow(
8381
stroke_width=3, color=self.fontColor
8482
)
85-
86-
87-
def add(
88-
files: List[str] = typer.Argument(
89-
default=None,
90-
help="The names of one or more files to add to Git's staging area",
91-
)
92-
):
93-
settings.hide_first_tag = True
94-
scene = Add(files=files)
95-
handle_animations(scene=scene)

git_sim/branch.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import manim as m
2-
import typer
32

4-
from git_sim.animations import handle_animations
53
from git_sim.git_sim_base_command import GitSimBaseCommand
64
from git_sim.settings import settings
75

@@ -52,13 +50,3 @@ def construct(self):
5250
self.color_by()
5351
self.fadeout()
5452
self.show_outro()
55-
56-
57-
def branch(
58-
name: str = typer.Argument(
59-
...,
60-
help="The name of the new branch",
61-
)
62-
):
63-
scene = Branch(name=name)
64-
handle_animations(scene=scene)

git_sim/checkout.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import git
55
import manim as m
66
import numpy
7-
import typer
87

9-
from git_sim.animations import handle_animations
108
from git_sim.git_sim_base_command import GitSimBaseCommand
119
from git_sim.settings import settings
1210

@@ -19,7 +17,11 @@ def __init__(self, branch: str, b: bool):
1917

2018
if self.b:
2119
if self.branch in self.repo.heads:
22-
print("git-sim error: can't create new branch '" + self.branch + "', it already exists")
20+
print(
21+
"git-sim error: can't create new branch '"
22+
+ self.branch
23+
+ "', it already exists"
24+
)
2325
sys.exit(1)
2426
else:
2527
try:
@@ -75,7 +77,7 @@ def construct(self):
7577
branch_commit = self.get_commit(self.branch)
7678

7779
if self.is_ancestor:
78-
commits_in_range = list(self.repo.iter_commits(self.branch + '..HEAD'))
80+
commits_in_range = list(self.repo.iter_commits(self.branch + "..HEAD"))
7981

8082
# branch is reached from HEAD, so draw everything
8183
if len(commits_in_range) <= self.n:
@@ -113,18 +115,3 @@ def construct(self):
113115
self.color_by()
114116
self.fadeout()
115117
self.show_outro()
116-
117-
118-
def checkout(
119-
branch: str = typer.Argument(
120-
...,
121-
help="The name of the branch to checkout",
122-
),
123-
b: bool = typer.Option(
124-
False,
125-
"-b",
126-
help="Create the specified branch if it doesn't already exist",
127-
),
128-
):
129-
scene = Checkout(branch=branch, b=b)
130-
handle_animations(scene=scene)

git_sim/cherrypick.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import git
44
import manim as m
5-
import typer
65

7-
from git_sim.animations import handle_animations
86
from git_sim.git_sim_base_command import GitSimBaseCommand
97
from git_sim.settings import settings
108

@@ -70,19 +68,3 @@ def construct(self):
7068
self.color_by(offset=2)
7169
self.fadeout()
7270
self.show_outro()
73-
74-
75-
def cherry_pick(
76-
commit: str = typer.Argument(
77-
...,
78-
help="The ref (branch/tag), or commit ID to simulate cherry-pick onto active branch",
79-
),
80-
edit: str = typer.Option(
81-
None,
82-
"--edit",
83-
"-e",
84-
help="Specify a new commit message for the cherry-picked commit",
85-
),
86-
):
87-
scene = CherryPick(commit=commit, edit=edit)
88-
handle_animations(scene=scene)

git_sim/clone.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
import git
66
import manim as m
77
import numpy
8-
import typer
98
import tempfile
109
import shutil
1110
import stat
1211
import re
1312

14-
from git_sim.animations import handle_animations
1513
from git_sim.git_sim_base_command import GitSimBaseCommand
1614
from git_sim.settings import settings
1715

@@ -28,28 +26,30 @@ def __init__(self, url: str):
2826

2927
def construct(self):
3028
if not settings.stdout and not settings.output_only_path and not settings.quiet:
31-
print(
32-
f"{settings.INFO_STRING } {type(self).__name__.lower()} {self.url}"
33-
)
29+
print(f"{settings.INFO_STRING } {type(self).__name__.lower()} {self.url}")
3430

3531
self.show_intro()
36-
32+
3733
# Configure paths to make local clone to run networked commands in
3834
repo_name = re.search(r"/([^/]+)/?$", self.url)
3935
if repo_name:
4036
repo_name = repo_name.group(1)
4137
if repo_name.endswith(".git"):
4238
repo_name = repo_name[:-4]
4339
else:
44-
print(f"git-sim error: Invalid repo URL, please confirm repo URL and try again")
40+
print(
41+
f"git-sim error: Invalid repo URL, please confirm repo URL and try again"
42+
)
4543
sys.exit(1)
4644
new_dir = os.path.join(tempfile.gettempdir(), "git_sim", repo_name)
4745

4846
# Create local clone of local repo
4947
try:
5048
self.repo = git.Repo.clone_from(self.url, new_dir, no_hardlinks=True)
5149
except git.GitCommandError as e:
52-
print(f"git-sim error: Invalid repo URL, please confirm repo URL and try again")
50+
print(
51+
f"git-sim error: Invalid repo URL, please confirm repo URL and try again"
52+
)
5353
sys.exit(1)
5454

5555
head_commit = self.get_commit()
@@ -69,7 +69,7 @@ def construct(self):
6969

7070
def add_details(self, repo_name):
7171
text1 = m.Text(
72-
f"Successfully cloned from {self.url} into ./{repo_name}",
72+
f"Successfully cloned from {self.url} into ./{repo_name}",
7373
font="Monospace",
7474
font_size=20,
7575
color=self.fontColor,
@@ -100,12 +100,3 @@ def add_details(self, repo_name):
100100
def del_rw(action, name, exc):
101101
os.chmod(name, stat.S_IWRITE)
102102
os.remove(name)
103-
104-
def clone(
105-
url: str = typer.Argument(
106-
...,
107-
help="The web URL or filesystem path of the Git repo to clone",
108-
),
109-
):
110-
scene = Clone(url=url)
111-
handle_animations(scene=scene)

0 commit comments

Comments
 (0)