Skip to content

Commit

Permalink
add CLI, v1.5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Jul 18, 2016
1 parent e1ed323 commit 4cb5364
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 61 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
include *.py
include *.txt
include *.rst
include bin/*
include MANIFEST.in
prune \#*
48 changes: 19 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ The Python implementation of Hjson is based on [simplejson](https://github.com/s

- or download from https://pypi.python.org/pypi/hjson

## Commandline

```
Usage:
hjson [options]
hjson [options] <input>
hjson (-h | --help)
hjson (-V | --version)
Options:
-h --help Show this screen.
-j Output as formatted JSON.
-c Output as JSON.
-V --version Show version.
```

E.g. `echo '{"json":"obj"}' | hjson`

# Usage

```python
Expand Down Expand Up @@ -69,39 +87,11 @@ Result:
`'["foo", {"bar": ["baz", null, 1.0, 2]}]'`


## From the Commandline

Use `hjson.tool` to validate and convert.

`python -m hjson.tool [FORMAT] [INFILE [OUTFILE]]`

Formats:

- `-h`: print Hjson
- `-j`: print formatted JSON
- `-c`: print compact JSON

E.g. `echo '{"json":"obj"}' | python -m hjson.tool`

# API

[hjson-py documentation](http://laktak.github.io/hjson-py/)

# History

- v1.5.6
- fix dump for comment tokens in keyname
- v1.5.4
- fix decode/encode single JSON value files
- v1.5.3
- fix trailing whitespace in keyname
- v1.5.2
- fix trailing space in quoteless strings
- v1.5.1
- better error messages & root check
- v1.5.0
- Added support for optional root braces
- v1.4.1
- Added documentation, links.
- v1.4.0
- First release.
[see history.md](history.md)
35 changes: 20 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ Installation

- or download from https://pypi.python.org/pypi/hjson

Commandline
-----------

::

Usage:
hjson [options]
hjson [options] <input>
hjson (-h | --help)
hjson (-V | --version)

Options:
-h --help Show this screen.
-j Output as formatted JSON.
-c Output as JSON.
-V --version Show version.

E.g. ``echo '{"json":"obj"}' | hjson``


Usage
=====

Expand Down Expand Up @@ -69,21 +89,6 @@ Note that this is probably not as performant as the simplejson version.
Result: ``'["foo", {"bar": ["baz", null, 1.0, 2]}]'``

From the Commandline
--------------------

Use ``hjson.tool`` to validate and convert.

``python -m hjson.tool [FORMAT] [INFILE [OUTFILE]]``

Formats:

- ``-h``: print Hjson
- ``-j``: print formatted JSON
- ``-c``: print compact JSON

E.g. ``echo '{"json":"obj"}' | python -m hjson.tool``

API
===

Expand Down
3 changes: 3 additions & 0 deletions bin/hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

python -m hjson.tool $*
1 change: 1 addition & 0 deletions bin/hjson.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@python -m hjson.tool %*
20 changes: 20 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# History

- v1.5.8
- add CLI scripts for pip
- v1.5.6
- fix dump for comment tokens in keyname
- v1.5.4
- fix decode/encode single JSON value files
- v1.5.3
- fix trailing whitespace in keyname
- v1.5.2
- fix trailing space in quoteless strings
- v1.5.1
- better error messages & root check
- v1.5.0
- Added support for optional root braces
- v1.4.1
- Added documentation, links.
- v1.4.0
- First release.
2 changes: 1 addition & 1 deletion hjson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"""
from __future__ import absolute_import
__version__ = '1.5.6'
__version__ = '1.5.8'
__all__ = [
'dump', 'dumps', 'load', 'loads',
'dumpJSON', 'dumpsJSON',
Expand Down
55 changes: 40 additions & 15 deletions hjson/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,63 @@
Usage::
$ echo '{"json":"obj"}' | python -m hjson.tool
$ echo '{"json":"obj"}' | hjson
{
"json": "obj"
}
$ echo '{ 1.2:3.4}' | python -m hjson.tool
Expecting property name: line 1 column 2 (char 2)
"""
from __future__ import with_statement
import sys
import hjson
import pkg_resources # part of setuptools

HELP="""Hjson, the Human JSON.
Usage:
hjson [options]
hjson [options] <input>
hjson (-h | --help)
hjson (-V | --version)
Options:
-h --help Show this screen.
-j Output as formatted JSON.
-c Output as JSON.
-V --version Show version.
""";

def showerr(msg):
sys.stderr.write(msg)
sys.stderr.write("\n")

def main():
todo = "-h"
format = 'hjson'
args = []
for arg in sys.argv[1:]:
if arg[0] == '-':
todo = arg
if arg == '-h' or arg == '--help':
showerr(HELP)
return
elif arg == '-j': format = 'json'
elif arg == '-c': format = 'compact'
elif arg == '-V' or arg == '--version':
showerr('Hjson ' + pkg_resources.require("Hjson")[0].version)
return

elif arg[0] == '-':
showerr(HELP)
raise SystemExit('unknown option ' + arg)
else:
args.append(arg)

outfile = sys.stdout
if len(args) == 0:
infile = sys.stdin
outfile = sys.stdout
elif len(args) == 1:
infile = open(args[0], 'r')
outfile = sys.stdout
elif len(args) == 2:
infile = open(args[0], 'r')
outfile = open(args[1], 'w')
else:
raise SystemExit(sys.argv[0] + " {-h|-j|-c} [infile [outfile]]")
showerr(HELP)
raise SystemExit('unknown options')

with infile:
try:
Expand All @@ -42,9 +67,9 @@ def main():
raise SystemExit(sys.exc_info()[1])

with outfile:
if todo == '-j':
hjson.dumpJSON(obj, outfile, use_decimal=True, indent=" ")
elif todo == '-c':
if format == 'json':
hjson.dumpJSON(obj, outfile, use_decimal=True, indent=' ')
elif format == 'compact':
hjson.dumpJSON(obj, outfile, use_decimal=True, separators=(',', ':'))
else:
hjson.dump(obj, outfile, use_decimal=True)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DistutilsPlatformError

IS_PYPY = hasattr(sys, 'pypy_translation_info')
VERSION = '1.5.6'
VERSION = '1.5.8'
DESCRIPTION = "JSON for Humans. A configuration file format with relaxed syntax, fewer mistakes and more comments."

with open('README.rst', 'r') as f:
Expand Down Expand Up @@ -72,6 +72,7 @@ def run_setup():
license="MIT License",
packages=['hjson', 'hjson.tests'],
platforms=['any'],
scripts=['bin/hjson', 'bin/hjson.cmd',],
**kw)

run_setup()

0 comments on commit 4cb5364

Please sign in to comment.