-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 9299599
Showing
9 changed files
with
795 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,7 @@ | ||
Copyright 2019 Tomas H (hawry) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,28 @@ | ||
# deforest | ||
|
||
Remove all `x-amazon`-tags from your Open API 3 or Swagger 2 specification. Useful if you are using Cloudformation to specify your API Gateways, and want to provide your consumers with the same specification but not wanting to disclose your internal Amazon integrations. | ||
|
||
# Installation | ||
`pip install --user deforest` | ||
|
||
## Features | ||
|
||
- Clean keys starting with the string `x-amazon` | ||
- Handles JSON and YAML input | ||
- Handles JSON and YAML output (defaults to YAML) | ||
|
||
# Usage | ||
``` | ||
Usage: deforest [OPTIONS] INFILE | ||
Options: | ||
-o, --outfile TEXT specify output file, default is | ||
./<title>-<version>.<format> | ||
-f, --format [yaml|json] output format [default: yaml] | ||
-i, --indent INTEGER if output format is json, specify indentation | ||
--version Show the version and exit. | ||
--help Show this message and exit. | ||
``` | ||
|
||
# Limitations | ||
The output file looses its order of the keys in the file, which shouldn't affect you if you're using a converter to create a graphical documentation/specification - but can be confusing if you have a specific internal order you wish to keep. |
Empty file.
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,57 @@ | ||
from tags import GetAttTag, SubTag, RefTag | ||
import yaml | ||
import re | ||
|
||
class DeforestCleaner(): | ||
keys = ["x-amazon"] | ||
filedata = None | ||
raw = {} | ||
processed = None | ||
|
||
def __init__(self, data): | ||
self.filedata = data | ||
|
||
def _namify(self, title, version): | ||
s = "{}-{}".format(title.lower(),version.lower()) | ||
s = re.sub(r"\s+", '-', s) | ||
return s | ||
|
||
def get_title_and_version(self): | ||
title = self.raw["info"]["title"] or "no-title" | ||
version = self.raw["info"]["version"] or "no-version" | ||
return self._namify(title,version) | ||
|
||
def get_raw(self): | ||
return self.raw | ||
|
||
def convert(self): | ||
self._enable_custom_tags() | ||
self._load() | ||
self._clean_all_keys() | ||
self._dump() | ||
return self.processed | ||
|
||
def _clean_all_keys(self): | ||
self._cleanup_keys(self.raw) | ||
|
||
def _cleanup_keys(self, v): | ||
for k in v.keys(): | ||
if any(m in k for m in self.keys): | ||
del v[k] | ||
else: | ||
if isinstance(v[k], dict): | ||
self._cleanup_keys(v[k]) | ||
|
||
def _enable_custom_tags(self): | ||
yaml.SafeLoader.add_constructor('!GetAtt', GetAttTag.from_yaml) | ||
yaml.SafeLoader.add_constructor('!Sub', SubTag.from_yaml) | ||
yaml.SafeLoader.add_constructor('!Ref', RefTag.from_yaml) | ||
yaml.SafeDumper.add_multi_representer(GetAttTag, GetAttTag.to_yaml) | ||
yaml.SafeDumper.add_multi_representer(SubTag, SubTag.to_yaml) | ||
yaml.SafeDumper.add_multi_representer(RefTag, RefTag.to_yaml) | ||
|
||
def _load(self): | ||
self.raw = yaml.safe_load(self.filedata) | ||
|
||
def _dump(self): | ||
self.processed = yaml.safe_dump(self.raw) |
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,2 @@ | ||
VERSION = "0.1.0" | ||
LOGGER = "deforest" |
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,29 @@ | ||
import click | ||
import json | ||
|
||
from constant import VERSION, LOGGER | ||
from cleaner import DeforestCleaner | ||
|
||
@click.command() | ||
@click.argument("infile") | ||
@click.option("--outfile","-o", help="specify output file, default is ./<title>-<version>.<format>") | ||
@click.option("--format","-f", default="yaml", show_default=True, type=click.Choice(["yaml","json"]), help="output format") | ||
@click.option("--indent", "-i", default=4,type=int, help="if output format is json, specify indentation") | ||
@click.version_option(None) | ||
def main(infile,outfile,format,indent): | ||
with open(infile, "r") as fh: | ||
d = fh.read() | ||
|
||
cleaner = DeforestCleaner(d) | ||
result = cleaner.convert() | ||
|
||
if outfile: | ||
filename = outfile | ||
else: | ||
filename = "{}.{}".format(cleaner.get_title_and_version(),"json" if format == "json" else "yaml") | ||
|
||
with open(filename,"w+") as fh: | ||
if format == "json": | ||
fh.write(json.dumps(cleaner.get_raw(), indent=indent)) | ||
else: | ||
fh.write(result) |
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,52 @@ | ||
import yaml | ||
|
||
class GetAttTag(yaml.YAMLObject): | ||
tag = u'!GetAtt' | ||
|
||
def __init__(self, var): | ||
self.var = var | ||
|
||
def __repr__(self): | ||
return self.var | ||
|
||
@classmethod | ||
def from_yaml(cls, loader, node): | ||
return GetAttTag(node.value) | ||
|
||
@classmethod | ||
def to_yaml(cls, dumper, data): | ||
return '' | ||
|
||
class SubTag(yaml.YAMLObject): | ||
tag = u'!Sub' | ||
|
||
def __init__(self, var): | ||
self.var = var | ||
|
||
def __repr__(self): | ||
return self.var | ||
|
||
@classmethod | ||
def from_yaml(cls, loader, node): | ||
return SubTag(node.value) | ||
|
||
@classmethod | ||
def to_yaml(cls, dumper, data): | ||
return '' | ||
|
||
class RefTag(yaml.YAMLObject): | ||
tag = u'!Ref' | ||
|
||
def __init__(self, var): | ||
self.var = var | ||
|
||
def __repr__(self): | ||
return self.var | ||
|
||
@classmethod | ||
def from_yaml(cls, loader, node): | ||
return RefTag(node.value) | ||
|
||
@classmethod | ||
def to_yaml(cls, dumper, data): | ||
return '' |
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,32 @@ | ||
import setuptools | ||
import re | ||
import os | ||
from deforest.constant import VERSION | ||
|
||
with open("README.md","r") as fh: | ||
long_desc = fh.read() | ||
|
||
setuptools.setup( | ||
name="deforest", | ||
version=VERSION, | ||
author="hawry", | ||
entry_points = { | ||
"console_scripts": ["deforest=deforest.deforest:main"] | ||
}, | ||
author_email="[email protected]", | ||
description="Remove all x-amazon tags from your OAS3 specification", | ||
long_description=long_desc, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/hawry/deforester", | ||
packages=setuptools.find_packages(), | ||
install_requires=[ | ||
"pyyaml==5.1.1", | ||
"click==6.7" | ||
], | ||
classifiers=[ | ||
"Programming Language :: Python :: 2.7", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: POSIX :: Linux", | ||
"Development Status :: 3 - Alpha" | ||
] | ||
) |