Merge pull request #299 from Carton/encoding
Add support for "fallback" or "default" encoding for SGFs with no "CA"?
This commit is contained in:
2 files changed
+7
-2
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
import copy
|
import copy
|
||||||
|
import chardet
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
@@ -368,7 +369,7 @@ class SGFNode:
|
|||||||
|
|
||||||
|
|
||||||
class SGF:
|
class SGF:
|
||||||
DEFAULT_ENCODING = "ISO-8859-1" # as specified by the standard
|
DEFAULT_ENCODING = "UTF-8"
|
||||||
|
|
||||||
_NODE_CLASS = SGFNode # Class used for SGF Nodes, can change this to something that inherits from SGFNode
|
_NODE_CLASS = SGFNode # Class used for SGF Nodes, can change this to something that inherits from SGFNode
|
||||||
# https://xkcd.com/1171/
|
# https://xkcd.com/1171/
|
||||||
@@ -408,7 +409,10 @@ class SGF:
|
|||||||
if match:
|
if match:
|
||||||
encoding = match[1].decode("ascii", errors="ignore")
|
encoding = match[1].decode("ascii", errors="ignore")
|
||||||
else:
|
else:
|
||||||
encoding = cls.DEFAULT_ENCODING
|
encoding = chardet.detect(bin_contents[:300])['encoding']
|
||||||
|
# workaround for some compatibility issues for Windows-1252 and GB2312 encodings
|
||||||
|
if encoding == "Windows-1252" or encoding == "GB2312":
|
||||||
|
encoding = "GBK"
|
||||||
try:
|
try:
|
||||||
decoded = bin_contents.decode(encoding=encoding, errors="ignore")
|
decoded = bin_contents.decode(encoding=encoding, errors="ignore")
|
||||||
except LookupError:
|
except LookupError:
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ setup(
|
|||||||
"urllib3",
|
"urllib3",
|
||||||
"pygame;platform_system=='Darwin'", # some mac versions need this for kivy
|
"pygame;platform_system=='Darwin'", # some mac versions need this for kivy
|
||||||
"screeninfo;platform_system!='Darwin'", # for screen resolution, has problems on macos
|
"screeninfo;platform_system!='Darwin'", # for screen resolution, has problems on macos
|
||||||
|
"chardet", # for automatic encoding detection
|
||||||
],
|
],
|
||||||
dependency_links=["https://kivy.org/downloads/simple/"],
|
dependency_links=["https://kivy.org/downloads/simple/"],
|
||||||
python_requires=">=3.6, <4",
|
python_requires=">=3.6, <4",
|
||||||
|
|||||||
Reference in new issue
Block a user