Skip to content

Commit

Permalink
Merge pull request #6 from hawry/add-more-tags
Browse files Browse the repository at this point in the history
add custom tags
  • Loading branch information
hawry authored Oct 14, 2019
2 parents 664629b + 53970cc commit 5416f4e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 56 deletions.
9 changes: 9 additions & 0 deletions deforest/cleaners.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def _is_apigw(self, node):
def _has_body(self, node):
return CloudFormationCleaner.KEY_CF_PROPERTIES in node and CloudFormationCleaner.KEY_CF_BODY in node[CloudFormationCleaner.KEY_CF_PROPERTIES]

def __str__(self):
return "CloudFormationCleaner"


class DefaultCleaner:
keys = ["x-amazon"]
Expand All @@ -62,6 +65,9 @@ def _clean(self, v):
if isinstance(v[k], dict):
self._clean(v[k])

def __str__(self):
return "DefaultCleaner"


class IgnoreCleaner:
DEFOREST_IGNORE_KEY = "x-deforest-ignore"
Expand All @@ -85,3 +91,6 @@ def _clean(self, v):
del v[k]
else:
self._clean(v[k])

def __str__(self):
return "IgnoreCleaner"
2 changes: 1 addition & 1 deletion deforest/constant.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "0.2.1"
VERSION = "0.2.2"
LOGGER = "deforest"
EXIT_NOTFOUND = 1
EXIT_PARSEERR = 2
36 changes: 21 additions & 15 deletions deforest/filecleaner.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
from Queue import Queue
from tags import GetAttTag, SubTag, RefTag
import yaml
from cleaners import DefaultCleaner, CloudFormationCleaner, IgnoreCleaner
import logging
import sys
from constant import EXIT_PARSEERR

import yaml

import cleaners
import tags
import cleaners
import constant


class ForestCleaner:
Expand All @@ -24,22 +27,24 @@ def _parse_data(self):
self.result = yaml.safe_load(self.data)
except yaml.scanner.ScannerError as e:
logging.error("could not parse file: {}".format(e))
sys.exit(EXIT_PARSEERR)
sys.exit(constant.EXIT_PARSEERR)

def _create_queue(self):
logging.debug("creating cleaner queue")
cleaner_queue = Queue()
logging.debug("created queue with size {}".format(
cleaner_queue.qsize()))

cleaner_queue.put(CloudFormationCleaner(self))
cleaner_queue.put(cleaners.CloudFormationCleaner(self))

cleaner_queue.put(DefaultCleaner(self))
cleaner_queue.put(cleaners.DefaultCleaner(self))

if not self.allow_ignored:
cleaner_queue.put(IgnoreCleaner(self))
cleaner_queue.put(cleaners.IgnoreCleaner(self))
else:
logging.info("allowing x-deforest-ignore paths")
logging.debug(
"cleaning will use {} methods".format(cleaner_queue.qsize()))
self.cleaner_queue = cleaner_queue

def clean(self):
Expand All @@ -50,12 +55,13 @@ def clean(self):

def _enable_cf_tags(self):
logging.debug("enabling CF tags")
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)
aws_tags = ["!GetAtt", "!Sub", "!Ref", "!Base64", "!Cidr", "!ImportValue", "!GetAZs", "!FindInMap",
"!Join", "!Select", "!Split", "!Transform", "!And", "!Equals", "!If", "!Not", "!Or"]
for t in aws_tags:
yaml.SafeLoader.add_constructor(t, tags.AWSTag.from_yaml)
yaml.SafeDumper.add_multi_representer(
tags.AWSTag, tags.AWSTag.to_yaml)
logging.debug("enabled {} tags".format(len(aws_tags)))

@property
def result(self):
Expand Down
42 changes: 2 additions & 40 deletions deforest/tags.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
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'

class AWSTag(yaml.YAMLObject):
def __init__(self, var):
self.var = var

Expand All @@ -48,7 +10,7 @@ def __repr__(self):

@classmethod
def from_yaml(cls, loader, node):
return RefTag(node.value)
return cls(node.value)

@classmethod
def to_yaml(cls, dumper, data):
Expand Down

0 comments on commit 5416f4e

Please sign in to comment.