11import argparse
22import logging
33import os
4+ from pathlib import Path
5+
6+ from zimscraperlib .constants import (
7+ MAXIMUM_DESCRIPTION_METADATA_LENGTH ,
8+ MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH ,
9+ RECOMMENDED_MAX_TITLE_LENGTH ,
10+ )
411
512from libretexts2zim .client import LibreTextsClient
613from libretexts2zim .constants import (
714 NAME ,
815 VERSION ,
916 logger ,
1017)
11- from libretexts2zim .generator import ContentFilter , Generator
18+ from libretexts2zim .processor import ContentFilter , Processor
1219from libretexts2zim .zimconfig import ZimConfig
1320
1421
@@ -28,13 +35,111 @@ def zim_defaults() -> ZimConfig:
2835 )
2936
3037
31- def main () -> None :
32- parser = argparse .ArgumentParser (
33- prog = NAME ,
38+ def add_zim_config_flags (parser : argparse .ArgumentParser , defaults : "ZimConfig" ):
39+ """
40+ Adds flags related to zim configuration
41+
42+ Flags are added to the given parser with given defaults.
43+ """
44+
45+ parser .add_argument (
46+ "--library-name" ,
47+ help = "Display name for the library, e.g. Geosciences" ,
48+ required = True ,
3449 )
3550
3651 parser .add_argument (
37- "--debug" , help = "Enable verbose output" , action = "store_true" , default = False
52+ "--creator" ,
53+ help = f"Name of content creator. Default: { defaults .creator !r} " ,
54+ default = defaults .creator ,
55+ )
56+
57+ parser .add_argument (
58+ "--publisher" ,
59+ help = f"Custom publisher name. Default: { defaults .publisher !r} " ,
60+ default = defaults .publisher ,
61+ )
62+
63+ parser .add_argument (
64+ "--file-name-format" ,
65+ help = "Custom file name format for individual ZIMs. "
66+ f"Default: { defaults .file_name_format !r} " ,
67+ default = defaults .file_name_format ,
68+ metavar = "FORMAT" ,
69+ )
70+
71+ parser .add_argument (
72+ "--name-format" ,
73+ help = "Custom name format for individual ZIMs. "
74+ f"Default: { defaults .name_format !r} " ,
75+ default = defaults .name_format ,
76+ metavar = "FORMAT" ,
77+ )
78+
79+ parser .add_argument (
80+ "--title-format" ,
81+ help = f"Custom title format for individual ZIMs. Final value must not be "
82+ f"longer than { RECOMMENDED_MAX_TITLE_LENGTH } chars. "
83+ f"Default: { defaults .title_format !r} " ,
84+ default = defaults .title_format ,
85+ metavar = "FORMAT" ,
86+ )
87+
88+ parser .add_argument (
89+ "--description-format" ,
90+ help = "Custom description format for individual ZIMs. Final value must not "
91+ f"be longer than { MAXIMUM_DESCRIPTION_METADATA_LENGTH } chars. "
92+ f"Default: { defaults .title_format !r} " ,
93+ default = defaults .description_format ,
94+ metavar = "FORMAT" ,
95+ )
96+
97+ parser .add_argument (
98+ "--long-description-format" ,
99+ help = "Custom long description format for your ZIM. Final value must not be "
100+ f"longer than { MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH } chars. "
101+ f"Default: { defaults .long_description_format !r} " ,
102+ default = defaults .long_description_format ,
103+ metavar = "FORMAT" ,
104+ )
105+
106+ # Due to https://github.com/python/cpython/issues/60603 defaulting an array in
107+ # argparse doesn't work so we expose the underlying semicolon delimited string.
108+ parser .add_argument (
109+ "--tags" ,
110+ help = "A semicolon (;) delimited list of tags to add to the ZIM."
111+ "Formatting is supported. "
112+ f"Default: { defaults .tags !r} " ,
113+ default = defaults .tags ,
114+ )
115+
116+ parser .add_argument (
117+ "--secondary-color" ,
118+ help = "Secondary (background) color of ZIM UI. Default: "
119+ f"{ defaults .secondary_color !r} " ,
120+ default = defaults .secondary_color ,
121+ )
122+
123+
124+ def add_content_filter_flags (parser : argparse .ArgumentParser ):
125+ """Adds flags related to content filtering to the given parser."""
126+
127+ parser .add_argument (
128+ "--shelves-include" ,
129+ help = "Includes only shelves matching the given regular expression." ,
130+ metavar = "REGEX" ,
131+ )
132+
133+ parser .add_argument (
134+ "--shelves-exclude" ,
135+ help = "Excludes shelves matching the given regular expression." ,
136+ metavar = "REGEX" ,
137+ )
138+
139+
140+ def main () -> None :
141+ parser = argparse .ArgumentParser (
142+ prog = NAME ,
38143 )
39144
40145 parser .add_argument (
@@ -44,39 +149,41 @@ def main() -> None:
44149 version = VERSION ,
45150 )
46151
152+ # Client configuration flags
153+ parser .add_argument (
154+ "--library-slug" ,
155+ help = "URL prefix for the library, e.g. for Geosciences which is at "
156+ "https://geo.libretexts.org/, the slug is `geo`" ,
157+ required = True ,
158+ )
159+
160+ # ZIM configuration flags
161+ add_zim_config_flags (parser , zim_defaults ())
162+
163+ # Document selection flags
164+ add_content_filter_flags (parser )
165+
47166 parser .add_argument (
48167 "--output" ,
49168 help = "Output folder for ZIMs. Default: /output" ,
50169 default = "/output" ,
51170 dest = "output_folder" ,
52171 )
53172
173+ parser .add_argument (
174+ "--debug" , help = "Enable verbose output" , action = "store_true" , default = False
175+ )
176+
54177 parser .add_argument (
55178 "--zimui-dist" ,
56179 type = str ,
57180 help = (
58- "Directory containing Vite build output from the Zim UI Vue.JS application"
181+ "Dev option to customize directory containing Vite build output from the "
182+ "ZIM UI Vue.JS application"
59183 ),
60184 default = os .getenv ("LIBRETEXTS_ZIMUI_DIST" , "../zimui/dist" ),
61185 )
62186
63- # ZIM configuration flags
64- ZimConfig .add_flags (
65- parser ,
66- zim_defaults (),
67- )
68-
69- # Document selection flags
70- ContentFilter .add_flags (parser )
71-
72- # Client configuration flags
73- parser .add_argument (
74- "--library-slug" ,
75- help = "URL prefix for the course, e.g. for Geosciences which is at "
76- "https://geo.libretexts.org/, the slug is `geo`" ,
77- required = True ,
78- )
79-
80187 args = parser .parse_args ()
81188
82189 logger .setLevel (level = logging .DEBUG if args .debug else logging .INFO )
@@ -88,17 +195,20 @@ def main() -> None:
88195 library_slug = args .library_slug ,
89196 )
90197
91- Generator (
198+ Processor (
92199 libretexts_client = libretexts_client ,
93200 zim_config = zim_config ,
94- output_folder = args .output_folder ,
95- zimui_dist = args .zimui_dist ,
201+ output_folder = Path ( args .output_folder ) ,
202+ zimui_dist = Path ( args .zimui_dist ) ,
96203 content_filter = doc_filter ,
97204 ).run ()
98- except Exception as e :
99- logger .exception (e )
100- logger .error (f"Generation failed with the following error: { e } " )
101- raise SystemExit (1 ) from e
205+ except SystemExit :
206+ logger .error ("Generation failed, exiting" )
207+ raise
208+ except Exception as exc :
209+ logger .exception (exc )
210+ logger .error (f"Generation failed with the following error: { exc } " )
211+ raise SystemExit (1 ) from exc
102212
103213
104214if __name__ == "__main__" :
0 commit comments