Skip to content

Commit ab7e243

Browse files
committed
move default encoding definition to backend, re-order engine, format, encoding
1 parent 8fe3f02 commit ab7e243

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

graphviz/backend.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878

7979
FORMATTERS = {'cairo', 'core', 'gd', 'gdiplus', 'gdwbmp', 'xlib'}
8080

81+
ENCODING = 'utf-8'
82+
8183
PLATFORM = platform.system().lower()
8284

8385

@@ -249,7 +251,7 @@ def pipe(engine, format, data, renderer=None, formatter=None, quiet=False):
249251

250252
def unflatten(source,
251253
stagger=None, fanout=False, chain=None,
252-
encoding='utf-8'):
254+
encoding=ENCODING):
253255
"""Return DOT ``source`` piped through Graphviz *unflatten* preprocessor.
254256
255257
Args:

graphviz/dot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
'test-output/m00se.gv.pdf'
2929
"""
3030

31-
from . import lang
31+
from . import backend
3232
from . import files
33+
from . import lang
3334

3435
__all__ = ['Graph', 'Digraph']
3536

@@ -52,7 +53,7 @@ class Dot(files.File):
5253

5354
def __init__(self, name=None, comment=None,
5455
filename=None, directory=None,
55-
format=None, engine=None, encoding=files.ENCODING,
56+
format=None, engine=None, encoding=backend.ENCODING,
5657
graph_attr=None, node_attr=None, edge_attr=None, body=None,
5758
strict=False):
5859
self.name = name

graphviz/files.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,17 @@
1515

1616
__all__ = ['File', 'Source']
1717

18-
ENCODING = 'utf-8'
19-
2018

2119
log = logging.getLogger(__name__)
2220

2321

2422
class Base(object):
2523

26-
_format = 'pdf'
2724
_engine = 'dot'
28-
_encoding = ENCODING
2925

30-
@property
31-
def format(self):
32-
"""The output format used for rendering (``'pdf'``, ``'png'``, ...)."""
33-
return self._format
26+
_format = 'pdf'
3427

35-
@format.setter
36-
def format(self, format):
37-
format = format.lower()
38-
if format not in backend.FORMATS:
39-
raise ValueError('unknown format: %r' % format)
40-
self._format = format
28+
_encoding = backend.ENCODING
4129

4230
@property
4331
def engine(self):
@@ -51,6 +39,18 @@ def engine(self, engine):
5139
raise ValueError('unknown engine: %r' % engine)
5240
self._engine = engine
5341

42+
@property
43+
def format(self):
44+
"""The output format used for rendering (``'pdf'``, ``'png'``, ...)."""
45+
return self._format
46+
47+
@format.setter
48+
def format(self, format):
49+
format = format.lower()
50+
if format not in backend.FORMATS:
51+
raise ValueError('unknown format: %r' % format)
52+
self._format = format
53+
5454
@property
5555
def encoding(self):
5656
"""The encoding for the saved source file."""
@@ -85,7 +85,7 @@ class File(Base):
8585
_default_extension = 'gv'
8686

8787
def __init__(self, filename=None, directory=None,
88-
format=None, engine=None, encoding=ENCODING):
88+
format=None, engine=None, encoding=backend.ENCODING):
8989
if filename is None:
9090
name = getattr(self, 'name', None) or self.__class__.__name__
9191
filename = '%s.%s' % (name, self._default_extension)
@@ -323,7 +323,7 @@ class Source(File):
323323

324324
@classmethod
325325
def from_file(cls, filename, directory=None,
326-
format=None, engine=None, encoding=ENCODING):
326+
format=None, engine=None, encoding=backend.ENCODING):
327327
"""Return an instance with the source string read from the given file.
328328
329329
Args:
@@ -342,7 +342,7 @@ def from_file(cls, filename, directory=None,
342342
return cls(source, filename, directory, format, engine, encoding)
343343

344344
def __init__(self, source, filename=None, directory=None,
345-
format=None, engine=None, encoding=ENCODING):
345+
format=None, engine=None, encoding=backend.ENCODING):
346346
super(Source, self).__init__(filename, directory,
347347
format, engine, encoding)
348348
self.source = source #: The verbatim DOT source code string.

tests/test_files.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ def source():
1919
return Source(**SOURCE)
2020

2121

22-
def test_format(source):
23-
assert not SOURCE['format'].islower()
24-
25-
assert source.format == SOURCE['format'].lower()
26-
with pytest.raises(ValueError, match=r'format'):
27-
source.format = ''
28-
29-
3022
def test_engine(source):
3123
assert not SOURCE['engine'].islower()
3224

@@ -35,6 +27,14 @@ def test_engine(source):
3527
source.engine = ''
3628

3729

30+
def test_format(source):
31+
assert not SOURCE['format'].islower()
32+
33+
assert source.format == SOURCE['format'].lower()
34+
with pytest.raises(ValueError, match=r'format'):
35+
source.format = ''
36+
37+
3838
def test_encoding(source):
3939
assert source.encoding == SOURCE['encoding']
4040

0 commit comments

Comments
 (0)