Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
refactor color handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Bakker committed Feb 14, 2021
1 parent 5fb08c8 commit 4d2071f
Show file tree
Hide file tree
Showing 8 changed files with 646 additions and 392 deletions.
449 changes: 299 additions & 150 deletions e2j2/cli.py

Large diffs are not rendered by default.

250 changes: 132 additions & 118 deletions e2j2/constants.py
@@ -1,148 +1,162 @@
VERSION = '0.7.0'
ERROR = '** ERROR'
BRIGHT_RED = '\033[1;31m'
RESET_ALL = '\033[00m'
YELLOW = '\033[93m'
GREEN = '\033[0;32m'
LIGHTGREEN = '\033[1;32m'
WHITE = '\033[0;37m'
DESCRIPTION = 'Parse jinja2 templates with enhanced JSON aware environment variables'
VERSION = "0.7.0"
ERROR = "** ERROR"
DESCRIPTION = "Parse jinja2 templates with enhanced JSON aware environment variables"
VAULT_STATUSCODES = {
'200': 'Success',
'204': 'Success, no data',
'400': 'Invalid request',
'403': 'Forbidden',
'404': 'Path not found',
'500': 'Internal server error',
'503': 'Sealed',
"200": "Success",
"204": "Success, no data",
"400": "Invalid request",
"403": "Forbidden",
"404": "Path not found",
"500": "Internal server error",
"503": "Sealed",
}
uri = {'type': 'string', 'format': 'uri'}
scheme = {'type': 'string', 'enum': ['http', 'https']}
host = {'type': 'string', 'format': 'hostname'}
port = {'type': 'number', 'minimum': 0, 'maximum': 65535}
token = {'type': 'string', "minLength": 5}
boolean = {'type': 'boolean'}
uri = {"type": "string", "format": "uri"}
scheme = {"type": "string", "enum": ["http", "https"]}
host = {"type": "string", "format": "hostname"}
port = {"type": "number", "minimum": 0, "maximum": 65535}
token = {"type": "string", "minLength": 5}
boolean = {"type": "boolean"}

