1.97 KB
import json

This module aims to manage environement variables local to the application.
These variables are persitent and located in a json file.

class PersistencyData(object):
    """Initialize with an persistent file (json)

    You can access to environment variables using the brackets operators.
    You can also check with the 'in' operator if a key is present among
    the variables.
    example code:
    env = Env("myfile.json")

    if "MyVariable" in env:
        env["MyVariable"] = "New value"

    def __init__(self, file: str):
        self.__variables = {}
        self.file = file

    def variables(self):
        return self.__variables

    def file(self):
        return self.__file

    def file(self, file):
        self.__file = file

    def set(self, key: str, value: str):
        """Modify an environment variable

            key (str): [description]
            value ([type]): [description]

    def get(self, key: str) -> str:
        """Get an environment variable.

            key (str): [description]

            [str]: [description]
        return self.variables[key]

    def __getitem__(self, key):
        return self.variables[key]

    def __setitem__(self, key, value):
        self.variables[key] = value

    def __delitem__(self, key):
        del self.variables[key]

    def __contains__(self, key):
        return key in self.variables
    def load(self):
        """Load the variables from the json file.
        with open(self.file, "r") as f:
            self.__variables = json.load(f)

    def save(self):
        """Save the variables into the json file.
        with open(self.file, "w") as f:
            json.dump(self.variables, f)