JSON wrapper which inputs to a dictionary and outputs a JSON object

Review Request #11521 — Created March 20, 2021 and submitted

Information

RBTools
master

Reviewers

JSON wrapper class which inputs to a dictionary and outputs a JSON object. Class
is defined in rbtools/commands/__init__.py Command class. New command --json
added to global options, which can be added to any command. When --json is active
run_from_argv will print the json object using Python's json library.

For a full list of proposed JSON objects for each command see:
https://www.notion.so/reviewboard/a8cd6ee32806415fb7bba484d630863e?v=25fb1c9182064421ab17e55606da765b

Passed all tests in Command. Added new tests in test_main.py for initializing
wrapper, adding key value pairs to dictionary, getting correct JSON string from
dictionary, and printing to output stream with correct JSON string.

Summary ID
merged in Output Wrapper Object from r#11401
fc3442b40f7688eae2dc8943c3905de1189aba49
added new method to print JSON to stream object and tests to ensure it works properly
93a7f52f930aec3fe39c3d0d9c0f15ee4a14922c
added new global command --json
8d6ea988b098794628c133cbbcd42f524293d873
added conditional at the end of command cycle to print JSON is enabled
312b2d649dc71fa01637c427209d5f972c193402
added method to append to classes and tests to ensure items are appended correctly
9ecb2ddb435c1889c5b000d2ad2d57f7bc3f04b8
added new method to add errors to existing errors key or create a new one
99df72a53c52f7b839decff237fa694d19baa190
Fixed issues with extra white spaces in files from Review Bot
505c93c8f2a525fc8a8114e37b69ccdf4f283b9d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
1c4e3367bd82af470f4889056c35e1e137b9d9ff
Fixed minor formatting issues in test_main.py
c110d05aca25324d53b55ed678169610aa9a7c07
Updated JSONOutput docstrings to format with guidelines
d58d4e8f8bbe7b1c4486479d2d4ebc25cb545010
Updated docstrings in JSONOutputTests in test_main.py to comply with guidelines
29daf66756b65033e122b5cb2466e588ad4a7635
Checking if there are any errors in json to decide status
a0ee53dabe7e1c7894412281719b38db1410f16f
Fixed trailing white space in test_main.test_hson_wrapper_append
40c9ab291084fd36bed86e74761ed579706a612c
Fixed blank lines with spaces in __init__ Command.run_from_argv
fe0dd7c90d293e3885890f4ca3be0dafe506a63a
Added any CommandError caught to errors key
b662b573e9452277ea177d990b2f8d750028f2f2
Fixed bug where test_main.py was imported JSONWrapper instead of JSONOutput
412f531a218f3c9bf2eac16ebb765ef74d0bcb30
Disabling standard output and error when --json option is enabled
4225f35a6f49eb25af995c27b7cdbc1978a31edc
Casting errors as string before adding to JSON object
f7e7e1ae2398aaa2aec3e63dcad942edc72a8b3e
Description From Last Updated

W293 blank line contains whitespace

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

E303 too many blank lines (2)

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

E501 line too long (80 > 79 characters)

reviewbotreviewbot

