-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
15 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
|
||
def is_valid_yaml(path: str | Path): | ||
path = Path(path) | ||
|
||
if not path.is_file(): | ||
print(f"File does not exist: {path}") | ||
return False | ||
|
||
try: | ||
with path.open("r") as file: | ||
yaml.safe_load(file) | ||
except yaml.YAMLError as e: | ||
print(f"Invalid YAML file: {path} - Error: {e}") | ||
return False | ||
except OSError as e: | ||
print(f"Error reading file: {path} - Error: {e}") | ||
return False | ||
|
||
return True | ||
|
||
|
||
@contextmanager | ||
def run_within_dir(path: str): | ||
oldpwd = os.getcwd() | ||
os.chdir(path) | ||
try: | ||
yield | ||
finally: | ||
os.chdir(oldpwd) | ||
|
||
|
||
def file_contains_text(file: str, text: str) -> bool: | ||
with open(file) as f: | ||
return f.read().find(text) != -1 |