1 # SPDX-License-Identifier: MIT
2 # Copyright 2012 Manfred Moitzi (mozman)
4 # Purpose: grab information from DXF drawings - all DXF versions supported
8 VERSION
= "%d.%d.%d" % version
10 __author__
= "mozman <mozman@gmx.at>"
11 __doc__
= """A Python library to grab information from DXF drawings - all DXF versions supported."""
14 # Python27/3x support should be done here
17 PYTHON3
= sys
.version_info
.major
> 2
24 # end of Python 2/3 adaption
25 # if tostr does not work, look at package 'dxfwrite' for escaping unicode chars
27 from .const
import BYBLOCK
, BYLAYER
30 from .tags
import dxfinfo
31 from .color
import aci_to_true_color
34 def read(stream
, options
=None):
35 if hasattr(stream
, 'readline'):
36 from .drawing
import Drawing
37 return Drawing(stream
, options
)
39 raise AttributeError('stream object requires a readline() method.')
42 def readfile(filename
, options
=None):
43 try: # is it ascii code-page encoded?
44 return readfile_as_asc(filename
, options
)
45 except UnicodeDecodeError: # try unicode and ignore errors
46 return readfile_as_utf8(filename
, options
, errors
='ignore')
49 def readfile_as_utf8(filename
, options
=None, errors
='strict'):
50 return _read_encoded_file(filename
, options
, encoding
='utf-8', errors
=errors
)
53 def readfile_as_asc(filename
, options
=None):
55 with io
.open(filename
) as fp
:
59 return _read_encoded_file(filename
, options
, encoding
=get_encoding())
62 def _read_encoded_file(filename
, options
=None, encoding
='utf-8', errors
='strict'):
63 from .drawing
import Drawing
65 with io
.open(filename
, encoding
=encoding
, errors
=errors
) as fp
:
66 dwg
= Drawing(fp
, options
)
67 dwg
.filename
= filename