k98kurz

yamljson.py

Jun 3rd, 2022 (edited)
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/bin/python3
  2. from __future__ import annotations
  3. import json
  4. from sys import argv, stdin
  5. import yaml
  6.  
  7.  
  8. def get_input(infile: str = None) -> str:
  9.     """Handles getting input."""
  10.     if not infile:
  11.         return stdin.read()
  12.     else:
  13.         with open(infile) as f:
  14.             return f.read()
  15.  
  16.  
  17. def set_output(data: str, outfile: str = None) -> None:
  18.     """Handles setting output."""
  19.     if not outfile:
  20.         print(data)
  21.     else:
  22.         with open(outfile, 'w') as f:
  23.             f.write(data)
  24.  
  25.  
  26. def y2j(data: str) -> str:
  27.     """Convert from yaml to json."""
  28.     data = yaml.safe_load(data)
  29.     return json.dumps(data, indent=2)
  30.  
  31.  
  32. def j2y(data: str) -> str:
  33.     """Convert from json to yaml."""
  34.     data = json.loads(data)
  35.     return yaml.dump(data)
  36.  
  37.  
  38. def usage(filename: str) -> None:
  39.     print(f'usage: {filename} [y2j|j2y] [infile] [outfile]')
  40.     print('\ty2j: convert yaml to json')
  41.     print('\tj2y: convert json to yaml')
  42.     print('\tinfile: the input file; if excluded, stdin will be used')
  43.     print('\toutfile: the output file; if excluded, stdout will be used')
  44.  
  45.  
  46. def main() -> None:
  47.     if len(argv) < 2:
  48.         return usage(argv[0])
  49.  
  50.     infile = argv[2] if len(argv) > 2 else None
  51.     outfile = argv[3] if len(argv) > 3 else None
  52.  
  53.     if argv[1] == 'y2j':
  54.         set_output(y2j(get_input(infile)), outfile)
  55.     if argv[1] == 'j2y':
  56.         set_output(j2y(get_input(infile)), outfile)
  57.  
  58.  
  59. def license():
  60.     """Copyleft (c) 2022 k98kurz
  61.  
  62.        Permission to use, copy, modify, and/or distribute this software
  63.        for any purpose with or without fee is hereby granted, provided
  64.        that the above copyleft notice and this permission notice appear in
  65.        all copies.
  66.  
  67.        THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  68.        WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  69.        WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  70.        AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  71.        CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  72.        OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  73.        NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  74.        CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  75.    """
  76.     return license.__doc__
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     main()
  81.  
Add Comment
Please, Sign In to add comment