How to use the datafiles.hooks function in datafiles

To help you get started, we’ve selected a few datafiles examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jacebrowning / datafiles / datafiles / model.py View on Github external
def modified_init(self, *args, **kwargs):
        with hooks.disabled():
            init(self, *args, **kwargs)
        Model.__post_init__(self)
github jacebrowning / datafiles / datafiles / manager.py View on Github external
def get(self, *args, **kwargs) -> HasDatafile:
        fields = dataclasses.fields(self.model)
        missing_args = [Missing] * (len(fields) - len(args) - len(kwargs))
        args = (*args, *missing_args)

        with hooks.disabled():
            instance = self.model(*args, **kwargs)
            instance.datafile.load()

        return instance
github jacebrowning / datafiles / datafiles / mappers.py View on Github external
return

        if self.path:
            if _log:
                log.info(f"Loading '{self.classname}' object from '{self.relpath}'")
        else:
            raise RuntimeError("'pattern' must be set to load the model")

        data = formats.deserialize(self.path, self.path.suffix)
        self._last_data = data

        message = f'Data from file: {self.path}'
        log.debug(message)
        log.debug('=' * len(message) + '\n\n' + prettify(data) + '\n')

        with hooks.disabled():

            for name, value in data.items():
                if name not in self.attrs and self.auto_attr:
                    cls: Any = type(value)
                    if issubclass(cls, list):
                        cls.__origin__ = list

                        if value:
                            item_cls = type(value[0])
                            for item in value:
                                if not isinstance(item, item_cls):
                                    log.warn(f'{name!r} list type cannot be inferred')
                                    item_cls = Converter
                                    break
                        else:
                            log.warn(f'{name!r} list type cannot be inferred')
github jacebrowning / datafiles / datafiles / mappers.py View on Github external
def save(self, *, include_default_values: Trilean = None, _log=True) -> None:
        if self._root:
            self._root.save(include_default_values=include_default_values, _log=_log)
            return

        if self.path:
            if _log:
                log.info(f"Saving '{self.classname}' object to '{self.relpath}'")
        else:
            raise RuntimeError(f"'pattern' must be set to save the model")

        with hooks.disabled():
            text = self._get_text(include_default_values=include_default_values)

        self._write(text)

        self.modified = False
github jacebrowning / datafiles / datafiles / model.py View on Github external
def __post_init__(self):
        log.debug(f'Initializing {self.__class__} object')

        self.datafile = create_mapper(self)

        if settings.HOOKS_ENABLED:
            with hooks.disabled():

                path = self.datafile.path
                exists = self.datafile.exists
                create = not self.datafile.manual

                if path:
                    log.debug(f'Datafile path: {path}')
                    log.debug(f'Datafile exists: {exists}')

                    if exists:
                        self.datafile.load(_first_load=True)
                    elif path and create:
                        self.datafile.save()

                    hooks.apply(self, self.datafile)
github jacebrowning / datafiles / datafiles / model.py View on Github external
with hooks.disabled():

                path = self.datafile.path
                exists = self.datafile.exists
                create = not self.datafile.manual

                if path:
                    log.debug(f'Datafile path: {path}')
                    log.debug(f'Datafile exists: {exists}')

                    if exists:
                        self.datafile.load(_first_load=True)
                    elif path and create:
                        self.datafile.save()

                    hooks.apply(self, self.datafile)

        log.debug(f'Initialized {self.__class__} object')