@@ -143,6 +143,7 @@ def __init__(self, tex_root):
143143 self ._command_cache = {}
144144
145145 self ._import_base_paths = {}
146+ self ._graphics_path = None
146147
147148 self .__frozen = False
148149
@@ -164,10 +165,9 @@ def tex_base_path(self, file_path):
164165 """
165166 file_path = os .path .normpath (file_path )
166167 try :
167- base_path = self ._import_base_paths [file_path ]
168+ return self ._import_base_paths [file_path ]
168169 except KeyError :
169- base_path , _ = os .path .split (self ._tex_root )
170- return base_path
170+ return os .path .dirname (self ._tex_root )
171171
172172 def content (self , file_name ):
173173 """
@@ -234,43 +234,33 @@ def filter_commands(self, how, flags=DEFAULT_FLAGS):
234234 NO_BEGIN_END_COMMANDS | ONLY_COMMANDS_WITH_ARGS
235235
236236 Returns:
237- A list of all commands, which are preprocessed with the flags
237+ A `generator` expression producing all commands, which are preprocessed
238+ with the flags.
238239 """
239- # convert the filter into a function
240240 if isinstance (how , str ):
241-
242- def command_filter (c ):
243- return c .command == how
241+ return (c for c in self ._commands (flags ) if c .command == how )
244242
245243 elif isinstance (how , Iterable ):
246-
247- def command_filter (c ):
248- return c .command in how
244+ return (c for c in self ._commands (flags ) if c .command in how )
249245
250246 elif callable (how ):
247+ return (c for c in self ._commands (flags ) if how (c ))
251248
252- def command_filter (c ):
253- return how (c )
254-
255- else :
256- raise Exception ("Unsupported filter type: " + str (type (how )))
257- com = self ._commands (flags )
258- return filter (command_filter , com )
249+ raise Exception (f"Unsupported filter type: { type (how )} " )
259250
260251 def graphics_paths (self ):
261- try :
262- return self ._graphics_path
263- except AttributeError :
264- pass
265- self ._graphics_path = []
266- commands = self .filter_commands ("graphicspath" )
267- for com in commands :
268- base_path = os .path .join (self .tex_base_path (com .file_name ))
269- paths = (p .rstrip ("}" ) for p in com .args .split ("{" ) if p )
270- self ._graphics_path .extend (
271- os .path .normpath (p if os .path .isabs (p ) else os .path .join (base_path , p ))
272- for p in paths
273- )
252+ if self ._graphics_path is None :
253+ result = []
254+ commands = self .filter_commands ("graphicspath" )
255+ for com in commands :
256+ base_path = os .path .join (self .tex_base_path (com .file_name ))
257+ paths = (p .rstrip ("}" ) for p in com .args .split ("{" ) if p )
258+ result .extend (
259+ os .path .normpath (p if os .path .isabs (p ) else os .path .join (base_path , p ))
260+ for p in paths
261+ )
262+ # freeze result
263+ self ._graphics_path = tuple (result )
274264
275265 return self ._graphics_path
276266
@@ -300,6 +290,8 @@ def _commands(self, flags):
300290 return self ._command_cache [flags ]
301291
302292 def _freeze (self ):
293+ if self .__frozen :
294+ return
303295 self ._content = frozendict (** self ._content )
304296 self ._raw_content = frozendict (** self ._raw_content )
305297 self ._all_commands = tuple (c for c in self ._all_commands )
@@ -341,10 +333,7 @@ def get_analysis(tex_root):
341333 elif not isinstance (tex_root , str ):
342334 raise TypeError ("tex_root must be a string or view" )
343335
344- result = LocalCache (tex_root ).cache ("analysis" , partial (analyze_document , tex_root ))
345- if result :
346- result ._freeze ()
347- return result
336+ return LocalCache (tex_root ).cache ("analysis" , partial (analyze_document , tex_root ))
348337
349338
350339def _generate_entries (m , file_name , offset = 0 ):
@@ -408,6 +397,8 @@ def analyze_document(tex_root):
408397 raise TypeError ("tex_root must be a string or view" )
409398
410399 result = _analyze_tex_file (tex_root )
400+ if result :
401+ result ._freeze ()
411402 return result
412403
413404
0 commit comments