def _get_write_kwargs(cls, fmt: Optional[FileFormat], path: Path) -> Mapping[str, Any]: t = cls.get_typing().io real_suffix = CompressionFormat.strip_suffix(path).suffix kwargs = t.write_kwargs.get(fmt, {}) kwargs.update(t.write_suffix_kwargs.get(real_suffix, {})) if fmt is FileFormat.json: # not perfect, but much better than the alternative of failing # I don't see a better solution anyway kwargs["force_ascii"] = False elif (fmt is not None and fmt.supports_encoding ): # and IS NOT JSON -- it doesn't use "encoding=" encoding = kwargs.get("encoding", t.text_encoding) kwargs["encoding"] = Utils.get_encoding(encoding) return kwargs
def _get_read_kwargs(cls, fmt: Optional[FileFormat], path: Path) -> Mapping[str, Any]: t = cls.get_typing().io real_suffix = CompressionFormat.strip_suffix(path).suffix kwargs = t.read_kwargs.get(fmt, {}) kwargs.update(t.read_suffix_kwargs.get(real_suffix, {})) if fmt in [ FileFormat.csv, FileFormat.tsv, FileFormat.properties, FileFormat.lines, FileFormat.flexwf, FileFormat.fwf, FileFormat.json, ]: encoding = kwargs.get("encoding", t.text_encoding) kwargs["encoding"] = Utils.get_encoding(encoding) return kwargs
def is_text_encoding_utf(self) -> bool: return Utils.get_encoding( self._text_encoding) in ["utf-8", "utf-16", "utf-32"]
def test_encoding(self): assert Utils.get_encoding("platform") == sys.getdefaultencoding() assert "bom" not in Utils.get_encoding("utf8(bom)") assert "bom" not in Utils.get_encoding("utf16(bom)") assert Utils.get_encoding("UTF-8") == "utf8" assert Utils.get_encoding("utf-16") == "utf16"