[docs]classSourceModule:"""Container that discovers and parses .zig files under a directory. Parameters ---------- dir_path: Directory to search for `.zig` files. lazy_parsing: Controls whether parsing is deferred. When enabled, constructing a :class:`SourceFile` is cheap and actual parsing happens later when the file's content/unit is accessed. When disabled, parsing is performed eagerly during construction. .. versionadded:: 0.2.3 use_threading: Controls whether parsing work is submitted to a thread pool. Using threads can significantly speed up parsing when there are many files, because parsing and native calls can run concurrently. .. versionadded:: 0.2.3 max_workers: Upper bound on the number of worker threads when threading is used. If None, picks a sensible default based on CPU count and number of files. .. versionadded:: 0.2.3 """def__init__(self,dir_path:str,lazy_parsing:bool=False,use_threading:bool=False,max_workers:Optional[int]=None,)->None:self.lazy_parsing=lazy_parsingself.use_threading=use_threadingself.max_workers=Noneifmax_workersisNoneelsemax_workersself._dir_path=dir_pathdef__repr__(self)->str:return(f"SourceModule(dir={self._dir_path}, use_threading={self.use_threading}, "f"max_workers={self.max_workers}, lazy_parsing={self.lazy_parsing})")@propertydeffiles(self)->list[SourceFile]:"""A list of SourceFile objects for every .zig file under ``dir_path``."""paths:list[str]=[]forroot,_,filesinwalk(self._dir_path):forfilenameinfiles:iffilename.endswith(".zig"):full_path=path.join(root,filename)paths.append(full_path)ifnotpaths:return[]# Sequential parsing path: simple and predictableifnotself.use_threading:return[SourceFile(p,lazy_parsing=self.lazy_parsing)forpinpaths]# Decide number of workers; explicit setting wins,# otherwise compute a reasonable default.max_workers=self.max_workersormin(len(paths),cpu_count()or4)# Ensure native library is initialised before spawning workers to avoid# races during library load or global init.init_native_library()withThreadPoolExecutor(max_workers=max_workers)asex:sources=list(ex.map(lambdap:SourceFile(p,lazy_parsing=self.lazy_parsing),paths))returnsources@propertydefdir_path(self)->str:"""Path that this SourceModule will walk for .zig files."""returnself._dir_path