@@ -65,63 +65,79 @@ def generate_notebook_contents(logger: logging.Logger):
65
65
os .makedirs (CONTENTS_DIR )
66
66
with open (CONTENTS_NB , 'w' , encoding = 'utf8' ) as f :
67
67
f .write ('%-*- coding: utf-8 -*-\n ' )
68
- chapters = file_preprocess (CONFIG .get_chapter_key (),
69
- scandir_dir_merge ( CONFIG .get_code_dir (), CONFIG .get_doc_dir ()), logger )
68
+ chapters = file_preprocess (CONFIG .get_chapter_key (), scandir_dir_merge (
69
+ CONFIG .get_code_dir (), CONFIG .get_doc_dir ()), logger )
70
70
logger .info (rf"{ len (chapters )} chapter(s) found" )
71
71
logger .debug ('Which are:\n \t ' + '\n \t ' .join (chapters ))
72
72
logger .debug ('Will include in listed order' )
73
73
74
74
for chapter in chapters :
75
- f .writelines (latex_chapter (NameLaTeX (CONFIG .get_chapter_title (chapter ))))
75
+ f .writelines (latex_chapter (
76
+ NameLaTeX (CONFIG .get_chapter_title (chapter ))))
76
77
77
- sections : list [Section ] = CONFIG .get_sections_by_chapter (chapter )
78
+ sections : list [Section ] = CONFIG .get_sections_by_chapter (
79
+ chapter )
78
80
logger .info (f"{ len (sections )} section(s) found in config" )
79
81
80
82
logger .debug (f"Getting section from existing files" )
81
83
_sections_generated : list [Section ] = []
82
- _partitioned_code_filename = load_from (os .path .join (CONFIG .get_code_dir (), chapter ))
83
- _partitioned_doc_filename = load_from (os .path .join (CONFIG .get_doc_dir (), chapter ))
84
+ _partitioned_code_filename = load_from (
85
+ os .path .join (CONFIG .get_code_dir (), chapter ))
86
+ _partitioned_doc_filename = load_from (
87
+ os .path .join (CONFIG .get_doc_dir (), chapter ))
84
88
__dict : dict [str , str ] = dict (_partitioned_code_filename )
85
89
if CONFIG .enable_test ():
86
- _partitioned_test_filename = load_from (os .path .join (CONFIG .get_test_dir (), chapter ))
90
+ _partitioned_test_filename = load_from (
91
+ os .path .join (CONFIG .get_test_dir (), chapter ))
87
92
for k , v in _partitioned_test_filename :
88
93
try :
89
- _sections_generated .append (Section (chapter , k , k , __dict .pop (k ), v ))
94
+ _sections_generated .append (
95
+ Section (chapter , k , k , __dict .pop (k ), v ))
90
96
except KeyError as e :
91
- logger .error (f"Test file '{ k } .{ v } ' found, while code file is not" )
97
+ logger .error (
98
+ f"Test file '{ k } .{ v } ' found, while code file is not" )
92
99
raise e
93
100
__null_ext_name : str = f"{ random .randint (0 , 114514 )} .null"
94
101
for k , v in _partitioned_doc_filename :
95
- _sections_generated .append (Section (chapter , k , k , __dict .pop (k , __null_ext_name ), __null_ext_name ))
102
+ _sections_generated .append (
103
+ Section (chapter , k , k , __dict .pop (k , __null_ext_name ), __null_ext_name ))
96
104
for k , v in __dict .items ():
97
- _sections_generated .append (Section (chapter , k , k , v , __null_ext_name ))
98
- logger .info (f"{ len (_sections_generated )} section(s) generated from existing files" )
105
+ _sections_generated .append (
106
+ Section (chapter , k , k , v , __null_ext_name ))
107
+ logger .info (
108
+ f"{ len (_sections_generated )} section(s) generated from existing files" )
99
109
100
- sections = unique (sections + _sections_generated , lambda x : x .name )
110
+ sections = unique (
111
+ sections + _sections_generated , lambda x : x .name )
101
112
logger .info (f"{ len (sections )} section(s) in total" )
102
113
103
114
for section in sections :
104
115
f .writelines (latex_section (NameLaTeX (section .title )))
105
- code_filepath , doc_filepath , test_filepath = section .get_filenames (CONFIG .get_code_dir (),
106
- CONFIG .get_doc_dir (),
107
- CONFIG .get_test_dir ())
108
- execute_if_file_exist (doc_filepath , lambda x : f .writelines (latex_input (PathLaTeX (x ))))
109
- execute_if_file_exist (code_filepath , lambda x : f .writelines (
110
- latex_listing_code (PathLaTeX (x ), CONFIG .get_code_style (section .code_ext ))))
116
+ code_filepath , doc_filepath , test_filepath = section .get_filenames (
117
+ CONFIG .get_code_dir (), CONFIG .get_doc_dir (), CONFIG .get_test_dir ())
118
+ f .writelines (latex_input (PathLaTeX (doc_filepath )))
119
+ f .writelines (latex_listing_code (
120
+ PathLaTeX (code_filepath ), CONFIG .get_code_style (section .code_ext )))
111
121
if CONFIG .generate_test_in_notebook ():
112
- execute_if_file_exist (test_filepath , lambda x : f .writelines (
113
- latex_listing_code (PathLaTeX (x ), CONFIG .get_code_style (section .test_ext ))))
122
+ if not os .path .getsize (test_filepath ):
123
+ continue
124
+ f .writelines (latex_listing_code (
125
+ PathLaTeX (test_filepath ), CONFIG .get_code_style (section .test_ext )))
114
126
115
127
@withlog
116
128
def load_from (dir_name : str , ** kwargs ) -> list [tuple [str , str ]]:
117
- filename_in_dir : list [str ] = scandir_file_merge (CONFIG .get_all_code_ext_names (), dir_name )
129
+ filename_in_dir : list [str ] = scandir_file_merge (
130
+ CONFIG .get_all_code_ext_names (), dir_name )
118
131
kwargs .get ('logger' ).info (rf"{ len (filename_in_dir )} file(s) found" )
119
132
if len (filename_in_dir ):
120
- kwargs .get ('logger' ).debug ('Which are:\n \t ' + '\n \t ' .join (filename_in_dir ))
133
+ kwargs .get ('logger' ).debug (
134
+ 'Which are:\n \t ' + '\n \t ' .join (filename_in_dir ))
121
135
122
- _partitioned_filename : list [tuple [str , str ]] = [parse_filename (filename ) for filename in filename_in_dir ]
136
+ _partitioned_filename : list [tuple [str , str ]] = [
137
+ parse_filename (filename ) for filename in filename_in_dir ]
123
138
if len (set ([k for k , v in _partitioned_filename ])) != len (_partitioned_filename ):
124
- kwargs .get ('logger' ).error (f'File with duplicate name found in dir: { dir_name } ' )
139
+ kwargs .get ('logger' ).error (
140
+ f'File with duplicate name found in dir: { dir_name } ' )
125
141
kwargs .get ('logger' ).info (
126
142
'Each filenames in the same folder should not be the same after removing the ext names' )
127
143
raise FileExistsError ()
@@ -143,18 +159,18 @@ def generate_cheatsheet_contents(logger: logging.Logger):
143
159
f .write ('%-*- coding: utf-8 -*-\n ' )
144
160
files : list [str ] = file_preprocess ([f'{ f } .tex' for f in CONFIG .get_cheatsheets ()],
145
161
scandir_file_merge (['tex' ], CONFIG .get_cheatsheet_dir ()), logger )
146
- files = [os .path .join (CONFIG .get_cheatsheet_dir (), item ) for item in files ]
162
+ files = [os .path .join (CONFIG .get_cheatsheet_dir (), item )
163
+ for item in files ]
147
164
logger .info (rf"{ len (files )} file(s) found:" )
148
165
logger .debug ('Which are:\n \t ' + '\n \t ' .join (files ))
149
166
logger .debug ('Will include in listed order' )
150
167
151
168
f .writelines (latex_chapter (NameLaTeX ('Cheatsheet' )))
152
169
for file in files :
153
- f .writelines (
154
- latex_section (NameLaTeX (CONFIG .get_cheatsheet_name (file .removeprefix (CONFIG .get_cheatsheet_dir ())
155
- .removeprefix ('/' )
156
- .removeprefix ('\\ ' )
157
- .removesuffix ('.tex' )))))
170
+ f .writelines (latex_section (NameLaTeX (CONFIG .get_cheatsheet_name (file .removeprefix (CONFIG .get_cheatsheet_dir ())
171
+ .removeprefix ('/' )
172
+ .removeprefix ('\\ ' )
173
+ .removesuffix ('.tex' )))))
158
174
f .writelines (latex_input (PathLaTeX (file )))
159
175
160
176
generate_cheatsheet_contents ()
@@ -173,7 +189,8 @@ def compile_all(_no_fmt: bool, _no_gen: bool, _no_clean: bool, **kwargs):
173
189
for procedure in LATEX_COMPILE_COMMAND_GROUP :
174
190
now_proc : list [str ] = procedure (CONFIG .get_notebook_file ())
175
191
cnt += 1
176
- kwargs .get ('logger' ).info (f'Step { cnt } / { len (LATEX_COMPILE_COMMAND_GROUP )} ' + '\n ' + '-' * 80 )
192
+ kwargs .get ('logger' ).info (
193
+ f'Step { cnt } / { len (LATEX_COMPILE_COMMAND_GROUP )} ' + '\n ' + '-' * 80 )
177
194
subprocess .run (now_proc , encoding = 'utf8' , check = True )
178
195
179
196
kwargs .get ('logger' ).info ('Finished' )
@@ -203,7 +220,8 @@ def reformat_all_codes(_code_type: str, **kwargs):
203
220
CONFIG .get_ext_names_by_code_style (_code_type ))
204
221
kwargs .get ('logger' ).info (f"{ len (filepaths )} file(s) found" )
205
222
for filepath in filepaths :
206
- cmd : list [str ] = CONFIG .get_formatting_command (_code_type , filepath )
223
+ cmd : list [str ] = CONFIG .get_formatting_command (
224
+ _code_type , filepath )
207
225
subprocess .run (cmd , encoding = 'utf8' , check = True )
208
226
209
227
kwargs .get ('logger' ).info ('finished' )
@@ -226,20 +244,24 @@ def _new_note(chapter_name: str, file_name: str, section_title: str, code_ext_na
226
244
@withlog
227
245
def add_new_note (_chapter_name : str , _file_name : str , _section_title : str ,
228
246
_code_ext_name : str , _test_ext_name : str , ** kwargs ):
229
- section : Section = Section (_chapter_name , _file_name , _section_title , _code_ext_name , _test_ext_name )
247
+ section : Section = Section (
248
+ _chapter_name , _file_name , _section_title , _code_ext_name , _test_ext_name )
230
249
231
- section .open (CONFIG .get_code_dir (), CONFIG .get_doc_dir (), CONFIG .get_test_dir (), 'x' )
250
+ section .open (CONFIG .get_code_dir (), CONFIG .get_doc_dir (),
251
+ CONFIG .get_test_dir (), 'x' )
232
252
kwargs .get ('logger' ).info ('Created' )
233
253
234
- _code , _doc , _test = section .get_filenames (CONFIG .get_code_dir (), CONFIG .get_doc_dir (), CONFIG .get_test_dir ())
254
+ _code , _doc , _test = section .get_filenames (
255
+ CONFIG .get_code_dir (), CONFIG .get_doc_dir (), CONFIG .get_test_dir ())
235
256
kwargs .get ('logger' ).info (f"Code: { os .path .join (os .curdir , _code )} " )
236
257
kwargs .get ('logger' ).info (f"Doc: { os .path .join (os .curdir , _doc )} " )
237
258
kwargs .get ('logger' ).info (f"Test: { os .path .join (os .curdir , _test )} " )
238
259
239
260
CONFIG .append_section (section )
240
261
kwargs .get ('logger' ).info ('Config updated' )
241
262
242
- add_new_note (chapter_name , file_name , section_title , code_ext_name , test_ext_name )
263
+ add_new_note (chapter_name , file_name , section_title ,
264
+ code_ext_name , test_ext_name )
243
265
244
266
245
267
# TODO
0 commit comments