Skip to content

Commit

Permalink
feat(py): Find _brand.yml or _brand.yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Oct 23, 2024
1 parent 9cb55c0 commit 9e583be
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
22 changes: 11 additions & 11 deletions docs/pkg/py/Brand.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ path

| Name | Description |
| --- | --- |
| [from_yaml](#brand_yml.Brand.from_yaml) | Read a Brand YAML file. |
| [from_yaml](#brand_yml.Brand.from_yaml) | Create a Brand instance from a Brand YAML file. |
| [from_yaml_str](#brand_yml.Brand.from_yaml_str) | Create a Brand instance from a string of YAML. |
| [model_dump_yaml](#brand_yml.Brand.model_dump_yaml) | Serialize the Brand object to YAML. |

Expand All @@ -75,28 +75,28 @@ path
Brand.from_yaml(path)
```

Read a Brand YAML file.
Create a Brand instance from a Brand YAML file.

Reads a Brand YAML file or finds and reads a `_brand.yml` file and returns
a validated :class:`Brand` instance.
Reads a Brand YAML file or finds and reads a `_brand.yml` file and
returns a validated :class:`Brand` instance.

To find a project-specific `_brand.yaml` file, pass `path` the project
To find a project-specific `_brand.yml` file, pass `path` the project
directory or `__file__` (the path of the current Python script).
[`brand_yml.Brand.from_yaml`](`brand_yml.Brand.from_yaml`) will look in
that directory or any parent directory for a `_brand.yml`,
`brand/_brand.yml` or `_brand/_brand.yml` file. Note that it starts the
search in the directory passed in and moves upward to find the
`_brand.yml` file; it does not search into subdirectories of the current
directory.
`brand/_brand.yml` or `_brand/_brand.yml` file (or the same variants
with a `.yaml` extension). Note that it starts the search in the
directory passed in and moves upward to find the `_brand.yml` file; it
does not search into subdirectories of the current directory.

#### Parameters {.doc-section .doc-section-parameters}

<code><span class="parameter-name">path</span><span class="parameter-annotation-sep">:</span> <span class="parameter-annotation">[str](`str`) \| [Path](`pathlib.Path`)</span></code>

: The path to the brand.yml file or a directory where `_brand.yml` is
expected to be found. Typically, you can pass `__file__` from the
calling script to find `_brand.yml` in the current directory or any of
its parent directories.
calling script to find `_brand.yml` or `_brand.yaml` in the current
directory or any of its parent directories.

#### Returns {.doc-section .doc-section-returns}

Expand Down
3 changes: 3 additions & 0 deletions docs/pkg/py/utilities.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ parent directory. In each directory, `find_project_brand_yml()` looks for
any of the following files in the given order:

* `_brand.yml`
* `_brand.yaml`
* `brand/_brand.yml`
* `brand/_brand.yaml`
* `_brand/_brand.yml`
* `_brand/_brand.yaml`

## Parameters {.doc-section .doc-section-parameters}

Expand Down
20 changes: 10 additions & 10 deletions pkg-py/src/brand_yml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ class Brand(BrandBase):
@classmethod
def from_yaml(cls, path: str | Path):
"""
Read a Brand YAML file.
Create a Brand instance from a Brand YAML file.
Reads a Brand YAML file or finds and reads a `_brand.yml` file and returns
a validated :class:`Brand` instance.
Reads a Brand YAML file or finds and reads a `_brand.yml` file and
returns a validated :class:`Brand` instance.
To find a project-specific `_brand.yaml` file, pass `path` the project
To find a project-specific `_brand.yml` file, pass `path` the project
directory or `__file__` (the path of the current Python script).
[`brand_yml.Brand.from_yaml`](`brand_yml.Brand.from_yaml`) will look in
that directory or any parent directory for a `_brand.yml`,
`brand/_brand.yml` or `_brand/_brand.yml` file. Note that it starts the
search in the directory passed in and moves upward to find the
`_brand.yml` file; it does not search into subdirectories of the current
directory.
`brand/_brand.yml` or `_brand/_brand.yml` file (or the same variants
with a `.yaml` extension). Note that it starts the search in the
directory passed in and moves upward to find the `_brand.yml` file; it
does not search into subdirectories of the current directory.
Parameters
----------
path
The path to the brand.yml file or a directory where `_brand.yml` is
expected to be found. Typically, you can pass `__file__` from the
calling script to find `_brand.yml` in the current directory or any of
its parent directories.
calling script to find `_brand.yml` or `_brand.yaml` in the current
directory or any of its parent directories.
Returns
-------
Expand Down
24 changes: 18 additions & 6 deletions pkg-py/src/brand_yml/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@


def find_project_file(
filename: str,
filename: tuple[str, ...] | str,
dir_: Path,
subdir: tuple[str, ...] = (),
) -> Path:
dir_og = dir_
i = 0
max_parents = 20

if isinstance(filename, str):
filename = tuple([filename])

while dir_ != dir_.parent and i < max_parents:
if (dir_ / filename).exists():
return dir_ / filename
for fname in filename:
if (dir_ / fname).exists():
return dir_ / fname
for sub in subdir:
if (dir_ / sub / filename).exists():
return dir_ / sub / filename
for fname in filename:
if (dir_ / sub / fname).exists():
return dir_ / sub / fname
dir_ = dir_.parent
i += 1

Expand All @@ -41,8 +46,11 @@ def find_project_brand_yml(path: Path | str) -> Path:
any of the following files in the given order:
* `_brand.yml`
* `_brand.yaml`
* `brand/_brand.yml`
* `brand/_brand.yaml`
* `_brand/_brand.yml`
* `_brand/_brand.yaml`
Parameters
----------
Expand All @@ -66,7 +74,11 @@ def find_project_brand_yml(path: Path | str) -> Path:
if path.is_file():
path = path.parent

return find_project_file("_brand.yml", path, ("brand", "_brand"))
return find_project_file(
filename=("_brand.yml", "_brand.yaml"),
dir_=path,
subdir=("brand", "_brand"),
)


PredicateFuncType = Callable[[Any], bool]
Expand Down

0 comments on commit 9e583be

Please sign in to comment.