Skip to content

Commit 31b2c8b

Browse files
committed
Bump version to 1.1.1; fix path resolution for font registration and improve file checks
1 parent cb8256c commit 31b2c8b

5 files changed

Lines changed: 47 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.1] - 2024-11-23
9+
10+
- Fix path resolution for font registration.
11+
812
## [1.1.0] - 2024-11-23
913

1014
- Fix dependency conflicts with `MoviePy 2.0.0`

captametropolis/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
from .errors import UtilityNotFoundError
1212
from .text_drawer import Word, create_shadow, create_text_ex, get_text_size_ex
1313
from .utils import (
14-
is_local_transcription_available,
1514
_get_font_path,
1615
ffmpeg_binary,
1716
ffmpeg_installed,
1817
imagemagick_binary,
18+
is_local_transcription_available,
1919
)
2020

2121
__all__ = [
2222
"__version__",
2323
"add_captions",
2424
"ffmpeg_installed",
2525
"imagemagick_binary",
26+
"is_local_transcription_available",
2627
]
2728

2829
lines_cache = {}

captametropolis/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.0"
1+
__version__ = "1.1.1"

captametropolis/utils.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<!ATTLIST include file CDATA #REQUIRED>
3030
]>
3131
"""
32-
_STANDARD_FONTS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "assets", "fonts")
32+
_STANDARD_FONTS_DIR = os.path.join(
33+
os.path.abspath(os.path.dirname(__file__)), "assets", "fonts"
34+
)
35+
3336

3437
def is_admin():
3538
if os.name == "nt":
@@ -54,19 +57,29 @@ def require_admin(quiet_run: bool = False, verbose: bool = False):
5457
command = [sys.executable] + sys.argv
5558
else:
5659
command = sys.argv
60+
command[0] = os.path.abspath(os.path.normpath(command[0]))
61+
if os.name == "nt" and command[0].startswith("\\\\?\\"):
62+
command[0] = command[0].replace("\\\\?\\", "")
63+
64+
if verbose:
65+
print(command)
5766

5867
if os.name == "posix":
5968
os.execvp("sudo", ["sudo"] + command)
6069
else:
6170
params = " ".join(command[1:])
62-
ctypes.windll.shell32.ShellExecuteW(
71+
result = ctypes.windll.shell32.ShellExecuteW(
6372
None,
6473
"runas",
6574
command[0],
6675
params,
6776
None,
6877
0 if quiet_run else 1,
6978
)
79+
if result <= 32:
80+
if result == 5:
81+
print("ERROR: Access is denied.")
82+
raise ctypes.WinError(result)
7083
sys.exit(0)
7184
else:
7285
if verbose:
@@ -152,7 +165,7 @@ def try_cmd(cmd):
152165
"magick.exe" if "ImageMagick-7" in imgmgck_directory else "convert.exe"
153166
)
154167
imgmgck_binary = os.path.join(imgmgck_directory, imgmgck_executable)
155-
if os.path.exists(imgmgck_binary):
168+
if os.path.isfile(imgmgck_binary):
156169
return imgmgck_binary
157170
else:
158171
return "unset"
@@ -206,13 +219,13 @@ def _get_font_info(font_path: str) -> dict:
206219

207220

208221
def register_font(fontpath: str, quiet_run: bool = False, verbose: bool = False) -> str:
209-
if not os.path.exists(fontpath):
210-
fontpath = os.path.join(_STANDARD_FONTS_DIR, fontpath)
211-
if not os.path.exists(fontpath):
222+
if not os.path.isfile(fontpath):
223+
fontpath = os.path.join(_STANDARD_FONTS_DIR, os.path.basename(fontpath))
224+
if not os.path.isfile(fontpath):
212225
raise FileNotFoundError(f"Font file not found: {fontpath}")
213226

214227
font_container = os.path.join(imagemagick_directory(), "type-ghostscript.xml")
215-
if not os.path.exists(font_container):
228+
if not os.path.isfile(font_container):
216229
raise FileNotFoundError("Font container not found")
217230

218231
require_admin(quiet_run, verbose)
@@ -250,11 +263,17 @@ def register_font(fontpath: str, quiet_run: bool = False, verbose: bool = False)
250263

251264
def is_font_registered(font_path_or_name: str) -> str | None:
252265
font_container = os.path.join(imagemagick_directory(), "type-ghostscript.xml")
253-
if not os.path.exists(font_container):
266+
if not os.path.isfile(font_container):
254267
raise FileNotFoundError("Font container not found")
255268

256-
if os.path.exists(font_path_or_name):
269+
if os.path.isfile(font_path_or_name):
257270
font_name = _get_font_info(font_path_or_name)["Full Font Name"]
271+
elif os.path.isfile(
272+
os.path.join(_STANDARD_FONTS_DIR, os.path.basename(font_path_or_name))
273+
):
274+
font_name = _get_font_info(
275+
os.path.join(_STANDARD_FONTS_DIR, os.path.basename(font_path_or_name))
276+
)["Full Font Name"]
258277
else:
259278
font_name = font_path_or_name.removesuffix(".ttf")
260279

@@ -272,16 +291,16 @@ def _get_font_path(font: str) -> tuple[str, str]:
272291
if not font.endswith(".ttf"):
273292
raise ValueError("Only TrueType fonts are currently supported")
274293

275-
if os.path.exists(font):
294+
if os.path.isfile(font):
276295
injected_font_name = is_font_registered(font)
277296
if injected_font_name:
278297
return os.path.abspath(font), injected_font_name
279298

280299
raise FontNotRegisteredError(font)
281300

282-
font = os.path.join(_STANDARD_FONTS_DIR, font)
301+
font = os.path.join(_STANDARD_FONTS_DIR, os.path.basename(font))
283302

284-
if not os.path.exists(font):
303+
if not os.path.isfile(font):
285304
raise FileNotFoundError(f"Font '{font}' not found")
286305

287306
injected_font_name = is_font_registered(font)
@@ -295,13 +314,19 @@ def unregister_font(
295314
font_path_or_name: str, quiet_run: bool = False, verbose: bool = False
296315
) -> bool:
297316
font_container = os.path.join(imagemagick_directory(), "type-ghostscript.xml")
298-
if not os.path.exists(font_container):
317+
if not os.path.isfile(font_container):
299318
raise FileNotFoundError("Font container not found")
300319

301320
require_admin(quiet_run, verbose)
302321

303-
if os.path.exists(font_path_or_name):
322+
if os.path.isfile(font_path_or_name):
304323
font_name = _get_font_info(font_path_or_name)["Full Font Name"]
324+
elif os.path.isfile(
325+
os.path.join(_STANDARD_FONTS_DIR, os.path.basename(font_path_or_name))
326+
):
327+
font_name = _get_font_info(
328+
os.path.join(_STANDARD_FONTS_DIR, os.path.basename(font_path_or_name))
329+
)["Full Font Name"]
305330
else:
306331
font_name = font_path_or_name.removesuffix(".ttf")
307332

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_version():
4141
maintainer_email="contact@appsolves.dev",
4242
author_email="unconventionalcoding@gmail.com",
4343
description="Add Automatic Captions to YouTube Shorts with AI",
44-
long_description=open("README.md").read(),
44+
long_description=open("README.md", "r", encoding="utf-8").read(),
4545
long_description_content_type="text/markdown",
4646
entry_points={
4747
"console_scripts": [

0 commit comments

Comments
 (0)