How to use the datafiles.formats.Formatter 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 / formats.py View on Github external
"""Formatter for JavaScript Object Notation."""

    @classmethod
    def extensions(cls):
        return {'.json'}

    @classmethod
    def deserialize(cls, file_object):
        return json.load(file_object)

    @classmethod
    def serialize(cls, data):
        return json.dumps(data, indent=2)


class TOML(Formatter):
    """Formatter for (round-trip) Tom's Obvious Minimal Language."""

    @classmethod
    def extensions(cls):
        return {'.toml'}

    @classmethod
    def deserialize(cls, file_object):
        import tomlkit

        return tomlkit.loads(file_object.read())

    @classmethod
    def serialize(cls, data):
        import tomlkit
github jacebrowning / datafiles / datafiles / formats.py View on Github external
    @abstractmethod
    def extensions(cls) -> List[str]:
        raise NotImplementedError

    @classmethod
    @abstractmethod
    def deserialize(cls, file_object: IO) -> Dict:
        raise NotImplementedError

    @classmethod
    @abstractmethod
    def serialize(cls, data: Dict) -> str:
        raise NotImplementedError


class JSON(Formatter):
    """Formatter for JavaScript Object Notation."""

    @classmethod
    def extensions(cls):
        return {'.json'}

    @classmethod
    def deserialize(cls, file_object):
        return json.load(file_object)

    @classmethod
    def serialize(cls, data):
        return json.dumps(data, indent=2)


class TOML(Formatter):
github jacebrowning / datafiles / datafiles / formats.py View on Github external
if settings.INDENT_YAML_BLOCKS:
            f = StringIO()
            y = yaml.YAML()
            y.indent(mapping=2, sequence=4, offset=2)
            y.dump(data, f)
            text = f.getvalue().strip() + '\n'
        else:
            text = yaml.round_trip_dump(data) or ""

        if text == "{}\n":
            return ""

        return text.replace('- \n', '-\n')


class PyYAML(Formatter):
    """Formatter for YAML Ain't Markup Language."""

    @classmethod
    def extensions(cls):
        return {'.yml', '.yaml'}

    @classmethod
    def deserialize(cls, file_object):
        import yaml

        return yaml.safe_load(file_object)

    @classmethod
    def serialize(cls, data):
        import yaml
github jacebrowning / datafiles / datafiles / formats.py View on Github external
def _get_formatter(extension: str):
    if settings.YAML_LIBRARY == 'PyYAML':  # pragma: no cover
        register('.yml', PyYAML)

    with suppress(KeyError):
        return _REGISTRY[extension]

    for formatter in Formatter.__subclasses__():
        if extension in formatter.extensions():
            return formatter

    raise ValueError(f'Unsupported file extension: {extension!r}')
github jacebrowning / datafiles / datafiles / formats.py View on Github external
return {'.toml'}

    @classmethod
    def deserialize(cls, file_object):
        import tomlkit

        return tomlkit.loads(file_object.read())

    @classmethod
    def serialize(cls, data):
        import tomlkit

        return tomlkit.dumps(data)


class RuamelYAML(Formatter):
    """Formatter for (round-trip) YAML Ain't Markup Language."""

    @classmethod
    def extensions(cls):
        return {'.yml', '.yaml'}

    @classmethod
    def deserialize(cls, file_object):
        from ruamel import yaml

        try:
            return yaml.round_trip_load(file_object, preserve_quotes=True)
        except NotImplementedError as e:
            log.error(str(e))
            return {}