Skip to content

Commit 855b7ae

Browse files
authored
Inherit pydantic in settings to handle env vars (#52)
- Add pydantic to BaseSettings - Remove obsolete env var handling
1 parent dfe3b83 commit 855b7ae

18 files changed

+170
-173
lines changed

git_sim/__main__.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,106 +21,106 @@
2121
import git_sim.stash
2222
import git_sim.status
2323
import git_sim.tag
24-
from git_sim.settings import ImgFormat, Settings, VideoFormat
24+
from git_sim.settings import ImgFormat, VideoFormat, settings
2525

2626
app = typer.Typer()
2727

2828

2929
@app.callback(no_args_is_help=True)
3030
def main(
3131
animate: bool = typer.Option(
32-
Settings.animate,
32+
settings.animate,
3333
help="Animate the simulation and output as an mp4 video",
3434
),
3535
auto_open: bool = typer.Option(
36-
Settings.auto_open,
36+
settings.auto_open,
3737
"--auto-open",
3838
" /-d",
3939
help="Enable / disable the automatic opening of the image/video file after generation",
4040
),
4141
img_format: ImgFormat = typer.Option(
42-
Settings.img_format,
42+
settings.img_format,
4343
help="Output format for the image files.",
4444
),
4545
light_mode: bool = typer.Option(
46-
Settings.light_mode,
46+
settings.light_mode,
4747
"--light-mode",
4848
help="Enable light-mode with white background",
4949
),
5050
logo: pathlib.Path = typer.Option(
51-
Settings.logo,
51+
settings.logo,
5252
help="The path to a custom logo to use in the animation intro/outro",
5353
),
5454
low_quality: bool = typer.Option(
55-
Settings.low_quality,
55+
settings.low_quality,
5656
"--low-quality",
5757
help="Render output video in low quality, useful for faster testing",
5858
),
5959
max_branches_per_commit: int = typer.Option(
60-
Settings.max_branches_per_commit,
60+
settings.max_branches_per_commit,
6161
help="Maximum number of branch labels to display for each commit",
6262
),
6363
max_tags_per_commit: int = typer.Option(
64-
Settings.max_tags_per_commit,
64+
settings.max_tags_per_commit,
6565
help="Maximum number of tags to display for each commit",
6666
),
6767
media_dir: pathlib.Path = typer.Option(
68-
Settings.media_dir,
68+
settings.media_dir,
6969
help="The path to output the animation data and video file",
7070
),
7171
outro_bottom_text: str = typer.Option(
72-
Settings.outro_bottom_text,
72+
settings.outro_bottom_text,
7373
help="Custom text to display below the logo during the outro",
7474
),
7575
outro_top_text: str = typer.Option(
76-
Settings.outro_top_text,
76+
settings.outro_top_text,
7777
help="Custom text to display above the logo during the outro",
7878
),
7979
reverse: bool = typer.Option(
80-
Settings.reverse,
80+
settings.reverse,
8181
"--reverse",
8282
"-r",
8383
help="Display commit history in the reverse direction",
8484
),
8585
show_intro: bool = typer.Option(
86-
Settings.show_intro,
86+
settings.show_intro,
8787
help="Add an intro sequence with custom logo and title",
8888
),
8989
show_outro: bool = typer.Option(
90-
Settings.show_outro,
90+
settings.show_outro,
9191
help="Add an outro sequence with custom logo and text",
9292
),
9393
speed: float = typer.Option(
94-
Settings.speed,
94+
settings.speed,
9595
help="A multiple of the standard 1x animation speed (ex: 2 = twice as fast, 0.5 = half as fast)",
9696
),
9797
title: str = typer.Option(
98-
Settings.title,
98+
settings.title,
9999
help="Custom title to display at the beginning of the animation",
100100
),
101101
video_format: VideoFormat = typer.Option(
102-
Settings.video_format.value,
102+
settings.video_format.value,
103103
help="Output format for the animation files.",
104104
case_sensitive=False,
105105
),
106106
):
107-
Settings.animate = animate
108-
Settings.auto_open = auto_open
109-
Settings.img_format = img_format
110-
Settings.light_mode = light_mode
111-
Settings.logo = logo
112-
Settings.low_quality = low_quality
113-
Settings.max_branches_per_commit = max_branches_per_commit
114-
Settings.max_tags_per_commit = max_tags_per_commit
115-
Settings.media_dir = media_dir
116-
Settings.outro_bottom_text = outro_bottom_text
117-
Settings.outro_top_text = outro_top_text
118-
Settings.reverse = reverse
119-
Settings.show_intro = show_intro
120-
Settings.show_outro = show_outro
121-
Settings.speed = speed
122-
Settings.title = title
123-
Settings.video_format = video_format
107+
settings.animate = animate
108+
settings.auto_open = auto_open
109+
settings.img_format = img_format
110+
settings.light_mode = light_mode
111+
settings.logo = logo
112+
settings.low_quality = low_quality
113+
settings.max_branches_per_commit = max_branches_per_commit
114+
settings.max_tags_per_commit = max_tags_per_commit
115+
settings.media_dir = media_dir
116+
settings.outro_bottom_text = outro_bottom_text
117+
settings.outro_top_text = outro_top_text
118+
settings.reverse = reverse
119+
settings.show_intro = show_intro
120+
settings.show_outro = show_outro
121+
settings.speed = speed
122+
settings.title = title
123+
settings.video_format = video_format
124124

125125

126126
app.command()(git_sim.add.add)

git_sim/add.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from git_sim.animations import handle_animations
88
from git_sim.git_sim_base_command import GitSimBaseCommand
9-
from git_sim.settings import Settings
9+
from git_sim.settings import settings
1010

1111

1212
class Add(GitSimBaseCommand):
@@ -30,7 +30,7 @@ def __init__(self, files: list[str]):
3030

3131
def construct(self):
3232
print(
33-
f"{Settings.INFO_STRING} {type(self).__name__.lower()} {' '.join(self.files)}"
33+
f"{settings.INFO_STRING} {type(self).__name__.lower()} {' '.join(self.files)}"
3434
)
3535

3636
self.show_intro()

git_sim/animations.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from manim import WHITE, Scene, config
1212
from manim.utils.file_ops import open_file
1313

14-
from git_sim.settings import Settings
14+
from git_sim.settings import settings
1515

1616

1717
def handle_animations(scene: Scene) -> None:
@@ -25,30 +25,22 @@ def handle_animations(scene: Scene) -> None:
2525
).working_tree_dir.split("\\")[-1]
2626

