-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: fal file #281
Merged
Merged
feat: fal file #281
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cab16ea
wip:
badayvedat 8fb09b6
feat: add fal file
badayvedat 0878f0a
feat: add tests
badayvedat 97bdaec
chore: remove unused file
badayvedat 81fe919
chore: nit
badayvedat 4c42c5d
feat: add test for no auth
badayvedat c6f02f8
Merge branch 'main' into vedat/fea-fal-file
badayvedat df66141
revert some changes
badayvedat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from functools import lru_cache | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional, Sequence, Tuple, Union | ||
|
||
import tomli | ||
|
||
|
||
@lru_cache | ||
def _load_toml(path: Union[Path, str]) -> Dict[str, Any]: | ||
with open(path, "rb") as f: | ||
return tomli.load(f) | ||
|
||
|
||
@lru_cache | ||
def _cached_resolve(path: Path) -> Path: | ||
return path.resolve() | ||
|
||
|
||
@lru_cache | ||
def find_project_root(srcs: Optional[Sequence[str]]) -> Tuple[Path, str]: | ||
"""Return a directory containing .git, or pyproject.toml. | ||
|
||
That directory will be a common parent of all files and directories | ||
passed in `srcs`. | ||
|
||
If no directory in the tree contains a marker that would specify it's the | ||
project root, the root of the file system is returned. | ||
|
||
Returns a two-tuple with the first element as the project root path and | ||
the second element as a string describing the method by which the | ||
project root was discovered. | ||
""" | ||
if not srcs: | ||
srcs = [str(_cached_resolve(Path.cwd()))] | ||
|
||
path_srcs = [_cached_resolve(Path(Path.cwd(), src)) for src in srcs] | ||
|
||
# A list of lists of parents for each 'src'. 'src' is included as a | ||
# "parent" of itself if it is a directory | ||
src_parents = [ | ||
list(path.parents) + ([path] if path.is_dir() else []) for path in path_srcs | ||
] | ||
|
||
common_base = max( | ||
set.intersection(*(set(parents) for parents in src_parents)), | ||
key=lambda path: path.parts, | ||
) | ||
|
||
for directory in (common_base, *common_base.parents): | ||
if (directory / ".git").exists(): | ||
return directory, ".git directory" | ||
|
||
if (directory / "pyproject.toml").is_file(): | ||
pyproject_toml = _load_toml(directory / "pyproject.toml") | ||
if "fal" in pyproject_toml.get("tool", {}): | ||
return directory, "pyproject.toml" | ||
|
||
return directory, "file system root" | ||
|
||
|
||
def find_pyproject_toml( | ||
path_search_start: Optional[Tuple[str, ...]] = None, | ||
) -> Optional[str]: | ||
"""Find the absolute filepath to a pyproject.toml if it exists""" | ||
path_project_root, _ = find_project_root(path_search_start) | ||
path_pyproject_toml = path_project_root / "pyproject.toml" | ||
|
||
if path_pyproject_toml.is_file(): | ||
return str(path_pyproject_toml) | ||
|
||
|
||
def parse_pyproject_toml(path_config: str) -> Dict[str, Any]: | ||
"""Parse a pyproject toml file, pulling out relevant parts for fal. | ||
|
||
If parsing fails, will raise a tomli.TOMLDecodeError. | ||
""" | ||
pyproject_toml = _load_toml(path_config) | ||
config: Dict[str, Any] = pyproject_toml.get("tool", {}).get("fal", {}) | ||
config = {k.replace("--", "").replace("-", "_"): v for k, v in config.items()} | ||
|
||
return config |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should eventually make this a typed dict or a data class so we have stronger typing and that way caller functions have guarantees about what fields they can expect to access