Example #1
0
 def test_json_formatter_converts_to_expected_string(self):
     formatter = DataFrameOutputFormatter(OutputFormat.JSON)
     output = formatter.get_formatted_output(self.test_df)
     assert (
         "".join(output) ==
         '{\n    "string_column": "string1",\n    "int_column": 42,\n    "null_column": null\n}\n{\n    "string_column": "string2",\n    "int_column": 43,\n    "null_column": null\n}\n'
     )
Example #2
0
 def test_raw_formatter_converts_to_expected_string(self):
     formatter = DataFrameOutputFormatter(OutputFormat.RAW)
     output = formatter.get_formatted_output(self.test_df)
     assert (
         "".join(output) ==
         '{"string_column": "string1", "int_column": 42, "null_column": null}\n{"string_column": "string2", "int_column": 43, "null_column": null}\n'
     )
Example #3
0
def _list(state, format=None):
    """List available saved searches."""
    formatter = DataFrameOutputFormatter(format)
    response = state.sdk.securitydata.savedsearches.get()
    saved_searches_df = DataFrame(response["searches"])
    formatter.echo_formatted_dataframes(saved_searches_df,
                                        columns=["name", "id", "notes"])
Example #4
0
 def test_json_formatter_converts_to_expected_string(self):
     formatter = DataFrameOutputFormatter(OutputFormat.JSON)
     output = formatter.get_formatted_output(self.test_df)
     assert (
         output.strip() ==
         '{"string_column":"string1","int_column":42,"null_column":null}\n{"string_column":"string2","int_column":43,"null_column":null}'
     )
Example #5
0
    def test_format_when_unknown_format_raises_value_error(self):
        with pytest.raises(Code42CLIError):
            DataFrameOutputFormatter("NOT_A_FORMAT")

        with pytest.raises(Code42CLIError):
            formatter = DataFrameOutputFormatter("JSON")
            formatter.output_format = "NOT_A_FORMAT"
            list(formatter.get_formatted_output(self.test_df))
Example #6
0
    def test_iter_rows_calls_checkpoint_func_on_every_row_in_df(self):
        checkpointed = []

        def checkpoint(event):
            checkpointed.append(event["string_column"])

        formatter = DataFrameOutputFormatter(None, checkpoint_func=checkpoint)
        list(formatter.iter_rows(self.test_df))
        assert checkpointed == list(self.test_df.string_column.values)
Example #7
0
def show_user(state, username, include_legal_hold_membership, format):
    """Show user details."""
    columns = (["userUid", "status", "username", "orgUid", "roles"]
               if format == OutputFormat.TABLE else None)
    response = state.sdk.users.get_by_username(username, incRoles=True)
    df = DataFrame.from_records(response["users"], columns=columns)
    if include_legal_hold_membership and not df.empty:
        df = _add_legal_hold_membership_to_user_dataframe(state.sdk, df)
    formatter = DataFrameOutputFormatter(format)
    formatter.echo_formatted_dataframes(df)
Example #8
0
    def test_echo_formatted_dataframes_prints_no_results_found_when_dataframes_empty(
            self, fmt, capsys):
        formatter = DataFrameOutputFormatter(fmt)

        def empty_results():
            yield DataFrame()

        formatter.echo_formatted_dataframes(empty_results())
        captured = capsys.readouterr()
        assert "No results found." in captured.out
Example #9
0
    def test_get_formatted_ouput_calls_checkpoint_func_on_every_row_in_df(
            self, fmt):
        checkpointed = []

        def checkpoint(event):
            checkpointed.append(event["string_column"])

        formatter = DataFrameOutputFormatter(fmt, checkpoint_func=checkpoint)
        list(formatter.get_formatted_output(self.test_df))
        assert checkpointed == list(self.test_df.string_column.values)