2727
config.media_dir = os.path.join(
28-
os.path.expanduser(Settings.media_dir), "git-sim_media"
28+
os.path.expanduser(settings.media_dir), "git-sim_media"
2929
)
3030
config.verbosity = "ERROR"
3131

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:
32+
if settings.low_quality:
4133
config.quality = "low_quality"
4234

43-
if Settings.light_mode:
35+
if settings.light_mode:
4436
config.background_color = WHITE
4537

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

4941
scene.render()
5042

51-
if Settings.video_format == "webm":
43+
if settings.video_format == "webm":
5244
webm_file_path = str(scene.renderer.file_writer.movie_file_path)[:-3] + "webm"
5345
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}"
5446
print("Converting video output to .webm format...")
@@ -60,7 +52,7 @@ def handle_animations(scene: Scene) -> None:
6052
os.remove(scene.renderer.file_writer.movie_file_path)
6153
scene.renderer.file_writer.movie_file_path = webm_file_path
6254

63-
if not Settings.animate:
55+
if not settings.animate:
6456
video = cv2.VideoCapture(str(scene.renderer.file_writer.movie_file_path))
6557
success, image = video.read()
6658
if success:
@@ -70,7 +62,7 @@ def handle_animations(scene: Scene) -> None:
7062
+ "_"
7163
+ t
7264
+ "."
73-
+ Settings.img_format
65+
+ settings.img_format
7466
)
7567
image_file_path = os.path.join(
7668
os.path.join(config.media_dir, "images"), image_file_name
@@ -80,9 +72,9 @@ def handle_animations(scene: Scene) -> None:
8072
else:
8173
print("Output video location:", scene.renderer.file_writer.movie_file_path)
8274

83-
if Settings.auto_open:
75+
if settings.auto_open:
8476
try:
85-
if not Settings.animate:
77+
if not settings.animate:
8678
open_file(image_file_path)
8779
else:
8880
open_file(scene.renderer.file_writer.movie_file_path)

git_sim/branch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from git_sim.animations import handle_animations
55
from git_sim.git_sim_base_command import GitSimBaseCommand
6-
from git_sim.settings import Settings
6+
from git_sim.settings import settings
77

88

99
class Branch(GitSimBaseCommand):
@@ -12,7 +12,7 @@ def __init__(self, name: str):
1212
self.name = name
1313

1414
def construct(self):
15-
print(f"{Settings.INFO_STRING} {type(self).__name__.lower()} {self.name}")
15+
print(f"{settings.INFO_STRING} {type(self).__name__.lower()} {self.name}")
1616

1717
self.show_intro()
1818
self.get_commits()
@@ -39,8 +39,8 @@ def construct(self):
3939

4040
fullbranch = m.VGroup(branchRec, branchText)
4141

42-
if Settings.animate:
43-
self.play(m.Create(fullbranch), run_time=1 / Settings.speed)
42+
if settings.animate:
43+
self.play(m.Create(fullbranch), run_time=1 / settings.speed)
4444
else:
4545
self.add(fullbranch)
4646

git_sim/cherrypick.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from git_sim.animations import handle_animations
88
from git_sim.git_sim_base_command import GitSimBaseCommand
9-
from git_sim.settings import Settings
9+
from git_sim.settings import settings
1010

1111

1212
class CherryPick(GitSimBaseCommand):
@@ -35,7 +35,7 @@ def __init__(self, commit: str, edit: str):
3535

3636
def construct(self):
3737
print(
38-
f"{Settings.INFO_STRING} {type(self).__name__.lower()} {self.commit}"
38+
f"{settings.INFO_STRING} {type(self).__name__.lower()} {self.commit}"
3939
+ ((' -e "' + self.edit + '"') if self.edit else "")
4040
)
4141

git_sim/commit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import git
44
import manim as m
55
import typer
6-
from git_sim.animations import handle_animations
7-
from git_sim.settings import Settings
86

7+
from git_sim.animations import handle_animations
98
from git_sim.git_sim_base_command import GitSimBaseCommand
9+
from git_sim.settings import settings
1010

1111

1212
class Commit(GitSimBaseCommand):
@@ -32,7 +32,7 @@ def __init__(self, message: str, amend: bool):
3232

3333
def construct(self):
3434
print(
35-
f"{Settings.INFO_STRING} {type(self).__name__.lower()} {'--amend ' if self.amend else ''}"
35+
f"{settings.INFO_STRING } {type(self).__name__.lower()} {'--amend ' if self.amend else ''}"
3636
+ '-m "'
3737
+ self.message
3838
+ '"'

0 commit comments

Comments
 (0)