Esempio n. 1
0
    def rw_lazy(self,
                name,
                call,
                args=None,
                main_group=False,
                group=None,
                force=False):
        """Load or write callable inside managed directory.

        Parameters
        ----------
        name : str
            Name
        call
            Any callable: lambda, function, class, etc.
            Its result is saved to file if file does not exist.
        args : optional
            Args to pass to callable.
        main_group : bool
            If should use main context group.
        group : str
            File group (folder)
            If main_group is set, will be placed inside it.
        force : bool
            Force overwrite.
        """
        if args is None:
            args = []
        elif not isinstance(args, list):
            args = [args]

        path = ensure_ext(self.path(name, main_group, group), self._extension)
        exists = os.path.exists(path)
        source = 'from cache' if exists else 'live'
        group_name = '{}/{}'.format(group, name) if group else name

        logger.info('Loading "{}" {}.'.format(group_name, source))

        data = None
        tic = time.clock()
        if exists and not force:
            try:
                data = self._load_obj(path)
            except FileNotFoundError as e:
                logger.debug('Reading error "{}". File: {}.'.format(e, path))
        if not data:
            data = call(*args)
            self._save_obj(data, path)
        tic = time.clock() - tic

        logger.info('Loaded "{}" in {:.4f} sec.'.format(group_name, tic))
        return data
Esempio n. 2
0
    def exists(self, name, main_group=False, group=None):
        """Check file existence in managed directory.

        Parameters
        ----------
        name : str
            Name
        main_group : bool
            If should use main context group.
        group : str
            File group (folder)
            If main_group is set, will be placed inside it.
        """
        name = ensure_ext(name, self._extension)
        return os.path.exists(self.path(name, main_group, group))
Esempio n. 3
0
    def remove(self, name, main_group=False, group=None):
        """Remove file in managed directory.

        Parameters
        ----------
        name : str
            Name
        main_group : bool
            If should use main context group.
        group : str
            File group (folder)
            If main_group is set, will be placed inside it.
        """
        name = ensure_ext(name, self._extension)
        path = self.path(name, main_group, group)
        if os.path.exists(self.path(name, main_group, group)):
            os.remove(path)
Esempio n. 4
0
    def r(self, name, main_group=False, group=None):
        """Load file inside managed directory.

        Parameters
        ----------
        name : str
            Name
        main_group : bool
            If should use main context group.
        group : str
            File group (folder)
            If main_group is set, will be placed inside it.
        """
        path = ensure_ext(self.path(name, main_group, group), self._extension)
        try:
            return self._load_obj(path)
        except FileNotFoundError as e:
            logger.debug('Reading error "{}". File: {}.'.format(e, path))
Esempio n. 5
0
    def w(self, name, obj, main_group=False, group=None):
        """Load file inside managed directory.

        Parameters
        ----------
        name : str
            Name
        obj : any
            Object to save if file does not exist.
        main_group : bool
            If should use main context group.
        group : str
            File group (folder)
            If main_group is set, will be placed inside it.
        """
        path = ensure_ext(self.path(name, main_group, group), self._extension)
        try:
            self._save_obj(obj, path)
        except FileNotFoundError as e:
            logger.debug('Writing error "{}". File: {}.'.format(e, path))