Example #10
0
def list_users(state, org_uid, role_name, active, inactive, format):
    """List users in your Code42 environment."""
    if inactive:
        active = False
    role_id = _get_role_id(state.sdk, role_name) if role_name else None
    columns = (["userUid", "status", "username", "orgUid"]
               if format == OutputFormat.TABLE else None)
    df = _get_users_dataframe(state.sdk, columns, org_uid, role_id, active)
    if df.empty:
        click.echo("No results found.")
    else:
        formatter = DataFrameOutputFormatter(format)
        formatter.echo_formatted_dataframe(df)
Example #11
0
 def test_table_formatter_converts_to_expected_string(self):
     formatter = DataFrameOutputFormatter(OutputFormat.TABLE)
     output = list(formatter.get_formatted_output(self.test_df))
     assert "string_column" in output[0]
     assert "int_column" in output[0]
     assert "null_column" in output[0]
     assert "string1" in output[1]
     assert "42" in output[1]
     assert "null" not in output[1]
     assert "NaN" not in output[1]
     assert "string2" in output[2]
     assert "43" in output[2]
     assert "null" not in output[2]
     assert "NaN" not in output[2]
Example #12
0
def list_backup_sets(
    state, active, org_uid, include_usernames, format,
):
    """Outputs a list of all devices."""
    columns = ["guid", "userUid"]
    devices_dataframe = _get_device_dataframe(state.sdk, columns, active, org_uid)
    if include_usernames:
        devices_dataframe = _add_usernames_to_device_dataframe(
            state.sdk, devices_dataframe
        )
    devices_dataframe = _add_backup_set_settings_to_dataframe(
        state.sdk, devices_dataframe
    )
    formatter = DataFrameOutputFormatter(format)
    formatter.echo_formatted_dataframe(devices_dataframe)
Example #13
0
def list_backup_sets(
    state, active, inactive, org_uid, include_usernames, format,
):
    """Get information about many devices and their backup sets."""
    if inactive:
        active = False
    columns = ["guid", "userUid"]
    df = _get_device_dataframe(state.sdk, columns, active, org_uid)
    if include_usernames:
        df = _add_usernames_to_device_dataframe(state.sdk, df)
    df = _add_backup_set_settings_to_dataframe(state.sdk, df)
    if df.empty:
        click.echo("No results found.")
    else:
        formatter = DataFrameOutputFormatter(format)
        formatter.echo_formatted_dataframe(df)
Example #14
0
def list_devices(
    state,
    active,
    days_since_last_connected,
    drop_most_recent,
    org_uid,
    include_backup_usage,
    include_usernames,
    include_settings,
    format,
):
    """Outputs a list of all devices."""
    columns = [
        "computerId",
        "guid",
        "name",
        "osHostname",
        "status",
        "lastConnected",
        "productVersion",
        "osName",
        "osVersion",
        "userUid",
    ]
    devices_dataframe = _get_device_dataframe(
        state.sdk, columns, active, org_uid, include_backup_usage
    )
    if drop_most_recent:
        devices_dataframe = _drop_n_devices_per_user(
            devices_dataframe, drop_most_recent
        )
    if days_since_last_connected:
        devices_dataframe = _drop_devices_which_have_not_connected_in_some_number_of_days(
            devices_dataframe, days_since_last_connected
        )
    if include_settings:
        devices_dataframe = _add_settings_to_dataframe(state.sdk, devices_dataframe)
    if include_usernames:
        devices_dataframe = _add_usernames_to_device_dataframe(
            state.sdk, devices_dataframe
        )
    formatter = DataFrameOutputFormatter(format)
    formatter.echo_formatted_dataframe(devices_dataframe)
Example #15
0
def list_users(
    state,
    org_uid,
    role_name,
    active,
    inactive,
    include_legal_hold_membership,
    include_roles,
    format,
):
    """List users in your Code42 environment."""
    if inactive:
        active = False
    role_id = _get_role_id(state.sdk, role_name) if role_name else None
    columns = (["userUid", "status", "username", "orgUid"]
               if format == OutputFormat.TABLE else None)
    if include_roles and columns:
        columns.append("roles")
    df = _get_users_dataframe(state.sdk, columns, org_uid, role_id, active,
                              include_roles)
    if include_legal_hold_membership:
        df = _add_legal_hold_membership_to_user_dataframe(state.sdk, df)
    formatter = DataFrameOutputFormatter(format)
    formatter.echo_formatted_dataframes(df)
