Exemple #1
0
 def test_get_obj(self):
     storage = JSONStore(self.storage_file)
     key = 'foobar1'
     value = {"does this work": "noway"}
     storage.save(key, value)
     return_value = storage.get(key)
     assert return_value == value
Exemple #2
0
 def test_get_string(self):
     storage = JSONStore(self.storage_file)
     key = 'foobar1'
     value = 'disco'
     storage.save(key, value)
     return_value = storage.get(key)
     assert return_value == value
Exemple #3
0
    class __InternalConfig:
        def __init__(self):
            self._home = None
            self.logging_level = logging.DEBUG
            DatmoLogger.get_logger(__name__).info("initalizing")
            self.data_cache = JSONStore(
                os.path.join(os.path.expanduser("~"), ".datmo", "cache.json"))

        @property
        def home(self):
            return self._home

        @home.setter
        def home(self, home_path):
            self._home = home_path

        def get_cache_item(self, key):
            cache_expire_key = 'cache_key_expires.' + key
            cache_key = 'cache_key.' + key
            cache_expire_val = self.data_cache.get(cache_expire_key)
            # no cache expire val, it's not stored
            if cache_expire_val == None:
                return None
            # return value if item has not expired
            elif int(cache_expire_val) > int(
                    datetime.datetime.now().strftime('%s')):
                return self.data_cache.get(cache_key)
            # expire item and return None
            else:
                self.data_cache.remove(cache_expire_key)
                self.data_cache.remove(cache_key)
                return None

        def set_cache_item(self, key, value, duration=60):
            cache_expire_key = 'cache_key_expires.' + key
            cache_key = 'cache_key.' + key
            expire_val = (duration * 60) + int(
                datetime.datetime.now().strftime('%s'))
            self.data_cache.save(cache_expire_key, expire_val)
            self.data_cache.save(cache_key, value)
Exemple #4
0
class BaseController(object):
    """BaseController is used to setup the repository. It serves as the basis for all other Controller objects

    Parameters
    ----------
    home : str
        home path of the project
    dal_driver : DALDriver
        an instance of a DALDriver to use while accessing the DAL

    Attributes
    ----------
    home : str
        Filepath for the location of the project
    dal_driver : DALDriver object
        This is an instance of a storage DAL driver
    config : JSONStore
        This is the set of configurations used to create a project
    dal
    model
    current_session
    code_driver
    file_driver
    environment_driver
    is_initialized

    Methods
    -------
    dal_instantiate()
        Instantiate a version of the DAL
    get_or_set_default(key, default_value)
        Returns value adn sets to default if no value present
    config_loader(key)
        Return the config dictionary based on key
    get_config_defaults()
        Return the configuration defaults
    """

    def __init__(self, home):
        self.home = home
        if not os.path.isdir(self.home):
            raise InvalidProjectPathException(__("error",
                                                 "controller.base.__init__",
                                                 home))
        self.config = JSONStore(os.path.join(self.home,
                                             ".datmo",
                                             ".config"))
        # property caches and initial values
        self._dal = None
        self._model = None
        self._current_session = None
        self._code_driver = None
        self._file_driver = None
        self._environment_driver = None
        self._is_initialized = False

    @property
    # Controller objects are only in sync if the data drivers are the same between objects
    # Currently pass dal_driver down from controller to controller to ensure syncing dals
    # TODO: To fix dal from different controllers so they sync within one session; they do NOT currently
    def dal(self):
        """Property that is maintained in memory

        Returns
        -------
        DAL
        """
        if self._dal == None:
            self._dal = self.dal_instantiate()
        return self._dal

    @property
    def model(self):
        """Property that is maintained in memory

        Returns
        -------
        Model
        """
        if self._model == None:
            models = self.dal.model.query({})
            self._model = models[0] if models else None
        return self._model

    @property
    def current_session(self):
        """Property that is maintained in memory

        Returns
        -------
        Session
        """
        if not self.model:
            raise DatmoModelNotInitializedException(__("error",
                                                      "controller.base.current_session"))
        if self._current_session == None:
            sessions = self.dal.session.query({"current": True})
            self._current_session = sessions[0] if sessions else None
        return self._current_session

    @property
    def code_driver(self):
        """Property that is maintained in memory

        Returns
        -------
        CodeDriver
        """
        if self._code_driver == None:
            module_details = self.config_loader("controller.code.driver")
            self._code_driver = module_details["constructor"](**module_details["options"])
        return self._code_driver

    @property
    def file_driver(self):
        """Property that is maintained in memory

        Returns
        -------
        FileDriver
        """
        if self._file_driver == None:
            module_details = self.config_loader("controller.file.driver")
            self._file_driver = module_details["constructor"](**module_details["options"])
        return self._file_driver

    @property
    def environment_driver(self):
        """Property that is maintained in memory

        Returns
        -------
        EnvironmentDriver
        """
        if self._environment_driver == None:
            module_details = self.config_loader("controller.environment.driver")
            self._environment_driver = module_details["constructor"](**module_details["options"])
        return self._environment_driver

    @property
    def is_initialized(self):
        """Property that is maintained in memory

        Returns
        -------
        bool
            True if the project is property initialized else False
        """
        if not self._is_initialized:
          if self.code_driver.is_initialized and \
              self.environment_driver.is_initialized and \
                  self.file_driver.is_initialized:
              if self.model:
                  self._is_initialized = True
        return self._is_initialized

    def dal_instantiate(self):
        # first load driver, then create DAL using driver
        dal_driver_dict = self.config_loader("storage.driver")
        dal_driver = dal_driver_dict["constructor"](**dal_driver_dict["options"])
        # Get DAL, set driver,
        dal_dict = self.config_loader("storage.local")
        dal_dict["options"]["driver"] = dal_driver
        return dal_dict["constructor"](**dal_dict["options"])

    def get_or_set_default(self, key, default_value):
        value = self.config.get(key)
        if value is None:
            self.config.save(key, default_value)
            value = default_value
        return value

    def config_loader(self, key):
        defaults = self.get_config_defaults()
        module_details = self.get_or_set_default(key, defaults[key])
        module_details["constructor"] = get_class_contructor(module_details["class_constructor"])
        return module_details

    def get_config_defaults(self):
        return {
            "controller.code.driver": {
                "class_constructor": "datmo.core.controller.code.driver.git.GitCodeDriver",
                "options": {
                    "filepath": self.home,
                    "execpath": "git"
                }
            },
            "controller.file.driver": {
                "class_constructor": "datmo.core.controller.file.driver.local.LocalFileDriver",
                "options": {
                    "filepath": self.home
                }
            },
            "controller.environment.driver":{
                "class_constructor": "datmo.core.controller.environment.driver.dockerenv.DockerEnvironmentDriver",
                "options": {
                    "filepath": self.home,
                    "docker_execpath": "docker"
                }
            },
            "storage.local": {
                "class_constructor": "datmo.core.storage.local.dal.LocalDAL",
                "options": {
                    "driver": "storage.driver"
                }
            },
            "storage.driver": {
                "class_constructor": "datmo.core.storage.driver.blitzdb_dal_driver.BlitzDBDALDriver",
                "options": {
                    "driver_type": "file",
                    "connection_string": os.path.join(self.home, ".datmo/database")
                }
            },
        }
