Skip to content

Commit

Permalink
Merge pull request #44 from kellyjonbrazil/dev
Browse files Browse the repository at this point in the history
Dev v1.5.0
  • Loading branch information
kellyjonbrazil committed Dec 9, 2021
2 parents 0afc225 + b7a0aed commit 25bfde8
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 495 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: [3.6, 3.7, 3.8, 3.9, 3.10.0]
python-version: [3.7, 3.8, 3.9, 3.10.0]

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
jello changelog

20211208 v1.5.0
- Note to Package Maintainers: `/jello/man/jello.1` no longer exists. Please use `/man/jello.1`
- Wrap warning messages that exceed the terminal width
- Add support for the NO_COLOR environment variable to set mono (http://no-color.org/)
- Add -C option (opts.force_color) to force color output even when using pipes (overrides -m and NO_COLOR)

20211129 v1.4.6
- Note to Package Maintainers:
TLDR: `/jello/man/jello.1` is deprecated and only `/man/jello.1` should be used.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ cat data.json | jello '_["foo"]'

#### Options
- `-c` compact print JSON output instead of pretty printing
- `-C` force color output even when using pipes (overrides `-m` and the `NO_COLOR` env variable)
- `-i` initialize environment with a custom config file
- `-l` lines output (suitable for bash array assignment)
- `-m` monochrome output
Expand Down Expand Up @@ -153,6 +154,10 @@ or
JELLO_COLORS=default,default,default,default
```

### Disable Colors via Environment Variable
You can set the [`NO_COLOR`](http://no-color.org/) environment variable to any value to disable color output in `jello`. Note that using the `-C` option to force color output will override both the `NO_COLOR` environment variable and the `-m` option.

### Advanced Usage
Here is more [Advanced Usage](https://github.com/kellyjonbrazil/jello/blob/master/ADVANCED_USAGE.md) information.

> To accelerate filter development and testing, try [`jellex`](https://github.com/kellyjonbrazil/jellex). `jellex` is an interactive front-end TUI built on `jello` that allows you to see your filter results in real-time along with any errors.
Expand Down
2 changes: 1 addition & 1 deletion jello/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""jello - query JSON at the command line with python syntax"""


__version__ = '1.4.6'
__version__ = '1.5.0'
AUTHOR = 'Kelly Brazil'
WEBSITE = 'https://github.com/kellyjonbrazil/jello'
COPYRIGHT = '© 2020-2021 Kelly Brazil'
Expand Down
14 changes: 10 additions & 4 deletions jello/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def print_help():
Usage: cat data.json | jello [OPTIONS] [QUERY]
-c compact JSON output
-C force color output even when using pipes (overrides -m)
-i initialize environment with .jelloconf.py in ~ (linux) or %appdata% (Windows)
-l output as lines suitable for assignment to a bash array
-m monochrome output
Expand Down Expand Up @@ -130,7 +131,8 @@ def main(data=None, query='_'):
opts.compact = opts.compact or 'c' in options
opts.initialize = opts.initialize or 'i' in options
opts.lines = opts.lines or 'l' in options
opts.mono = opts.mono or 'm' in options
opts.force_color = opts.force_color or 'C' in options
opts.mono = opts.mono or ('m' in options or bool(os.getenv('NO_COLOR')))
opts.nulls = opts.nulls or 'n' in options
opts.raw = opts.raw or 'r' in options
opts.schema = opts.schema or 's' in options
Expand Down Expand Up @@ -174,22 +176,26 @@ def main(data=None, query='_'):
except Exception as e:
print_exception(e, list_dict_data, query, ex_type='Query')

# reset opts.mono after pyquery since initialization in pyquery can change values
if opts.force_color:
opts.mono = False

# Create and print schema or JSON/JSON-Lines/Lines
output = ''
try:
if opts.schema:
schema = Schema()
output = schema.create_schema(response)

if not opts.mono and sys.stdout.isatty():
if not opts.mono and (sys.stdout.isatty() or opts.force_color):
schema.set_colors()
output = schema.color_output(output)

else:
json_out = Json()
output = json_out.create_json(response)

if not opts.mono and not opts.raw and sys.stdout.isatty():
if (not opts.mono and not opts.raw) and (sys.stdout.isatty() or opts.force_color):
json_out.set_colors()
output = json_out.color_output(output)

Expand All @@ -200,4 +206,4 @@ def main(data=None, query='_'):


if __name__ == '__main__':
main()
pass
62 changes: 50 additions & 12 deletions jello/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import ast
import json
import shutil
from textwrap import TextWrapper
from jello.dotmap import DotMap

# make pygments import optional
Expand All @@ -28,6 +30,7 @@ class opts:
nulls = None
raw = None
lines = None
force_color = None
mono = None
schema = None
types = None
Expand All @@ -40,10 +43,10 @@ class opts:
class JelloTheme:
if PYGMENTS_INSTALLED:
theme = {
Name: f'bold ansiblue',
Keyword: f'ansibrightblack',
Number: f'ansimagenta',
String: f'ansigreen'
Name: 'bold ansiblue',
Keyword: 'ansibrightblack',
Number: 'ansimagenta',
String: 'ansigreen'
}

def set_colors(self):
Expand Down Expand Up @@ -85,7 +88,7 @@ def set_colors(self):

# if there is an issue with the env variable, just set all colors to default and move on
if input_error:
print('jello: Warning: could not parse JELLO_COLORS environment variable\n', file=sys.stderr)
warning_message(['could not parse JELLO_COLORS environment variable'])
color_list = ['default', 'default', 'default', 'default']

if PYGMENTS_INSTALLED:
Expand Down Expand Up @@ -191,12 +194,9 @@ def _schema_gen(self, src, path='_'):
self._schema_gen(item, path=f'{path}.{k}[{i}]')

elif isinstance(v, dict):
if not opts.mono:
k = f'{k}'
self._schema_gen(v, path=f'{path}.{k}')

else:
k = f'{k}'
val = json.dumps(v, ensure_ascii=False)
val_type = ''
padding = ''
Expand Down Expand Up @@ -338,6 +338,38 @@ def load_json(data):
return json_dict


def warning_message(message_lines):
"""
Prints warning message for non-fatal issues. The first line is prepended with
'jello: Warning - ' and subsequent lines are indented. Wraps text as needed
based on the terminal width.
Parameters:
message: (list) list of string lines
Returns:
None - just prints output to STDERR
"""
columns = shutil.get_terminal_size().columns

first_wrapper = TextWrapper(width=columns, subsequent_indent=' ' * 12)
next_wrapper = TextWrapper(width=columns, initial_indent=' ' * 8,
subsequent_indent=' ' * 12)

first_line = message_lines.pop(0)
first_str = f'jello: Warning - {first_line}'
first_str = first_wrapper.fill(first_str)
print(first_str, file=sys.stderr)

for line in message_lines:
if line == '':
continue
message = next_wrapper.fill(line)
print(message, file=sys.stderr)


def pyquery(_θ_data, _θ_query):
"""Sets options and runs the user's query. This function uses odd variable names so they don't
collide with user defined names."""
Expand Down Expand Up @@ -380,9 +412,9 @@ def pyquery(_θ_data, _θ_query):
_θ_i_block = ast.parse(_θ_jelloconf, mode='exec')
exec(compile(_θ_i_block, '<string>', mode='exec'))

for _θ_option in [opts.compact, opts.raw, opts.lines, opts.nulls, opts.mono, opts.schema, opts.types]:
for _θ_option in [opts.compact, opts.raw, opts.lines, opts.nulls, opts.force_color, opts.mono, opts.schema, opts.types]:
if not isinstance(_θ_option, bool) and _θ_option is not None:
opts.compact = opts.raw = opts.lines = opts.nulls = opts.mono = opts.schema = opts.types = False
opts.compact = opts.raw = opts.lines = opts.nulls = opts.force_color = opts.mono = opts.schema = opts.types = False
_θ_warn_options = True

for _θ_color_config in [opts.keyname_color, opts.keyword_color, opts.number_color, opts.string_color]:
Expand All @@ -395,11 +427,17 @@ def pyquery(_θ_data, _θ_query):
_θ_warn_colors = True

if _θ_warn_options:
print(f'Jello: Warning: Options must be set to True or False in {_θ_conf_file}\n Unsetting all options.\n', file=sys.stderr)
warning_message([
f'Options must be set to True or False in {_θ_conf_file}',
'Unsetting all options.'
])

if _θ_warn_colors:
_θ_valid_colors_string = ', '.join(_θ_valid_colors)
print(f'Jello: Warning: Colors must be set to one of: {_θ_valid_colors_string} in {_θ_conf_file}\n Unsetting all colors.\n', file=sys.stderr)
warning_message([
f'Colors must be set to one of: {_θ_valid_colors_string} in {_θ_conf_file}',
'Unsetting all colors.'
])

# clean up variables
del _θ_color_config
Expand Down

0 comments on commit 25bfde8

Please sign in to comment.