Skip to content

Fixes encoding error when building under Windows #2222

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

Merged
merged 3 commits into from
Sep 19, 2024
Merged
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
34 changes: 32 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,38 @@

# Only for windows compatibility - Forces default encoding to UTF8, which it may not be on windows
if os.name == 'nt':
reload(sys)
sys.setdefaultencoding('UTF8')
# monkeypatches sphinxcontrib.datatemplates so it uses utf-8 as the encoding
# instead of cp1252 when opening a file. using sys.setdefaultencoding does
# not alter the encoding when opening a file for reading.

from sphinxcontrib.datatemplates import directive
from xml.etree import ElementTree as ET
import mimetypes

old_load_data = directive.DataTemplateLegacy._load_data

def _load_data(self, env, data_source, encoding):
rel_filename, filename = env.relfn2path(data_source)
if data_source.endswith('.yaml'):
return self._load_yaml(filename, 'utf-8')
elif data_source.endswith('.json'):
return self._load_json(filename, 'utf-8')
elif data_source.endswith('.csv'):
return self._load_csv(filename, 'utf-8')
elif "xml" in mimetypes.guess_type(data_source)[0]:
# there are many XML based formats
return ET.parse(filename).getroot()
else:
raise NotImplementedError('cannot load file type of %s' %
data_source)


directive.DataTemplateLegacy._load_data = _load_data
setattr(
sys.modules['sphinxcontrib.datatemplates.directive'].DataTemplateLegacy,
'_load_data',
_load_data
)


sys.path.append(os.getcwd()) # noqa
Expand Down