Exemple #5
0
    class __InternalConfig:
        def __init__(self):
            self._home = None
            self.datmo_directory_name = ".datmo"
            self.logging_level = logging.DEBUG
            DatmoLogger.get_logger(__name__).info("initializing")
            self.data_cache = JSONStore(
                os.path.join(os.path.expanduser("~"), ".datmo", "cache.json"))
            self.docker_cli = '/usr/bin/docker'

        @property
        def home(self):
            return self._home

        @property
        def remote_credentials(self):
            """
            Returns credentials if present

            Returns
            -------
            MASTER_SERVER_IP : str
                return if present else None
            DATMO_API_KEY : str
                return if present else None
            END_POINT : str
                return if present else None
            """
            # 1) Load from the environment if datmo config not already saved globally
            MASTER_SERVER_IP = os.environ.get('MASTER_SERVER_IP', None)
            DATMO_API_KEY = os.environ.get('DATMO_API_KEY', None)

            # 2) loading the datmo config if present
            datmo_config_filepath = os.path.join(os.path.expanduser("~"),
                                                 ".datmo", "config")
            if os.path.isfile(datmo_config_filepath):
                datmo_config = JSONStore(datmo_config_filepath)
                config_dict = datmo_config.to_dict()
                if MASTER_SERVER_IP is None:
                    MASTER_SERVER_IP = config_dict.get('MASTER_SERVER_IP',
                                                       None)
                if DATMO_API_KEY is None:
                    DATMO_API_KEY = config_dict.get('DATMO_API_KEY', None)

            if MASTER_SERVER_IP:
                END_POINT = 'http://' + MASTER_SERVER_IP + ':2083/api/v1'
            else:
                END_POINT = None

            return MASTER_SERVER_IP, DATMO_API_KEY, END_POINT

        def set_home(self, home_path):
            self._home = home_path

        def get_cache_item(self, key):
            cache_expire_key = 'cache_key_expires.' + key
            cache_key = 'cache_key.' + key
            cache_expire_val = self.data_cache.get(cache_expire_key)
            # no cache expire val, it's not stored
            if cache_expire_val == None:
                return None
            # return value if item has not expired
            elif int(cache_expire_val) > int(
                    datetime.datetime.now().strftime('%s')):
                return self.data_cache.get(cache_key)
            # expire item and return None
            else:
                self.data_cache.remove(cache_expire_key)
                self.data_cache.remove(cache_key)
                return None

        def set_cache_item(self, key, value, duration=60):
            cache_expire_key = 'cache_key_expires.' + key
            cache_key = 'cache_key.' + key
            expire_val = (duration * 60) + int(
                datetime.datetime.now().strftime('%s'))
            self.data_cache.save(cache_expire_key, expire_val)
            self.data_cache.save(cache_key, value)