[WIP] Add --json flag to format rbt output as json.

Review Request #9245 - Created Oct. 3, 2017 and updated

Brian LeBlanc
RBTools
release-0.7.x
987fe5e...
rbtools, students
When running the `rbt` command from a script, it is difficult to parse
the output log messages in order to get necessary information from the 
command that was run.

This change adds a `--json` flag to all `rbt` commands to change the 
output to json to make command output parsing easier from a script.

All rbt tests passed.

  • 10
  • 0
  • 0
  • 0
  • 10
Description From Last Updated
I think this would be cleaner if we build repository_type_list in one loop (or even better, a list comprehension), and ... David Trowbridge David Trowbridge
We should use six.text_type() instead of str() for future Python 3 compatibility. Here and lots of other places throughout your ... David Trowbridge David Trowbridge
Your output in this command is a little inconsistent. Here you have file_path as the key but below you use ... David Trowbridge David Trowbridge
I suspect we want to change these uses of logging.info() to be print() like the other commands. David Trowbridge David Trowbridge
I suspect we want to change these uses of logging.info() to be print() like the other commands. David Trowbridge David Trowbridge
How does this change the output? David Trowbridge David Trowbridge
By printing here, it looks like we'll end up printing multiple JSON objects out? That doesn't seem correct. Instead of ... David Trowbridge David Trowbridge
Can you put the { on the next line and then indent things within the dict another 4 spaces? David Trowbridge David Trowbridge
} should be on a separate line from the for .... We should also add a trailing comma after the ... David Trowbridge David Trowbridge
It would be nice to pass in the indent parameter to json.dumps so that we get easier-to-read output. David Trowbridge David Trowbridge
Brian LeBlanc
Brian LeBlanc
Brian LeBlanc
Review request changed

Change Summary:

Add json formatted error output to more rbt commands.

Commit:

-5c155c735b6c56743c4c146262afe8f9c83db5db
+987fe5eb378687bda29964142f41ae35a558bfb0

Diff:

Revision 4 (+739 -117)

Show changes

Checks run (2 succeeded)

JSHint passed.
flake8 passed.
David Trowbridge
  1. 
      
  2. rbtools/clients/__init__.py (Diff revision 4)
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    I think this would be cleaner if we build repository_type_list in one loop (or even better, a list comprehension), and then we can either print it out directly as json or loop through it again to print in plain-text:

    repository_type_list = [
        {
            'name': name,
            'tool_name': tool.name,
            'detected': bool(tool.get_repository_info()),
        }
        for name, tool in six.iteritems(SCMCLIENTS)
    ]
    
    if options.json:
        print_json({
            'repository_types': repository_type_list,
        })
    else:
        for repository_type in repository_type_list:
            if repository_type['detected']:
                dectected = '*'
            else:
                dectected = ' '
    
            print(' %s "%s": %s' % (detected,
                                    repository_type['name'],
                                    repository_type['tool_name']))
    
  3. rbtools/commands/api_get.py (Diff revision 4)
     
     

    We should use six.text_type() instead of str() for future Python 3 compatibility. Here and lots of other places throughout your change.

  4. rbtools/commands/attach.py (Diff revision 4)
     
     

    Your output in this command is a little inconsistent. Here you have file_path as the key but below you use path_to_file.

  5. rbtools/commands/login.py (Diff revision 4)
     
     

    I suspect we want to change these uses of logging.info() to be print() like the other commands.

  6. rbtools/commands/logout.py (Diff revision 4)
     
     

    I suspect we want to change these uses of logging.info() to be print() like the other commands.

  7. rbtools/commands/patch.py (Diff revision 4)
     
     

    How does this change the output?

  8. rbtools/commands/post.py (Diff revision 4)
     
     
     
     

    By printing here, it looks like we'll end up printing multiple JSON objects out? That doesn't seem correct. Instead of splatting in a print_json() call anywhere there might be a print() statement we should probably collect the information about what happened and then only print at the very end.

  9. rbtools/commands/status.py (Diff revision 4)
     
     

    Can you put the { on the next line and then indent things within the dict another 4 spaces?

  10. rbtools/commands/status.py (Diff revision 4)
     
     
     

    } should be on a separate line from the for .... We should also add a trailing comma after the ]

  11. rbtools/utils/console.py (Diff revision 4)
     
     

    It would be nice to pass in the indent parameter to json.dumps so that we get easier-to-read output.

  12. 
      
Loading...