CONFIG_SCHEMAS = {
'configfile': {
'type': 'object',
'properties': {
'extension': {'type': 'string'},
'filelist': {'type': 'array', 'items': {'type': 'string'}},
'searchlist': {'type': 'array', 'items': {'type': 'string'}},
'env_whitelist': {'type': 'array', 'items': {'type': 'string'}},
'env_blacklist': {'type': 'array', 'items': {'type': 'string'}},
'watchlist': {'type': 'array', 'items': {'type': 'string'}},
'run': {'type': 'array', 'items': {'type': 'string'}},
'recursive': {'type': 'boolean'},
'no_color': {'type': 'boolean'},
'twopass': {'type': 'boolean'},
'nested_tags': {'type': 'boolean'},
'noop': {'type': 'boolean'},
'test_first': {'type': 'boolean'},
'copy_file_permissions': {'type': 'boolean'},
'stacktrace': {'type': 'boolean'},
'initial_run': {'type': 'boolean'},
'marker_set': {'type': 'string', 'enum': ['{{', '{=', '<=', '[=', '(=']},
'autodetect_marker_set': {'type': 'boolean'},
'block_start': {'type': ['string', 'null']},
'block_end': {'type': ['string', 'null']},
'variable_start': {'type': ['string', 'null']},
'variable_end': {'type': ['string', 'null']},
'comment_start': {'type': ['string', 'null']},
'comment_end': {'type': ['string', 'null']},
'config_start': {'type': ['string', 'null']},
'config_end': {'type': ['string', 'null']},
'splay': {'type': 'number', 'minimum': 0, 'maximum': 900},
"configfile": {
"type": "object",
"properties": {
"extension": {"type": "string"},
"filelist": {"type": "array", "items": {"type": "string"}},
"searchlist": {"type": "array", "items": {"type": "string"}},
"env_whitelist": {"type": "array", "items": {"type": "string"}},
"env_blacklist": {"type": "array", "items": {"type": "string"}},
"watchlist": {"type": "array", "items": {"type": "string"}},
"run": {"type": "array", "items": {"type": "string"}},
"recursive": {"type": "boolean"},
"no_color": {"type": "boolean"},
"twopass": {"type": "boolean"},
"nested_tags": {"type": "boolean"},
"noop": {"type": "boolean"},
"test_first": {"type": "boolean"},
"copy_file_permissions": {"type": "boolean"},
"stacktrace": {"type": "boolean"},
"initial_run": {"type": "boolean"},
"marker_set": {"type": "string", "enum": ["{{", "{=", "<=", "[=", "(="]},
"autodetect_marker_set": {"type": "boolean"},
"block_start": {"type": ["string", "null"]},
"block_end": {"type": ["string", "null"]},
"variable_start": {"type": ["string", "null"]},
"variable_end": {"type": ["string", "null"]},
"comment_start": {"type": ["string", "null"]},
"comment_end": {"type": ["string", "null"]},
"config_start": {"type": ["string", "null"]},
"config_end": {"type": ["string", "null"]},
"splay": {"type": "number", "minimum": 0, "maximum": 900},
},
"additionalProperties": False,
},
'json:': {'type': 'object', 'properties': {'flatten': boolean}},
'jsonfile:': {'type': 'object', 'properties': {'flatten': boolean}},
'consul:': {
'type': 'object',
'properties': {'url': uri, 'scheme': scheme, 'host': host, 'port': port, 'token': token, 'flatten': boolean},
"json:": {"type": "object", "properties": {"flatten": boolean}},
"jsonfile:": {"type": "object", "properties": {"flatten": boolean}},
"consul:": {
"type": "object",
"properties": {
"url": uri,
"scheme": scheme,
"host": host,
"port": port,
"token": token,
"flatten": boolean,
},
"additionalProperties": False,
},
'vault:': {
'type': 'object',
'properties': {
'url': uri,
'scheme': scheme,
'host': host,
'port': port,
'token': token,
'backend': {'type': 'string', 'enum': ['raw', 'kv1', 'kv2']},
'flatten': boolean,
"vault:": {
"type": "object",
"properties": {
"url": uri,
"scheme": scheme,
"host": host,
"port": port,
"token": token,
"backend": {"type": "string", "enum": ["raw", "kv1", "kv2"]},
"flatten": boolean,
},
"additionalProperties": False,
},
'dns:': {
'type': 'object',
'properties': {
'nameservers': {
'type': 'array',
'items': {'type': 'string', 'oneOf': [{'format': 'ipv4'}, {'format': 'ipv6'}]},
"dns:": {
"type": "object",
"properties": {
"nameservers": {
"type": "array",
"items": {
"type": "string",
"oneOf": [{"format": "ipv4"}, {"format": "ipv6"}],
},
},
'port': port,
'type': {'type': 'string', 'enum': ['A', 'AAAA', 'MX', 'SRV']},
'flatten': boolean,
"port": port,
"type": {"type": "string", "enum": ["A", "AAAA", "MX", "SRV"]},
"flatten": boolean,
},
"additionalProperties": False,
},
}

TAGS = ['json:', 'jsonfile:', 'base64:', 'consul:', 'list:', 'file:', 'vault:', 'dns:', 'escape:']
NESTED_TAGS = ['json:', 'jsonfile:', 'list:']
TAGS = [
"json:",
"jsonfile:",
"base64:",
"consul:",
"list:",
"file:",
"vault:",
"dns:",
"escape:",
]
NESTED_TAGS = ["json:", "jsonfile:", "list:"]
MARKER_SETS = {
"{{": {
'block_start': '{%',
'block_end': '%}',
'variable_start': '{{',
'variable_end': '}}',
'comment_start': '{#',
'comment_end': '#}',
'config_start': '{',
'config_end': '}',
"block_start": "{%",
"block_end": "%}",
"variable_start": "{{",
"variable_end": "}}",
"comment_start": "{#",
"comment_end": "#}",
"config_start": "{",
"config_end": "}",
},
"{=": {
'block_start': '{%',
'block_end': '%}',
'variable_start': '{=',
'variable_end': '=}',
'comment_start': '{#',
'comment_end': '#}',
'config_start': '{',
'config_end': '}',
"block_start": "{%",
"block_end": "%}",
"variable_start": "{=",
"variable_end": "=}",
"comment_start": "{#",
"comment_end": "#}",
"config_start": "{",
"config_end": "}",
},
"<=": {
'block_start': '<%',
'block_end': '%>',
'variable_start': '<=',
'variable_end': '=>',
'comment_start': '<#',
'comment_end': '#>',
'config_start': '<',
'config_end': '>',
"block_start": "<%",
"block_end": "%>",
"variable_start": "<=",
"variable_end": "=>",
"comment_start": "<#",
"comment_end": "#>",
"config_start": "<",
"config_end": ">",
},
"[=": {
'block_start': '[%',
'block_end': '%]',
'variable_start': '[=',
'variable_end': '=]',
'comment_start': '[#',
'comment_end': '#]',
'config_start': '[',
'config_end': ']',
"block_start": "[%",
"block_end": "%]",
"variable_start": "[=",
"variable_end": "=]",
"comment_start": "[#",
"comment_end": "#]",
"config_start": "[",
"config_end": "]",
},
"(=": {
'block_start': '(%',
'block_end': '%)',
'variable_start': '(=',
'variable_end': '=)',
'comment_start': '(#',
'comment_end': '#)',
'config_start': '(',
'config_end': ')',
"block_start": "(%",
"block_end": "%)",
"variable_start": "(=",
"variable_end": "=)",
"comment_start": "(#",
"comment_end": "#)",
"config_start": "(",
"config_end": ")",
},
}
59 changes: 48 additions & 11 deletions e2j2/display.py
@@ -1,10 +1,48 @@
import sys
from string import Template
from e2j2 import cache
from e2j2.constants import BRIGHT_RED, GREEN, LIGHTGREEN, YELLOW, WHITE, RESET_ALL

BRIGHT_RED = "\033[1;31m"
RESET_ALL = "\033[00m"
YELLOW = "\033[93m"
GREEN = "\033[0;32m"
LIGHTGREEN = "\033[1;32m"
WHITE = "\033[0;37m"

def stdout(msg):

class Colorize:
red = BRIGHT_RED
reset = RESET_ALL
yellow = YELLOW
green = GREEN
lightgreen = LIGHTGREEN
white = WHITE


_colors = Colorize()


def get_colors():
return _colors


def colorize():
global _colors
_colors = Colorize()


def no_colors():
global _colors
_colors = Colorize()
_colors.red = ''
_colors.reset = ''
_colors.yellow = ''
_colors.green = ''
_colors.lightgreen = ''
_colors.white = ''


def write(msg):
print_at = cache.print_at
increment = cache.increment
counter = cache.log_repeat_log_msg_counter
Expand All @@ -21,16 +59,15 @@ def stdout(msg):
cache.last_log_line = msg


def display(config, text):
colors = config['colors']
def display(text):
template = Template(text)
stdout(
write(
template.substitute(
bright_red=colors['bright_red'],
green=colors['green'],
lightgreen=colors['lightgreen'],
white=colors['white'],
yellow=colors['yellow'],
reset_all=colors['reset_all'],
bright_red=_colors.red,
green=_colors.green,
lightgreen=_colors.lightgreen,
white=_colors.white,
yellow=_colors.yellow,
reset_all=_colors.reset,
)
)

0 comments on commit 4d2071f

Please sign in to comment.