Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend codec support for CSA file parsing #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions shogi/CSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import socket
import threading
import collections
import codecs

DEFAULT_PORT = 4081
PING_SLEEP_DURATION = 1
Expand Down Expand Up @@ -63,8 +64,13 @@
class Parser:
@staticmethod
def parse_file(path):
with open(path) as f:
return Parser.parse_str(f.read())
for enc in ['cp932', 'utf-8-sig']:
try:
with codecs.open(path, 'r', enc) as f:
return Parser.parse_str(f.read())
except:
pass
return None
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if the function raises an exception when we cannot decode as cp932 and utf-8-sig instead of returning None.


@staticmethod
def parse_str(csa_str):
Expand Down