diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0ed121 --- /dev/null +++ b/.gitignore @@ -0,0 +1,588 @@ + +# Created by https://www.gitignore.io/api/vim,python,intellij,visualstudio,visualstudiocode +# Edit at https://www.gitignore.io/?templates=vim,python,intellij,visualstudio,visualstudiocode + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +# JetBrains templates +**___jb_tmp___ + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don’t work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history + +### VisualStudio ### +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- Backup*.rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +.vscode +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# End of https://www.gitignore.io/api/vim,python,intellij,visualstudio,visualstudiocode + +example/* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1f0d32f --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c586353 --- /dev/null +++ b/README.md @@ -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 + ./-<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. diff --git a/deforest/__init__.py b/deforest/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/deforest/cleaner.py b/deforest/cleaner.py new file mode 100644 index 0000000..8d17094 --- /dev/null +++ b/deforest/cleaner.py @@ -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) diff --git a/deforest/constant.py b/deforest/constant.py new file mode 100644 index 0000000..15a791c --- /dev/null +++ b/deforest/constant.py @@ -0,0 +1,2 @@ +VERSION = "0.1.0" +LOGGER = "deforest" diff --git a/deforest/deforest.py b/deforest/deforest.py new file mode 100644 index 0000000..ad34390 --- /dev/null +++ b/deforest/deforest.py @@ -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) diff --git a/deforest/tags.py b/deforest/tags.py new file mode 100644 index 0000000..b5b5100 --- /dev/null +++ b/deforest/tags.py @@ -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 '' diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7f3fe6e --- /dev/null +++ b/setup.py @@ -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="hawry@hawry.net", + 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" + ] +)