Skip to content

Commit

Permalink
handle tabs and spaces in passed JSON values - longstanding issue not…
Browse files Browse the repository at this point in the history
… noted till today
  • Loading branch information
mahtin committed Nov 3, 2022
1 parent d4ac4a4 commit f511729
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def dump_commands_from_web(cf):
else:
sys.stdout.write('%-6s %s\n' % (r['action'], r['cmd']))

def strip_multiline(s):
""" remove leading/trailing tabs/spaces on each line"""
# This hack is needed in order to use yaml.safe_load() on JSON text - tabs are not allowed
return '\n'.join([l.strip() for l in s.splitlines()])

def run_command(cf, method, command, params=None, content=None, files=None):
"""run the command line"""
# remove leading and trailing /'s
Expand Down Expand Up @@ -403,6 +408,9 @@ def do_it(args):
#value = json.loads(value) - changed to yaml code to remove unicode string issues
if yaml is None:
sys.exit('cli4: install yaml support')
# cleanup string before parsing so that yaml.safe.load does not complain about whitespace
# >>> found character '\t' that cannot start any token <<<
value_string = strip_multiline(value_string)
try:
value = yaml.safe_load(value_string)
except yaml.parser.ParserError as e:
Expand Down

0 comments on commit f511729

Please sign in to comment.