Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible implementation for reporter/output handlers #13897

Draft
wants to merge 16 commits into
base: feature/reporters-backends
Choose a base branch
from

Conversation

travishathaway
Copy link
Contributor

@travishathaway travishathaway commented May 7, 2024

Description

This pull request demonstrates not only how reporter and output handlers are defined as plugin hooks within conda but also gives a practical example of their usage in the conda info command. Along with the code changes itself, below is an explanation of this API and how it will be used in conda.

ReporterManager API

When interacting with this API, most conda contributors will be using an instance of the ReporterManager class. This class is a singleton and is therefore accessed via the get_reporter_manager function to ensure we only create one object per program run.

The ReporterManager class has a render method which accepts a heterogeneous object that will eventually be converted to a string and rendered to one of the configured output handlers (e.g. stdout). Optionally, callers can specify a component that will use a custom rendering function defined by the reporter handler. Currently available rendering functions only include "detail_view" and "envs_list", but this will be expanded as continue to incorporate this into more areas of the code.

Because the "component" argument is optional, the default behavior of the render method will be to either try to convert the object to a string and pass it to the registered output handler, or if it is the JSON reporter handler, it will attempt to render the object as a JSON string and pass it to the registered output handler.

Below is an example of how this looks in practice:

from conda.common.io import get_reporter_manager

reporter_manager = get_reporter_manager()

# prints a simple string
reporter_manager.render("hello")

# output:
# hello 

# prints a detailed view
data = {"field_one": "value_one", "field_two": "value_two"}

reporter_manager.render(data, component="detail_view")

# output:
# 
# field_one : value_one
# field_two : value_two
# 

By itself, the above example doesn't look impressive at all and perhaps a little cumbersome. The real power of this approach is now we are able to configure any number of reporter/output handler pairings. This means that when we want to change the output to JSON we do not have to change any of the code above.

A note about the conda info refactor

While digging into the refactor of conda info, I discovered a couple quirks with the existing code. I was hoping to remove most logic that relied on directly checking context.json, but that simply wasn't possible considering how it worked. This will most likely still be present in most of our code as we proceed with the refactor because what the "console" output displays isn't always a direct mapping to what the "json" output displays.

Inside the conda.cli.main_info module, I have also intentionally left a piece of the output rendering in the module itself rather than porting it to conda.plugins.reporter_handlers.console like I did for envs_list. This is in part to show it is possible to still do the layout handling in the module itself, but if I were to completely refactor that module, I would favor moving that logic to the conda.plugins.reporter_handlers.console and simply make it available as a type of component we could use.

@conda-bot conda-bot added the cla-signed [bot] added once the contributor has signed the CLA label May 7, 2024
Copy link

codspeed-hq bot commented May 7, 2024

CodSpeed Performance Report

Merging #13897 will not alter performance

Comparing travishathaway:reporter-output-experiment (c97e71f) with feature/reporters-backends (6ce69e9)

Summary

✅ 21 untouched benchmarks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed [bot] added once the contributor has signed the CLA
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

None yet

2 participants