|
4 | 4 | import sys |
5 | 5 | import types |
6 | 6 |
|
| 7 | +from .utils.tex import * |
7 | 8 | from . import constants |
8 | 9 | from . import dirs |
9 | 10 | from .logger import logger |
10 | 11 |
|
11 | | -__all__ = ["parse_cli", "get_configuration", "initialize_directories"] |
| 12 | +__all__ = ["parse_cli", "get_configuration", "initialize_directories","register_tex_template","initialize_tex"] |
12 | 13 |
|
13 | 14 |
|
14 | 15 | def parse_cli(): |
@@ -140,7 +141,12 @@ def parse_cli(): |
140 | 141 | "--text_dir", |
141 | 142 | help="Directory to write text", |
142 | 143 | ) |
| 144 | + parser.add_argument( |
| 145 | + "--tex_template", |
| 146 | + help="Specify a custom TeX template file", |
| 147 | + ) |
143 | 148 | return parser.parse_args() |
| 149 | + |
144 | 150 | except argparse.ArgumentError as err: |
145 | 151 | logger.error(str(err)) |
146 | 152 | sys.exit(2) |
@@ -176,6 +182,7 @@ def get_configuration(args): |
176 | 182 | "video_dir": args.video_dir, |
177 | 183 | "tex_dir": args.tex_dir, |
178 | 184 | "text_dir": args.text_dir, |
| 185 | + "tex_template": args.tex_template, |
179 | 186 | } |
180 | 187 |
|
181 | 188 | # Camera configuration |
@@ -281,3 +288,39 @@ def initialize_directories(config): |
281 | 288 | dirs.VIDEO_DIR = dir_config["video_dir"] |
282 | 289 | dirs.TEX_DIR = dir_config["tex_dir"] |
283 | 290 | dirs.TEXT_DIR = dir_config["text_dir"] |
| 291 | + |
| 292 | +def register_tex_template(tpl): |
| 293 | + """Register the given LaTeX template for later use. |
| 294 | +
|
| 295 | + Parameters |
| 296 | + ---------- |
| 297 | + tpl : :class:`~.TexTemplate` |
| 298 | + The LaTeX template to register. |
| 299 | + """ |
| 300 | + constants.TEX_TEMPLATE = tpl |
| 301 | + |
| 302 | +def initialize_tex(config): |
| 303 | + """Safely create a LaTeX template object from a file. |
| 304 | + If file is not readable, the default template file is used. |
| 305 | +
|
| 306 | + Parameters |
| 307 | + ---------- |
| 308 | + filename : :class:`str` |
| 309 | + The name of the file with the LaTeX template. |
| 310 | + """ |
| 311 | + filename="" |
| 312 | + if config["tex_template"]: |
| 313 | + filename = os.path.expanduser(config["tex_template"]) |
| 314 | + if filename and not os.access(filename, os.R_OK): |
| 315 | + # custom template not available, fallback to default |
| 316 | + logger.warning( |
| 317 | + f"Custom TeX template {filename} not found or not readable. " |
| 318 | + "Falling back to the default template." |
| 319 | + ) |
| 320 | + filename = "" |
| 321 | + if filename: |
| 322 | + # still having a filename -> use the file |
| 323 | + constants.TEX_TEMPLATE = TexTemplateFromFile(filename=filename) |
| 324 | + else: |
| 325 | + # use the default template |
| 326 | + constants.TEX_TEMPLATE = TexTemplate() |
0 commit comments