Utils

Plugin utils module for core plugin functionality.

class smarter.apps.plugin.plugin.utils.PluginExample(filepath, filename)[source]

Bases: object

A class for loading and working with built-in YAML-based plugin examples.

This class reads plugin example files in YAML format, parses their contents, and exposes metadata and serialization methods for inspection and testing.

Parameters:
  • filepath (str) – The directory path containing the YAML file.

  • filename (str) – The name of the YAML file to load.

See also

PluginExamples for managing collections of plugin examples.

Example usage:

example = PluginExample(filepath="/path/to/examples", filename="my_plugin.yaml")
print(example.name)
print(example.to_yaml())
print(example.to_json())
__init__(filepath, filename)[source]

Initialize the class from a yaml file

convert_filename()[source]

Convert the filename to the desired format.

Return type:

Optional[str]

property filename: str | None

Return the name of the plugin.

property name: str | None

Return the name of the plugin.

to_json()[source]

Return the plugin as a dictionary.

Return type:

Union[list, dict, None]

to_yaml()[source]

Return the plugin as a yaml string.

Return type:

Optional[str]

class smarter.apps.plugin.plugin.utils.PluginExamples(*args, **kwargs)[source]

Bases: object

A class for managing a collection of PluginExample instances.

This class loads all YAML-based plugin examples from a specified directory, providing access to the collection and utility methods for counting and retrieving examples.

Parameters:
  • args (tuple) – Optional positional arguments (unused).

  • kwargs (dict) – Optional keyword arguments (unused).

Note

Only files ending with .yaml in the plugins path are loaded as examples.

Tip

Use count() to get the number of loaded plugin examples, and the plugins() property to access the list.

See also

PluginExample for individual example details.

Example usage:

examples = PluginExamples()
print(examples.count())
for example in examples.plugins:
    print(example.filename, example.name)
HERE = '/home/docs/checkouts/readthedocs.org/user_builds/smarter/checkouts/latest/smarter/smarter/apps/plugin/plugin'
PLUGINS_PATH = '/home/docs/checkouts/readthedocs.org/user_builds/smarter/checkouts/latest/smarter/smarter/apps/plugin/data/sample-plugins'
__init__(*args, **kwargs)[source]

Initialize the class.

count()[source]

Return the number of plugins.

Return type:

int

property plugins: list[PluginExample]

Return a list of plugins in dictionary format.

class smarter.apps.plugin.plugin.utils.Plugins(user, account)[source]

Bases: object

A class for managing and interacting with multiple plugins.

This class provides methods to retrieve, serialize, and work with plugins associated with a user and account. It loads plugins using metadata and controllers, and exposes their data in dictionary and JSON formats.

Parameters:
  • user (User) – The user for whom plugins are loaded.

  • account (Account) – The account context for plugin retrieval.

Raises:

PluginDataValueError – If a plugin cannot be loaded or is malformed

See also

PluginController for plugin instantiation. PluginMeta for plugin metadata.

Example usage:

plugins = Plugins(user=my_user, account=my_account)
plugin_dicts = plugins.data
plugin_json = plugins.to_json()
__init__(user, account)[source]
account: Optional[Account] = None
property data: list[dict]

Return a list of plugins in dictionary format.

plugins: list[PluginBase] = []
to_json()[source]

Return a list of plugins in JSON format.

Return type:

list[dict[str, Any]]

user_profile: Optional[UserProfile] = None
smarter.apps.plugin.plugin.utils.should_log(level)[source]

Check if logging should be done based on the waffle switch.