def readfile(filename: Union[str, 'Path'], encoding: str = None, errors: str = 'surrogateescape') -> 'Drawing': """ Read the DXF document `filename` from the file-system. This is the preferred method to load existing ASCII or Binary DXF files, the required text encoding will be detected automatically and decoding errors will be ignored. Override encoding detection by setting argument `encoding` to the estimated encoding. (use Python encoding names like in the :func:`open` function). If this function struggles to load the DXF document and raises a :class:`DXFStructureError` exception, try the :func:`ezdxf.recover.readfile` function to load this corrupt DXF document. Args: filename: filename of the ASCII- or Binary DXF document encoding: use ``None`` for auto detect (default), or set a specific encoding like "utf-8", argument is ignored for Binary DXF files errors: specify decoding error handler - "surrogateescape" to preserve possible binary data (default) - "ignore" to use the replacement char U+FFFD "\ufffd" for invalid data - "strict" to raise an :class:`UnicodeDecodeError` exception for invalid data Raises: IOError: not a DXF file or file does not exist DXFStructureError: for invalid or corrupted DXF structures UnicodeDecodeError: if `errors` is "strict" and a decoding error occurs .. deprecated:: v0.14 argument `legacy_mode`, use module :mod:`ezdxf.recover` to load DXF documents with structural flaws. """ from ezdxf.lldxf.validator import is_dxf_file, is_binary_dxf_file from ezdxf.tools.codepage import is_supported_encoding from ezdxf.lldxf.tagger import binary_tags_loader filename = str(filename) if is_binary_dxf_file(filename): with open(filename, 'rb') as fp: data = fp.read() loader = binary_tags_loader(data, errors=errors) return Drawing.load(loader) if not is_dxf_file(filename): raise IOError(f"File '{filename}' is not a DXF file.") info = dxf_file_info(filename) if encoding is not None: # override default encodings if absolute necessary info.encoding = encoding with open(filename, mode='rt', encoding=info.encoding, errors=errors) as fp: doc = read(fp) doc.filename = filename if encoding is not None and is_supported_encoding(encoding): # store overridden encoding if supported by AutoCAD, else default # encoding stored in $DWGENCODING is used as document encoding or # 'cp1252' if $DWGENCODING is unset. doc.encoding = encoding return doc
def readfile(filename: str, encoding: str = None, legacy_mode: bool = False) -> 'Drawing': """ Read the DXF document `filename` from the file-system. This is the preferred method to load existing ASCII or Binary DXF files, the required text encoding will be detected automatically and decoding errors will be ignored. Override encoding detection by setting argument `encoding` to the estimated encoding. (use Python encoding names like in the :func:`open` function). If this function struggles to load the DXF document and raises a :class:`DXFStructureError` exception, try the :func:`ezdxf.recover.readfile` function to load this corrupt DXF document. Args: filename: filename of the ASCII- or Binary DXF document encoding: use ``None`` for auto detect (default), or set a specific encoding like "utf-8", argument is ignored for Binary DXF files legacy_mode: adds an extra trouble shooting import layer if ``True`` (deprecated) Raises: IOError: File `filename` is not a DXF file or does not exist. DXFStructureError: for invalid DXF structure .. deprecated:: v0.14 argument `legacy_mode`, use module :mod:`ezdxf.recover` to load DXF documents with structural flaws. """ from ezdxf.lldxf.validator import is_dxf_file, is_binary_dxf_file from ezdxf.tools.codepage import is_supported_encoding from ezdxf.lldxf.tagger import binary_tags_loader if legacy_mode: warnings.warn( '"legacy_mode" is deprecated (removed in v0.16), replace call by ' 'ezdxf.recover.read().', DeprecationWarning) filename = str(filename) if is_binary_dxf_file(filename): with open(filename, 'rb') as fp: data = fp.read() loader = binary_tags_loader(data) return Drawing.load(loader, legacy_mode) if not is_dxf_file(filename): raise IOError("File '{}' is not a DXF file.".format(filename)) info = dxf_file_info(filename) if encoding is not None: # override default encodings if absolute necessary info.encoding = encoding with open(filename, mode='rt', encoding=info.encoding, errors='ignore') as fp: doc = read(fp, legacy_mode=legacy_mode) doc.filename = filename if encoding is not None and is_supported_encoding(encoding): # store overridden encoding if supported by AutoCAD, else default # encoding stored in $DWGENCODING is used as document encoding or # 'cp1252' if $DWGENCODING is unset. doc.encoding = encoding return doc