diff --git a/cli.py b/cli.py index 417ee77..e8f3c4f 100644 --- a/cli.py +++ b/cli.py @@ -39,7 +39,7 @@ def build(files, lib): with open(f"{name}.yaml", "w") as template: click.echo(f'{head} Saving to "{name}.yaml"') - template.write(ct.yaml) + template.write(ct.yaml()) cli.add_command(build) diff --git a/poetry.lock b/poetry.lock index 9ad1a3e..87162a0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -15,6 +15,14 @@ optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" version = "1.3.0" +[[package]] +category = "main" +description = "Simple decorator to set attributes of target function or class in a DRY way." +name = "attr" +optional = false +python-versions = "*" +version = "0.3.1" + [[package]] category = "dev" description = "Classes Without Boilerplate" @@ -312,12 +320,13 @@ version = "0.6.0" more-itertools = "*" [metadata] -content-hash = "8b98f0ec71c743eaa4b4e20ea47a27623933499bc148691cb9e6873769973491" +content-hash = "34aba17e7f55024ab4816c63d2f35b6e40e62692876d018dae01f0e782d6e9d4" python-versions = "^3.7" [metadata.hashes] appdirs = ["9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92", "d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e"] atomicwrites = ["03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4", "75a9445bac02d8d058d5e1fe689654ba5a6556a1dfd8ce6ec55a0ed79866cfa6"] +attr = ["0b1aaddb85bd9e9c4bd75092f4440d6616ff40b0df0437f00771871670f7c9fd", "9091548058d17f132596e61fa7518e504f76b9a4c61ca7d86e1f96dbf7d4775d"] attrs = ["08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c", "f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72"] black = ["09a9dcb7c46ed496a9850b76e4e825d6049ecd38b611f1224857a79bd985a8cf", "68950ffd4d9169716bcb8719a56c07a2f4485354fec061cdd5910aa07369731c"] click = ["2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", "5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"] diff --git a/policytool/models.py b/policytool/models.py index 4fe30ea..c021cfe 100644 --- a/policytool/models.py +++ b/policytool/models.py @@ -1,5 +1,6 @@ from typing import List +import attr import yaml @@ -9,15 +10,27 @@ def str_presenter(dumper, data): return dumper.represent_scalar("tag:yaml.org,2002:str", data) +@attr.s(auto_attribs=True) class ConstraintTemplate(object): - def __init__(self, kind: str, rego: str, libs: List[str] = []): - self.name = kind.lower() - self.kind = kind - self.list_kind = f"{kind}List" - self.plural = self.name - self.singular = self.name.rstrip("s") - self.rego = rego - self.libs = libs + kind: str + rego: str + libs: List[str] = attr.Factory(list) + + @property + def name(self): + return self.kind.lower() + + @property + def plural(self): + return self.name + + @property + def singular(self): + return self.name.rstrip("s") + + @property + def list_kind(self): + return f"{self.kind}List" @property def _template(self): @@ -45,7 +58,6 @@ def _template(self): temp["spec"]["targets"][0]["libs"] = self.libs return temp - @property def yaml(self): yaml.add_representer(str, str_presenter) return yaml.dump(self._template) diff --git a/policytool/test_models.py b/policytool/test_models.py index 78608e6..f695ee1 100644 --- a/policytool/test_models.py +++ b/policytool/test_models.py @@ -44,7 +44,7 @@ def test_default_libs(self, template): def test_yaml(self, template): assert ( - template.yaml + template.yaml() == """apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: diff --git a/pyproject.toml b/pyproject.toml index 4237d7e..9dc3e7e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,13 +5,14 @@ license = "Apache-2.0" description = "A set of utilities and classes for working with Open Policy Agent based tools, including Gatekeeper and Conftest" authors = ["Gareth Rushgrove "] readme = "README.md" -repository = "https://github.com/garethr/policytool +repository = "https://github.com/garethr/policytool" [tool.poetry.dependencies] python = "^3.7" PyYAML = "^5.1" click = "^7.0" colorama = "^0.4.1" +attr = "^0.3.1" [tool.poetry.scripts] policytool = "cli:cli"