This class and all the methods inside need docstrings. Perhaps instead of "Wrapper" (it's not really wrapping anything), just call …

daviddavid

W293 blank line contains whitespace

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

Let's make the help be something like "Output results as JSON data instead of text"

daviddavid

W293 blank line contains whitespace

reviewbotreviewbot

This comment isn't necessary--the code is pretty self explanatory.

daviddavid

Looks like this still needs to be done. But initialize_scm_tool isn't the right place to do it.

daviddavid

Let's do import kgb and inherit from kgb.SpyAgency

daviddavid

These two lines can be combined: from rbtools.commands import JSONWrapper, main as rbt_main

daviddavid

It might be worth defining a setUp method that sets self.json = JSONWrapper(sys.stdout). That way you don't have to do …

daviddavid

E303 too many blank lines (2)

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

E501 line too long (80 > 79 characters)

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

W291 trailing whitespace

reviewbotreviewbot

E127 continuation line over-indented for visual indent

reviewbotreviewbot

E501 line too long (83 > 79 characters)

reviewbotreviewbot

W291 trailing whitespace

reviewbotreviewbot

W291 trailing whitespace

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

W291 trailing whitespace

reviewbotreviewbot

W293 blank line contains whitespace

reviewbotreviewbot

I think it might be better to do self.json.add_error(str(e)) because most of the errors raised (ex. CommandError, ApiError) are not …

amohapatraamohapatra
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

ryankang
Review request changed
Commits:
Summary ID
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

david
  1. 
      
  2. rbtools/commands/__init__.py (Diff revision 2)
     
     
    Show all issues

    This class and all the methods inside need docstrings.

    Perhaps instead of "Wrapper" (it's not really wrapping anything), just call it something like "JSONOutput"?

  3. rbtools/commands/__init__.py (Diff revision 2)
     
     
    Show all issues

    Let's make the help be something like "Output results as JSON data instead of text"

  4. rbtools/commands/__init__.py (Diff revision 2)
     
     
    Show all issues

    This comment isn't necessary--the code is pretty self explanatory.

  5. rbtools/commands/__init__.py (Diff revision 2)
     
     
     
     
     
    Show all issues

    Looks like this still needs to be done. But initialize_scm_tool isn't the right place to do it.

    1. Would you recommend putting it at the start of run_from_argv instead?

  6. rbtools/commands/tests/test_main.py (Diff revision 2)
     
     
    Show all issues

    Let's do import kgb and inherit from kgb.SpyAgency

  7. rbtools/commands/tests/test_main.py (Diff revision 2)
     
     
     
    Show all issues

    These two lines can be combined:

    from rbtools.commands import JSONWrapper, main as rbt_main
    
  8. rbtools/commands/tests/test_main.py (Diff revision 2)
     
     
    Show all issues

    It might be worth defining a setUp method that sets self.json = JSONWrapper(sys.stdout). That way you don't have to do it individually in each test.

  9. 
      
ryankang
ryankang
Review request changed
Commits:
Summary ID
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

ryankang
ryankang
Review request changed
Commits:
Summary ID
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

ryankang
Review request changed
Commits:
Summary ID
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b
Updated docstrings in JSONOutputTests in test_main.py to comply with guidelines
b3970c176e63810f47ad5e642d947259d3d54d7a

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

ryankang
Review request changed
Commits:
Summary ID
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b
Updated docstrings in JSONOutputTests in test_main.py to comply with guidelines
b3970c176e63810f47ad5e642d947259d3d54d7a
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b
Updated docstrings in JSONOutputTests in test_main.py to comply with guidelines
b3970c176e63810f47ad5e642d947259d3d54d7a
Checking if there are any errors in json to decide status
9623fa15b41db8b60b8ed18fba179b5d94bd0d31

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

ryankang
Review request changed
Commits:
Summary ID
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b
Updated docstrings in JSONOutputTests in test_main.py to comply with guidelines
b3970c176e63810f47ad5e642d947259d3d54d7a
Checking if there are any errors in json to decide status
9623fa15b41db8b60b8ed18fba179b5d94bd0d31
added JSON wrapper class to main.py which inputs to a dictionary and outputs a JSON object. Added tests for JSON wrapper functionality in test_main.py
9bfcd1b986f5b42603b0809c7d78b638f6cee848
added new method to print JSON to stream object and tests to ensure it works properly
fbd48ac58a17e2f8d9feb7a219f66d061eed989c
added new global command --json
9cf5b3f5bb931691494dfb7922a5a0c910ea8a2d
added conditional at the end of command cycle to print JSON is enabled
2876f2452d8b0655055e362ef6684de8b8e32ae7
added method to append to classes and tests to ensure items are appended correctly
bcf004f831c02cbb35dfb5dc3d61b5e3406af2b5
added new method to add errors to existing errors key or create a new one
195b6bf5193604d04511431c6761790a033fb74f
Fixed issues with extra white spaces in files from Review Bot
6bb1b7d1a5798e5c1b547cc7b705b848b1a4d13d
Added docstrings to JSONOutput class and changed name from JSONWrapper to JSONOutput
Improved command hint for --json Fixed header in test_main to import kgb and concatonated imports from rbtools.commands Created _setup() method in JSONOutputTests test class to standardize JSONOutput initialization
75af994a92e6b1ebae91f8a5da0ce5cf4aadc867
Fixed minor formatting issues in test_main.py
572bb4ce5998732a35c0380c17bf31d6cef461ac
Updated JSONOutput docstrings to format with guidelines
0e811e589d36ff193279d6e6d8a01a67d9b2074b
Updated docstrings in JSONOutputTests in test_main.py to comply with guidelines
b3970c176e63810f47ad5e642d947259d3d54d7a
Checking if there are any errors in json to decide status
9623fa15b41db8b60b8ed18fba179b5d94bd0d31
Fixed trailing white space in test_main.test_hson_wrapper_append
dc879872ed0c58fb6c82c93663a1c3a54e32e34b

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

ryankang
ryankang
amohapatra
  1. 
      
  2. rbtools/commands/__init__.py (Diff revision 11)
     
     
    Show all issues

    I think it might be better to do self.json.add_error(str(e)) because most of the errors raised (ex. CommandError, ApiError) are not JSON serializable.

  3. 
      
ryankang
ryankang
ryankang
ryankang
ryankang
ryankang
david
Review request changed
Status:
Completed