2020from ..logging import logger
2121from ..version import __version__
2222from ..errors .user import HelpfulUserError
23-
23+ from .. io import AbstractFileCache
2424
2525log = logger ()
2626
2727
2828def read_config_from_toml (
29- path : Path , section : str | None = None
29+ fs : AbstractFileCache , path : Path , section : str | None = None
3030) -> ConfigUpdate | None :
3131 """Read a config from given `path` in given `section`. The path should refer to
3232 a TOML file that should decode to a `Config` object. If `section` is given, only
@@ -39,17 +39,17 @@ def read_config_from_toml(
3939 read_config_from_toml(Path("./pyproject.toml"), "tool.entangled")
4040 ```
4141 """
42- if not path . exists () :
42+ if path not in fs :
4343 return None
4444 try :
45- with open ( path , "rb" ) as f :
46- json : Any = tomllib .load ( f ) # pyright: ignore[reportExplicitAny]
47- if section is not None :
48- for s in section .split ("." ):
49- json = json [s ] # pyright: ignore[reportAny]
50- update = msgspec .convert (json , type = ConfigUpdate )
51- log .debug ("Read config from `%s`" , path )
52- return update
45+ content = fs [ path ]. content
46+ json : Any = tomllib .loads ( content )
47+ if section is not None :
48+ for s in section .split ("." ):
49+ json = json [s ] # pyright: ignore[reportAny]
50+ update = msgspec .convert (json , type = ConfigUpdate )
51+ log .debug ("Read config from `%s`" , path )
52+ return update
5353
5454 except (msgspec .ValidationError , tomllib .TOMLDecodeError ) as e :
5555 raise HelpfulUserError (f"Could not read config: { e } " )
@@ -59,23 +59,33 @@ def read_config_from_toml(
5959 return None
6060
6161
62- def read_config () -> ConfigUpdate | None :
63- if Path ("./entangled.toml" ).exists ():
64- return read_config_from_toml (Path ("./entangled.toml" ))
65- if Path ("./pyproject.toml" ).exists ():
62+ def read_config (fs : AbstractFileCache ) -> ConfigUpdate | None :
63+ """
64+ Read configuration from any of the possible hard-coded locations:
65+
66+ - `./entangled.toml`
67+ - `./pyproject.toml` section `[tool.entangled]`.
68+
69+ Returns a `ConfigUpdate` or `None`. To get the full `Config` object,
70+ run `Config() | read_config(fs)`.
71+ """
72+ if Path ("./entangled.toml" ) in fs :
73+ return read_config_from_toml (fs , Path ("./entangled.toml" ))
74+ if Path ("./pyproject.toml" ) in fs :
6675 return (
67- read_config_from_toml (Path ("./pyproject.toml" ), "tool.entangled" )
76+ read_config_from_toml (fs , Path ("./pyproject.toml" ), "tool.entangled" )
6877 )
6978 return None
7079
7180
72- def get_input_files (cfg : Config ) -> list [Path ]:
81+ def get_input_files (fs : AbstractFileCache , cfg : Config ) -> list [Path ]:
82+ """
83+ Get a sorted list of all input files for this project.
84+ """
7385 log .debug ("watch list: %s; ignoring: %s" , cfg .watch_list , cfg .ignore_list )
74- include_file_list = chain .from_iterable (map (Path ("." ).glob , cfg .watch_list ))
75- input_file_list = [
76- path for path in include_file_list
77- if not any (path .match (pat ) for pat in cfg .ignore_list ) and path .is_file ()
78- ]
86+ input_file_list = filter (
87+ lambda p : not any (p .match (pat ) for pat in cfg .ignore_list ),
88+ chain .from_iterable (map (fs .glob , cfg .watch_list )))
7989 log .debug ("input file list %s" , input_file_list )
8090 return sorted (input_file_list )
8191
0 commit comments