Skip to main content

A Python library for managing and loading class instances from modules and YAML configurations.

Project description

import micro-registry

micro-registry is a Python library that provides a flexible and dynamic way to manage classes and their instances. It simplifies the development of complex systems by introducing mechanisms for dynamic class registration, instance creation, and configuration loading from YAML files. The library also implements the MicroComponent pattern and provides built-in RESTful APIs for component management.

Features

  • Dynamic Class Registration: Easily register classes using decorators for dynamic instantiation.
  • MicroComponent Pattern: Build hierarchical component systems with parent-child relationships.
  • Instance Management: Create and manage instances of registered classes with ease.
  • YAML Configuration Loading: Define your system components in YAML files for flexible configuration.
  • Use regular Modules: Register instances of class from regular python packages.
  • Built-in RESTful APIs: Use the RegistryAPI and ComponentAPI to interact with your components over HTTP.
  • Plugin Architecture Support: Load modules dynamically to extend functionality without modifying the core system.

Installation

You can install the package via pip:

pip install micro-registry

Or clone the repository and install it locally:

git clone https://github.com/yourusername/micro-registry.git
cd micro-registry
pip install .

Getting Started

Registering Classes

Registering classes is straightforward using the @register_class decorator. This allows the class to be dynamically instantiated later.

from micro_registry.registry import register_class

@register_class
class MyComponent:
    def __init__(self, name, **kwargs):
        self.name = name
        # Additional initialization

    def start(self):
        print(f"Component {self.name} started.")

    def stop(self):
        print(f"Component {self.name} stopped.")

The MicroComponent Pattern

The MicroComponent pattern provides a base class for creating components with hierarchical relationships. Components can have parents and children, making it easy to build complex systems.

from micro_registry.component import MicroComponent

@register_class
class MyMicroComponent(MicroComponent):
    def __init__(self, name, parent=None, **kwargs):
        super().__init__(name, parent)
        # Additional initialization

Loading Components from YAML

Define your system's components in a YAML file for easy configuration and modification.

components.yaml

components:
  - name: component_a
    class: MyMicroComponent
    parameters:
      param1: value1

  - name: component_b
    class: MyMicroComponent
    parameters:
      param2: value2
    children:
      - name: component_c
        class: MyMicroComponent
        parameters:
          param3: value3

Load the components and start the system:

from micro_registry.component_loader import load_components_and_start_system

load_components_and_start_system('components.yaml')

Using the RegistryAPI and ComponentAPI

micro-registry includes built-in RESTful APIs for interacting with your components.

RegistryAPI

The RegistryAPI provides endpoints for:

  • Listing registered classes.
  • Listing instances.
  • Loading instances from YAML.
  • Filtering instances by base class.

ComponentAPI

The ComponentAPI allows you to:

  • Retrieve component hierarchies.
  • Get and update component attributes.
  • Invoke component methods.

Example:

from micro_registry.registry_rest_api import RegistryRestApi
from micro_registry.component_rest_api import ComponentRestApi

# Initialize the Registry API
registry_api = RegistryRestApi(name='registry_api', host='0.0.0.0', port=8000)

# Add the Component API as a child
component_api = ComponentRestApi(name='component_api', parent=registry_api)

# Start the API server
registry_api.start()

Full Example

components.yaml

components:
  - name: registry_api
    class: RegistryRestApi
    parameters:
      host: "0.0.0.0"
      port: 8000
    children:
      - name: component_api
        class: ComponentRestApi

  - name: scheduler_main
    class: Scheduler
    children:
      - name: living_room_light
        class: Light
        parameters:
          location: "Living Room"
          brightness: 75

      - name: hallway_thermostat
        class: Thermostat
        parameters:
          location: "Hallway"
          temperature: 21.5

      - name: evening_lights_automation
        class: Automation
        parameters:
          action: "turn_on"
          target_devices:
            - "living_room_light"

      - name: morning_temperature_automation
        class: Automation
        parameters:
          action: "set_temperature"
          target_devices:
            - "hallway_thermostat"
          temperature: 23.0

main.py

from micro_registry.component_loader import load_components_and_start_system

if __name__ == '__main__':
    load_components_and_start_system('components.yaml')

Interacting with the API

Once your system is running, you can interact with it using the API.

  • List Instances

    curl http://localhost:8000/api/v1/instances
    
  • Get Component Hierarchy

    curl http://localhost:8000/api/v1/component/scheduler_main/hierarchy/
    
  • Update Component Property

    curl -X POST http://localhost:8000/api/v1/component/living_room_light/update-property/ \
         -H 'Content-Type: application/json' \
         -d '{"property_name": "status", "value": "on"}'
    

Advanced Usage

Plugin Architecture

Load additional modules dynamically to extend the functionality of your system without modifying the core code.

from micro_registry.registry import load_modules_from_directory

load_modules_from_directory('plugins')

Custom Component Methods

Define custom methods in your components and expose them via the API.

@register_class
class Device(MicroComponent):
    def __init__(self, name, device_type, location, **kwargs):
        super().__init__(name)
        self.device_type = device_type
        self.location = location

    def turn_on(self):
        print(f"{self.device_type.capitalize()} '{self.name}' at '{self.location}' is now ON.")

    def turn_off(self):
        print(f"{self.device_type.capitalize()} '{self.name}' at '{self.location}' is now OFF.")

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository on GitHub.

  2. Clone your fork locally.

    git clone https://github.com/yourusername/micro_registry.git
    
  3. Install development dependencies.

    pip install -e .[dev]
    
  4. Create a new branch for your feature or bugfix.

    git checkout -b feature/new-feature
    
  5. Make your changes, write tests, and ensure all tests pass.

    flake8 .
    python -m unittest discover -s tests
    
  6. Commit your changes with a descriptive commit message.

    git commit -am "Add new feature to improve X"
    
  7. Push to your fork and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For questions or contributions, reach out at aleksander.stanik@hammerheadsengineers.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micro_registry-0.4.2.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

micro_registry-0.4.2-py3-none-any.whl (20.0 kB view details)

Uploaded Python 3

File details

Details for the file micro_registry-0.4.2.tar.gz.

File metadata

  • Download URL: micro_registry-0.4.2.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for micro_registry-0.4.2.tar.gz
Algorithm Hash digest
SHA256 3b610223fd2adc5fed4839e9b669a9ea20d0a7c02eade9c3cb909544246baa95
MD5 92e651e9fa55184bad400d76c0185801
BLAKE2b-256 fe2e85e15b54cccb878518dbad2bf45d6694fc3db322dc5e2d3ce42289e72d45

See more details on using hashes here.

File details

Details for the file micro_registry-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: micro_registry-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 20.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for micro_registry-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b9a3a11083edfd85a047c1d62895c1a8f7ebd13b4f69006de7d682bc2467a8f5
MD5 31b6217db6f3377a8aee5d0d60d9fe4a
BLAKE2b-256 cfeaf7f47b6faa4cd23bed17856f8ec4ad10f147e5fe43b578e86cdbb42d407b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page