daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. # needs old ruamel.yaml to preserve order/comments/quotes
  4. # pip install 'ruamel.yaml<0.15'
  5.  
  6. import logging
  7. import os
  8. import sys
  9.  
  10. import ruamel.yaml
  11.  
  12. log = logging.getLogger(__name__)
  13.  
  14.  
  15. def main():
  16.     log.debug('sys.argv: %s', sys.argv)
  17.     args = sys.argv[1:]
  18.     for arg in args:
  19.         fo = open(arg, 'r')
  20.         # data = yaml.load(fo)
  21.         data = ruamel.yaml.round_trip_load(fo, preserve_quotes=True)
  22.         old_name = data['name']
  23.         if '.' in old_name and 'namespace' not in data:
  24.             parts = old_name.split('.', 1)
  25.             name = ruamel.yaml.scalarstring.DoubleQuotedScalarString(parts[1])
  26.             namespace = ruamel.yaml.scalarstring.DoubleQuotedScalarString(parts[0])
  27.  
  28.             data['name'] = name
  29.             # insert namespace attr first
  30.             data.insert(0, 'namespace', namespace)
  31.  
  32.         log.debug('type(data): %s %r', type(data), data)
  33.         ruamel.yaml.round_trip_dump(data, sys.stdout, block_seq_indent=2)
  34.  
  35.     return 0
  36.  
  37.  
  38. if __name__ == '__main__':
  39.     logging.basicConfig(level=logging.INFO,
  40.                         format='%(asctime)s %(process)d %(name)s %(funcName)s:%(lineno)d - %(message)s')
  41.     sys.exit(main())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top