├── .github └── workflows │ └── pull-requests.yaml ├── .gitignore ├── LICENSE ├── bin └── jformat ├── examples └── example.json ├── jformat ├── __init__.py ├── main.py ├── reformat.py └── tests │ ├── __init__.py │ ├── test_main.py │ └── test_verify_output.py ├── setup.py └── tox.ini /.github/workflows/pull-requests.yaml: -------------------------------------------------------------------------------- 1 | name: Check Python Interpreters 2 | 3 | on: 4 | pull_request: 5 | # allow to easily run this on demand, without a PR 6 | workflow_dispatch: 7 | 8 | jobs: 9 | 10 | check: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | matrix: 14 | python-version: ['2.7', '3.5', '3.6', '3.8'] 15 | 16 | steps: 17 | - name: Check out the repo 18 | uses: actions/checkout@v2 19 | 20 | - name: Set up Python 21 | uses: actions/setup-python@v2 22 | with: 23 | python-version: ${{ matrix.python-version }} 24 | 25 | - name: install tox 26 | run: pip install tox 27 | 28 | - name: run tox and pytest 29 | run: tox -v 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alfredo Deza 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bin/jformat: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | from jformat import main 4 | 5 | if __name__ == '__main__': 6 | main.main() 7 | -------------------------------------------------------------------------------- /examples/example.json: -------------------------------------------------------------------------------- 1 | { 2 | "arg": 1, 3 | "zetta": 7, 4 | "boto": true} 5 | -------------------------------------------------------------------------------- /jformat/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alfredodeza/jformat/6e513bd4f1b4dda654d71549bc4990fe48498d42/jformat/__init__.py -------------------------------------------------------------------------------- /jformat/main.py: -------------------------------------------------------------------------------- 1 | from . import reformat 2 | import argparse 3 | import os 4 | import pkg_resources 5 | import sys 6 | import logging 7 | 8 | # utilities 9 | 10 | def str_to_bool(val): 11 | """ 12 | Convert a string representation of truth to True or False 13 | 14 | True values are 'y', 'yes', or ''; case-insensitive 15 | False values are 'n', or 'no'; case-insensitive 16 | Raises ValueError if 'val' is anything else. 17 | """ 18 | true_vals = ['yes', 'y', ''] 19 | false_vals = ['no', 'n'] 20 | try: 21 | val = val.lower() 22 | except AttributeError: 23 | val = str(val).lower() 24 | if val in true_vals: 25 | return True 26 | elif val in false_vals: 27 | return False 28 | else: 29 | raise ValueError("Invalid input value: %s" % val) 30 | 31 | 32 | def prompt_bool(question): 33 | try: 34 | return str_to_bool(response) 35 | except ValueError: 36 | print('Valid true responses are: y, yes, ') 37 | print('Valid false responses are: n, no') 38 | print('That response was invalid, please try again') 39 | return prompt_bool(question) 40 | 41 | 42 | def main(argv=None): 43 | argv = argv or sys.argv 44 | parser = argparse.ArgumentParser( 45 | prog='jformat', 46 | description='Format JSON files!', 47 | ) 48 | 49 | parser.add_argument( 50 | '--file', 51 | help='JSON file' 52 | ) 53 | 54 | parser.add_argument( 55 | '--indent', 56 | default=4, 57 | help='Indentation spaces, defaults to 4', 58 | ) 59 | 60 | parser.add_argument( 61 | '--sort', 62 | action='store_true', 63 | help='Sort the keys alphabetically', 64 | ) 65 | 66 | parser.add_argument( 67 | '--inline', 68 | action='store_true', 69 | help='Replace/rewrite the file inline', 70 | ) 71 | 72 | args = parser.parse_args() 73 | 74 | if args.inline and args.file: 75 | with open(args.file, 'r') as _f: 76 | result = reformat.formatter( 77 | _f.read(), 78 | sort_keys=args.sort, 79 | indent=args.indent 80 | ) 81 | 82 | with open(args.file, 'w') as _f: 83 | _f.write(result) 84 | return 85 | 86 | if args.file: 87 | with open(args.file, 'r') as _f: 88 | print( 89 | reformat.formatter( 90 | _f.read(), 91 | sort_keys=args.sort, 92 | indent=args.indent 93 | ) 94 | ) 95 | -------------------------------------------------------------------------------- /jformat/reformat.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | def formatter(string, sort_keys=True, indent=4): 4 | # load incoming string into JSON 5 | loaded_json = json.loads(string) 6 | # dump as string 7 | return json.dumps(loaded_json, sort_keys=sort_keys, indent=indent) 8 | -------------------------------------------------------------------------------- /jformat/tests/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /jformat/tests/test_main.py: -------------------------------------------------------------------------------- 1 | from jformat import main 2 | 3 | 4 | def test_yes_is_true(): 5 | result = main.str_to_bool('yes') 6 | assert result == True 7 | -------------------------------------------------------------------------------- /jformat/tests/test_verify_output.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | class TestLongComparisons(): 4 | 5 | def setup_class(self): 6 | pass 7 | 8 | def teardown_class(self): 9 | pass 10 | 11 | def setup(self): 12 | self.first = "this is one very long string" 13 | 14 | def teardown(self): 15 | pass 16 | 17 | def utility(self, contents=''): 18 | # create the file 19 | pass 20 | 21 | def test_compare_large_strings(self): 22 | self.utility() 23 | second = "this is one very long string" 24 | assert self.first == second 25 | 26 | def test_basic(self): 27 | assert True 28 | 29 | def test_compare_large_lists_to_none(self): 30 | #first = ["this", "is", "one", "very", "long", "list"] 31 | first = self.first.split() 32 | second = ["this", "is", "one", "very", "long", "list"] 33 | assert first != None 34 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name='jformat', 5 | version='0.1', 6 | description='Reformat any file or input into JSON', 7 | author='Alfredo Deza', 8 | author_email='alfredo@deza.pe', 9 | test_suite='jformat', 10 | scripts=['bin/jformat'], 11 | zip_safe=False, 12 | include_package_data=True, 13 | packages=find_packages() 14 | ) 15 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py27, py35, py36, py38 3 | skip_missing_interpreters = true 4 | 5 | [testenv] 6 | deps = 7 | py27: pytest<4.7 8 | py35,py36,py38: pytest==5.3.4 9 | 10 | commands = pytest -v jformat 11 | 12 | [testenv:flake8] 13 | deps=flake8 14 | commands=flake8 jformat 15 | 16 | [flake8] 17 | select=F 18 | --------------------------------------------------------------------------------