Registry

In glim, most of the core components are extending from Registry class. The Registry class provides configuration which can be dynamically accessed using "." notation. The Registry implementation is the following;

class Registry:

    """

    This class is basically a dictionary supercharged with
    useful functions.

    Attributes
    ----------
      registrar (dict): A registrar to hold a generic
        configuration of a class.

    Usage
    -----
      config = {
        'a' : 'b',
        'c' : {
            'd' : 'e'
        }
      }
      reg = Registry(config)
      reg.get('a') # returns b
      reg.get('c.d') # returns e
      reg.set('f', 'g') # sets 'f' key of dict
      reg.get('f') # returns g

    """

    def __init__(self, registrar):
        self.registrar = registrar

    def get(self, key):
        """

        Function deeply gets the key with "." notation

        Args
        ----
          key (string): A key with the "." notation.

        Returns
        -------
          reg (unknown type): Returns a dict or a primitive
            type.

        """
        try:
            layers = key.split('.')
            value = self.registrar
            for key in layers:
                value = value[key]
            return value
        except:
            return None

    def set(self, key, value):
        """

        Function deeply sets the key with "." notation

        Args
        ----
          key (string): A key with the "." notation.
          value (unknown type): A dict or a primitive type.

        """
        target = self.registrar
        for element in key.split('.')[:-1]:
            target = target.setdefault(element, dict())
        target[key.split(".")[-1]] = value

    def flush(self):
        """

        Function clears the registrar

        Note:
          After this function is called, all of your data
          in your registry will be lost. So, use this smartly.

        """
        self.registrar.clear()

    def update(self, registrar):
        """

        Function batch updates the registry. This function is an
        alias of dict.update()

        Args
        ----
          registrar (dict): A dict of configuration.

        Note:
          After this function is called, all of your data may be
          overriden and lost by the registrar you passed. So, use
          this smartly.

        """
        self.registrar.update(registrar)

    def all(self):
        """

        Function returns all the data in registrar.

        Returns
        -------
          registrar (dict): The current registry in the class.

        """
        return self.registrar

    def __str__(self):
        return self.registrar

You can see the docstring introduces the usage of a registry object. In glim, the Config class is extending the registry. It has the following implementation;

class Config(Registry):

    """

    The configuration class is to hold framework level constants.
    It holds the dict in app.config.<env>.config.

    """

You can notice that there aren't any changes in the Registry class. The Config can be instantiated with a dict namely the registrar.