2020 from robocop .config import Config
2121
2222
23- def _get_model_with_optional_lang (get_model_func : Callable , source : Path , lang : Languages ) -> File :
24- """
25- Get Robot file tokenised model with optional language option.
26-
27- Language option was added in more recent Robot version and we need this code for backward compatibility.
28- """
29- if LANG_SUPPORTED :
30- return get_model_func (source , lang = lang )
31- return get_model_func (source )
32-
33-
34- def get_model_with_lang (source : Path , lang : Languages | None ) -> File :
35- if "__init__" in source .name :
36- return _get_model_with_optional_lang (get_init_model , source , lang )
37- if source .suffix == ".resource" :
38- return _get_model_with_optional_lang (get_resource_model , source , lang )
39- return _get_model_with_optional_lang (get_model , source , lang )
40-
41-
4223@dataclass
4324class SourceFile :
4425 """
@@ -55,10 +36,23 @@ class SourceFile:
5536 path : Path
5637 config : Config
5738 _model : File | None = None
58- source_lines : list [str ] | None = None
39+ _source_lines : list [str ] | None = None
5940
6041 @property
6142 def model (self ) -> File :
6243 if self ._model is None :
63- self ._model = get_model_with_lang ( self .path , self . config . languages )
44+ self ._model = self ._load_model ( )
6445 return self ._model
46+
47+ def _load_model (self ) -> File :
48+ """Determine the correct model loader based on file type and loads it."""
49+ if "__init__" in self .path .name :
50+ loader : Callable = get_init_model
51+ elif self .path .suffix == ".resource" :
52+ loader : Callable = get_resource_model
53+ else :
54+ loader : Callable = get_model
55+
56+ if LANG_SUPPORTED :
57+ return loader (self .path , lang = self .config .languages )
58+ return loader (self .path )
0 commit comments