Example #16
0
 def test_echo_formatted_dataframes_uses_pager_when_len_rows_gt_threshold_const(
         self, mocker):
     mock_echo = mocker.patch("click.echo")
     mock_pager = mocker.patch("click.echo_via_pager")
     formatter = DataFrameOutputFormatter(OutputFormat.TABLE)
     rows_len = output_formats_module.OUTPUT_VIA_PAGER_THRESHOLD + 1
     big_df = DataFrame([{"column": val} for val in range(rows_len)])
     small_df = DataFrame([{"column": val} for val in range(5)])
     formatter.echo_formatted_dataframes(big_df)
     formatter.echo_formatted_dataframes(small_df)
     assert mock_echo.call_count == 1
     assert mock_pager.call_count == 1
Example #17
0
def list_devices(
    state,
    active,
    inactive,
    org_uid,
    include_backup_usage,
    include_usernames,
    include_settings,
    include_legal_hold_membership,
    include_total_storage,
    exclude_most_recently_connected,
    last_connected_after,
    last_connected_before,
    created_after,
    created_before,
    format,
):
    """Get information about many devices."""
    if inactive:
        active = False
    columns = [
        "computerId",
        "guid",
        "name",
        "osHostname",
        "status",
        "lastConnected",
        "creationDate",
        "productVersion",
        "osName",
        "osVersion",
        "userUid",
    ]
    df = _get_device_dataframe(
        state.sdk,
        columns,
        active,
        org_uid,
        (include_backup_usage or include_total_storage),
    )
    if last_connected_after:
        df = df.loc[to_datetime(df.lastConnected) > last_connected_after]
    if last_connected_before:
        df = df.loc[to_datetime(df.lastConnected) < last_connected_before]
    if created_after:
        df = df.loc[to_datetime(df.creationDate) > created_after]
    if created_before:
        df = df.loc[to_datetime(df.creationDate) < created_before]
    if exclude_most_recently_connected:
        most_recent = (
            df.sort_values(["userUid", "lastConnected"], ascending=False)
            .groupby("userUid")
            .head(exclude_most_recently_connected)
        )
        df = df.drop(most_recent.index)
    if include_total_storage:
        df = _add_storage_totals_to_dataframe(df, include_backup_usage)
    if include_settings:
        df = _add_settings_to_dataframe(state.sdk, df)
    if include_usernames:
        df = _add_usernames_to_device_dataframe(state.sdk, df)
    if include_legal_hold_membership:
        df = _add_legal_hold_membership_to_device_dataframe(state.sdk, df)
    if df.empty:
        click.echo("No results found.")
    else:
        formatter = DataFrameOutputFormatter(format)
        formatter.echo_formatted_dataframe(df)
Example #18
0
 def test_csv_formatter_converts_to_expected_string(self):
     formatter = DataFrameOutputFormatter(OutputFormat.CSV)
     output = formatter.get_formatted_output(self.test_df)
     assert (
         "".join(output) ==
         "string_column,int_column,null_column\nstring1,42,\nstring2,43,\n")
Example #19
0
 def test_format_when_unknown_format_raises_value_error(self):
     with pytest.raises(ValueError):
         formatter = DataFrameOutputFormatter("NOT_A_FORMAT")
         formatter.get_formatted_output(self.test_df)
Example #20
0
 def test_format_when_none_passed_defaults_to_table(self):
     formatter = DataFrameOutputFormatter(output_format=None)
     assert formatter.output_format == OutputFormat.TABLE
Example #21
0
 def test_table_formatter_converts_to_expected_string(self):
     formatter = DataFrameOutputFormatter(OutputFormat.TABLE)
     output = formatter.get_formatted_output(self.test_df)
     assert output == ("string_column  int_column null_column\n"
                       "      string1          42            \n"
                       "      string2          43            ")