Skip to content

Commit

Permalink
refactor: small improvements for push
Browse files Browse the repository at this point in the history
  • Loading branch information
clay-lake committed Dec 6, 2024
1 parent 86899f8 commit 5241995
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 90 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode
__pycache__
*.rock
.venv/
yaml_checker/yaml_checker.egg-info
108 changes: 108 additions & 0 deletions yaml_checker/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
# YAML Checker

An internal CLI util for formatting and validating YAML files. This project
relies on Pydantic and Ruamel libraries.

**Installation**
```bash
pip install -e yaml_checker
```

**Usage**
```
usage: yaml_checker [-h] [-v] [-w] [--config CONFIG] [files ...]
positional arguments:
files Additional files to process (optional).
options:
-h, --help show this help message and exit
-v, --verbose Enable verbose output.
-w, --write Write yaml output to disk.
--config CONFIG CheckYAML subclass to load
```

**Example**

```bash
# Lets cat a demonstration file for comparison.
$ cat yaml_checker/demo/slice.yaml
# yaml_checker --config=Chisel demo/slice.yaml

package: grep

essential:
- grep_copyright

# hello: world

slices:
bins:
essential:
- libpcre2-8-0_libs # tests

# another test
- libc6_libs
contents:
/usr/bin/grep:

deprecated:
# These are shell scripts requiring a symlink from /usr/bin/dash to
# /usr/bin/sh.
# See: https://manpages.ubuntu.com/manpages/noble/en/man1/grep.1.html
essential:
- dash_bins
- grep_bins
contents:
# we ned this leading comment
/usr/bin/rgrep: # this should be last

/usr/bin/fgrep:

# careful with this path ...
/usr/bin/egrep: # it is my favorite
copyright:
contents:
/usr/share/doc/grep/copyright:
# Note: Missing new line at EOF

# Now we can run the yaml_checker to format the same file.
# Note how comments are preserved during sorting of lists and
# dict type objects. If you want to test the validator,
# uncomment the hello field.
$ yaml_checker --config=Chisel yaml_checker/demo/slice.yaml
# yaml_checker --config=Chisel demo/slice.yaml

package: grep

essential:
- grep_copyright

# hello: world

slices:
bins:
essential:
- libc6_libs
- libpcre2-8-0_libs # tests

# another test
contents:
/usr/bin/grep:

deprecated:
# These are shell scripts requiring a symlink from /usr/bin/dash to
# /usr/bin/sh.
# See: https://manpages.ubuntu.com/manpages/noble/en/man1/grep.1.html
essential:
- dash_bins
- grep_bins
contents:
# we ned this leading comment

# careful with this path ...
/usr/bin/egrep: # it is my favorite
/usr/bin/fgrep:
/usr/bin/rgrep: # this should be last
copyright:
contents:
/usr/share/doc/grep/copyright:

```
13 changes: 0 additions & 13 deletions yaml_checker/demo/basic.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions yaml_checker/demo/factory.yaml

This file was deleted.

28 changes: 16 additions & 12 deletions yaml_checker/demo/slice.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
# python3 -m yaml_checker --config=Chisel demo/factory.yaml
# yaml_checker --config=Chisel demo/slice.yaml

package: grep

essential:
- grep_copyright
- grep_copyright

# hello: world

slices:
bins:
essential:
- libpcre2-8-0_libs # tests
- libc6_libs
- libpcre2-8-0_libs # tests

# another test
- libc6_libs
contents:
/usr/bin/grep:
/usr/bin/grep:

deprecated:
# These are shell scripts requiring a symlink from /usr/bin/dash to
# /usr/bin/sh.
# See: https://manpages.ubuntu.com/manpages/noble/en/man1/grep.1.html
essential: ["dash_bins", "grep_bins"]
essential:
- dash_bins
- grep_bins
contents:
/usr/bin/rgrep:
# we ned this leading comment
/usr/bin/rgrep: # this should be last

# tests
/usr/bin/fgrep:

/usr/bin/egrep: # tests1

# careful with this path ...
/usr/bin/egrep: # it is my favorite
copyright:
contents:
/usr/share/doc/grep/copyright:
contents:
/usr/share/doc/grep/copyright:
4 changes: 2 additions & 2 deletions yaml_checker/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ruyaml
pytest
pydantic==2.8.2
ruamel.yaml==0.18.6
8 changes: 0 additions & 8 deletions yaml_checker/sample.yaml

This file was deleted.

18 changes: 12 additions & 6 deletions yaml_checker/setup.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from pathlib import Path
from setuptools import setup, find_packages

from setuptools import find_packages, setup

def read(filename):

def read_text(filename):
filepath = Path(__file__).parent / filename
file = open(filepath, "r")
return file.read()
return filepath.read_text()


setup(
name="yaml_checker",
version="0.1.0",
long_description=read("README.md"),
long_description=read_text("README.md"),
packages=find_packages(),
install_requires=read("requirements.txt"),
install_requires=read_text("requirements.txt"),
entry_points={
"console_scripts": [
"yaml_checker=yaml_checker.__main__:main",
"clayaml=yaml_checker.__main__:main",
],
},
)
7 changes: 4 additions & 3 deletions yaml_checker/yaml_checker/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .config.base import YAMLCheckConfigBase
from pathlib import Path
import argparse
import logging
from pathlib import Path

import argparse
from .config.base import YAMLCheckConfigBase

# TODO: display all available configs in help
parser = argparse.ArgumentParser()

parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions yaml_checker/yaml_checker/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from pathlib import Path
from importlib import import_module
from pathlib import Path

submodule_root = Path(__file__).parent
package_name = __name__


# import all submodules so our configs registry is populated
for submodule in submodule_root.glob("*.py"):
submodule_name = submodule.stem

Expand Down
11 changes: 6 additions & 5 deletions yaml_checker/yaml_checker/config/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fnmatch
from io import StringIO
import logging
from io import StringIO
from pathlib import Path
from pydantic import BaseModel
from ruyaml import YAML
from typing import Any

from pydantic import BaseModel
from ruamel.yaml import YAML


class YAMLCheckConfigReg(type):
def __init__(cls, *args, **kwargs):
Expand All @@ -27,7 +28,7 @@ class Config:
extra = "allow"

class Config:
"""ruyaml.YAML configuration set before loading."""
"""ruamel.yaml configuration set before loading."""

preserve_quotes = True
width = 80
Expand All @@ -49,7 +50,7 @@ def __init__(self):
if hasattr(self.yaml, attr):
setattr(self.yaml, attr, attr_val)
else:
raise AttributeError(f"Invalid ruyaml.YAML attribute: {attr}")
raise AttributeError(f"Invalid ruamel.yaml attribute: {attr}")

def load(self, yaml_str: str):
"""Load YAML data from string"""
Expand Down
Loading

0 comments on commit 5241995

Please sign in to comment.