Skip to content

Commit 6cd6585

Browse files
committed
Update dotenv.py
1 parent 95fa239 commit 6cd6585

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

scripts/utils/dotenv.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,31 @@
2222

2323
class DotEnv:
2424
def __read_dotenv(self, path: str | Path):
25-
with open(path, 'r') as f:
26-
for line in f:
27-
line = line.strip()
28-
if line == '' or line.startswith('#'):
29-
continue
30-
31-
key, value = line.strip().split('=', 1)
32-
33-
self.dotenv_vars[key] = value
25+
text_data = ''
26+
27+
with open(path, 'rb') as f: # Open the file in binary mode first to detect BOM
28+
raw_data = f.read()
29+
30+
# Check for BOM and strip it if present
31+
if raw_data.startswith(b'\xef\xbb\xbf'): # UTF-8 BOM
32+
text_data = raw_data[3:].decode('utf-8')
33+
elif raw_data.startswith(b'\xff\xfe'): # UTF-16 LE BOM
34+
text_data = raw_data[2:].decode('utf-16le')
35+
elif raw_data.startswith(b'\xfe\xff'): # UTF-16 BE BOM
36+
text_data = raw_data[2:].decode('utf-16be')
37+
38+
# Now process the text data
39+
for line in text_data.splitlines():
40+
line = line.strip()
41+
if line == '' or line.startswith('#'):
42+
continue
43+
44+
split = line.strip().split('=', 1)
45+
if len(split) != 2:
46+
print('Failed to parse: ' + line)
47+
continue
48+
49+
self.dotenv_vars[line[0]] = line[1]
3450

3551
def __init__(self, path: str | Path, environment: str):
3652
self.dotenv_vars: dict[str, str] = {}

0 commit comments

Comments
 (0)