Skip to content

Releases: jlgerber/py_service_locator

Improved Multi-Threaded Support

30 Mar 23:14
Compare
Choose a tag to compare

Improved Multi-Threaded Support

Ok, not really a functional change. Definitely style over substance. I have switched to a context manager to lock and unlock around the context manager. A benefit is that the code is a bit more readable and a bit shorter. But thats it.

Initial Version

30 Mar 23:07
Compare
Choose a tag to compare

service locator v1

  • Register services with the service locator
  • Fetch ServiceProxy from the service locator any module. The ServiceProxy defers fetching of the service until first access, making import and service locator population order inconsequential
  • Handles both instance and class services.
  • Optionally require service keys to be superclasses of related services.

Example Usage

Interface Definition - bases.py

from abc import (ABCMeta, abstractmethod)
class LoggerBase(object):
    __meta__ = ABCMeta
    @abstractmethod
    def debug(self, *kwargs):
         pass
    @abstractmethod
    def info(self, *kwargs):
        pass

Service Consumer - consumer.py

from service_locator import get_service_proxy
from bases import LoggerBase

class Consumer(object):

    logger = get_service_proxy(LoggerBase)

    def consume(self):
        self.logger.debug("I am consuming stuff")
        print "consume"
        self.logger.info("Consumed stuff")

Service Implementation - logger.py

from bases import LoggerBase

class SimpleLogger(LoggerBase):
    def debug(self, *args):
        print "DEBUG | ", " | ".join(args)

       def info(self, *args):
        print "INFO | ", " | ".join(args)

Main

import service_locator
import bases
import logger
import consumer

service_logger.register(bases.LoggerBase, logger.SimpleLogger)

def main():
    my_consumer = consumer.Consumer()
    my_consumer.consume()

if __name__ == "__main__":
    main()