Skip to content

Commit 157b103

Browse files
committed
[Added] Workaround for broken Python setups on Windows
Where the default encoding is broken and we can't parse KiCad's XML files. See #581
1 parent 9aa533f commit 157b103

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

kicost/edas/eda_kicad.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424

2525
# Libraries.
2626
import os
27+
import re
2728
from datetime import datetime
2829
import re
2930
from bs4 import BeautifulSoup
3031
from collections import OrderedDict
3132
from .. import SEPRTR
3233
from .eda import eda_class
33-
from .log__ import debug_overview
34+
from .log__ import debug_overview, debug_obsessive
3435

3536

3637
__all__ = ['eda_kicad']
@@ -70,11 +71,17 @@ def get_part_groups(in_file):
7071
@return `dict()` of the parts designed. The keys are the componentes references.
7172
'''
7273
# Read-in the schematic XML file to get a tree and get its root.
73-
debug_overview('# Getting from XML \'{}\' KiCad BoM...'.format(
74-
os.path.basename(in_file)))
75-
file_h = open(in_file)
76-
root = BeautifulSoup(file_h, 'xml')
77-
file_h.close()
74+
debug_overview('# Getting from XML \'{}\' KiCad BoM...'.format(os.path.basename(in_file)))
75+
# KiCad uses UTF-8 encoding, some broken Python setups uses CP1252 (see #581)
76+
with open(in_file, 'rb') as f:
77+
xml = f.read()
78+
res = re.search(rb'encoding\s*=\s*"(.*)"', xml)
79+
assert res, "Broken XML, no encoding declared"
80+
encoding = res.group(1).decode()
81+
debug_obsessive('XML encoding: {}'.format(encoding))
82+
xml_s = xml.decode(encoding)
83+
84+
root = BeautifulSoup(xml_s, 'xml')
7885

7986
# Get the general information of the project BoM XML file.
8087
debug_overview('Getting authorship data...')

0 commit comments

Comments
 (0)