From ab7e243b4eb3072fec0d5b36006730476a5277a5 Mon Sep 17 00:00:00 2001 From: Sebastian Bank Date: Thu, 24 Dec 2020 13:23:10 +0100 Subject: [PATCH] move default encoding definition to backend, re-order engine, format, encoding --- graphviz/backend.py | 4 +++- graphviz/dot.py | 5 +++-- graphviz/files.py | 34 +++++++++++++++++----------------- tests/test_files.py | 16 ++++++++-------- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/graphviz/backend.py b/graphviz/backend.py index 846bc15901..1a452d6706 100644 --- a/graphviz/backend.py +++ b/graphviz/backend.py @@ -78,6 +78,8 @@ FORMATTERS = {'cairo', 'core', 'gd', 'gdiplus', 'gdwbmp', 'xlib'} +ENCODING = 'utf-8' + PLATFORM = platform.system().lower() @@ -249,7 +251,7 @@ def pipe(engine, format, data, renderer=None, formatter=None, quiet=False): def unflatten(source, stagger=None, fanout=False, chain=None, - encoding='utf-8'): + encoding=ENCODING): """Return DOT ``source`` piped through Graphviz *unflatten* preprocessor. Args: diff --git a/graphviz/dot.py b/graphviz/dot.py index 09d09fd4ee..cb7a4ed610 100644 --- a/graphviz/dot.py +++ b/graphviz/dot.py @@ -28,8 +28,9 @@ 'test-output/m00se.gv.pdf' """ -from . import lang +from . import backend from . import files +from . import lang __all__ = ['Graph', 'Digraph'] @@ -52,7 +53,7 @@ class Dot(files.File): def __init__(self, name=None, comment=None, filename=None, directory=None, - format=None, engine=None, encoding=files.ENCODING, + format=None, engine=None, encoding=backend.ENCODING, graph_attr=None, node_attr=None, edge_attr=None, body=None, strict=False): self.name = name diff --git a/graphviz/files.py b/graphviz/files.py index 39b10a933c..6b6911aae7 100644 --- a/graphviz/files.py +++ b/graphviz/files.py @@ -15,29 +15,17 @@ __all__ = ['File', 'Source'] -ENCODING = 'utf-8' - log = logging.getLogger(__name__) class Base(object): - _format = 'pdf' _engine = 'dot' - _encoding = ENCODING - @property - def format(self): - """The output format used for rendering (``'pdf'``, ``'png'``, ...).""" - return self._format + _format = 'pdf' - @format.setter - def format(self, format): - format = format.lower() - if format not in backend.FORMATS: - raise ValueError('unknown format: %r' % format) - self._format = format + _encoding = backend.ENCODING @property def engine(self): @@ -51,6 +39,18 @@ def engine(self, engine): raise ValueError('unknown engine: %r' % engine) self._engine = engine + @property + def format(self): + """The output format used for rendering (``'pdf'``, ``'png'``, ...).""" + return self._format + + @format.setter + def format(self, format): + format = format.lower() + if format not in backend.FORMATS: + raise ValueError('unknown format: %r' % format) + self._format = format + @property def encoding(self): """The encoding for the saved source file.""" @@ -85,7 +85,7 @@ class File(Base): _default_extension = 'gv' def __init__(self, filename=None, directory=None, - format=None, engine=None, encoding=ENCODING): + format=None, engine=None, encoding=backend.ENCODING): if filename is None: name = getattr(self, 'name', None) or self.__class__.__name__ filename = '%s.%s' % (name, self._default_extension) @@ -323,7 +323,7 @@ class Source(File): @classmethod def from_file(cls, filename, directory=None, - format=None, engine=None, encoding=ENCODING): + format=None, engine=None, encoding=backend.ENCODING): """Return an instance with the source string read from the given file. Args: @@ -342,7 +342,7 @@ def from_file(cls, filename, directory=None, return cls(source, filename, directory, format, engine, encoding) def __init__(self, source, filename=None, directory=None, - format=None, engine=None, encoding=ENCODING): + format=None, engine=None, encoding=backend.ENCODING): super(Source, self).__init__(filename, directory, format, engine, encoding) self.source = source #: The verbatim DOT source code string. diff --git a/tests/test_files.py b/tests/test_files.py index 7075f633aa..7333f4bc78 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -19,14 +19,6 @@ def source(): return Source(**SOURCE) -def test_format(source): - assert not SOURCE['format'].islower() - - assert source.format == SOURCE['format'].lower() - with pytest.raises(ValueError, match=r'format'): - source.format = '' - - def test_engine(source): assert not SOURCE['engine'].islower() @@ -35,6 +27,14 @@ def test_engine(source): source.engine = '' +def test_format(source): + assert not SOURCE['format'].islower() + + assert source.format == SOURCE['format'].lower() + with pytest.raises(ValueError, match=r'format'): + source.format = '' + + def test_encoding(source): assert source.encoding == SOURCE['encoding']