fixed colorize auto detection

master
Alexander Graf 4 years ago
parent e46d4737b0
commit e4c83e162d

@ -19,7 +19,7 @@ from flask.cli import FlaskGroup, with_appcontext
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
from . import models from . import models
from .schemas import MailuSchema, get_schema, get_fieldspec, colorize, RenderJSON, HIDDEN from .schemas import MailuSchema, get_schema, get_fieldspec, colorize, canColorize, RenderJSON, HIDDEN
db = models.db db = models.db
@ -351,7 +351,7 @@ def config_import(verbose=0, secrets=False, quiet=False, color=False, update=Fal
verbose = 2 verbose = 2
color_cfg = { color_cfg = {
'color': color or sys.stdout.isatty(), 'color': color or (canColorize and sys.stdout.isatty()),
'lexer': 'python', 'lexer': 'python',
'strip': True, 'strip': True,
} }
@ -598,7 +598,7 @@ def config_export(full=False, secrets=False, color=False, dns=False, output=None
} }
schema = MailuSchema(only=only, context=context) schema = MailuSchema(only=only, context=context)
color_cfg = {'color': color or output.isatty()} color_cfg = {'color': color or (canColorize and output.isatty())}
if as_json: if as_json:
schema.opts.render_module = RenderJSON schema.opts.render_module = RenderJSON

@ -27,9 +27,9 @@ try:
from pygments.lexers.data import YamlLexer from pygments.lexers.data import YamlLexer
from pygments.formatters import get_formatter_by_name from pygments.formatters import get_formatter_by_name
except ModuleNotFoundError: except ModuleNotFoundError:
COLOR_SUPPORTED = False canColorize = False
else: else:
COLOR_SUPPORTED = True canColorize = True
from . import models, dkim from . import models, dkim
@ -99,11 +99,11 @@ def colorize(data, lexer='yaml', formatter='terminal', color=None, strip=False):
""" add ANSI color to data """ """ add ANSI color to data """
if color is None: if color is None:
# autodetect colorize # autodetect colorize
color = COLOR_SUPPORTED color = canColorize
if not color: if not color:
# no color wanted # no color wanted
return data return data
if not COLOR_SUPPORTED: if not canColorize:
# want color, but not supported # want color, but not supported
raise ValueError('Please install pygments to colorize output') raise ValueError('Please install pygments to colorize output')

Loading…
Cancel
Save