Skip to content

Commit 3e616a8

Browse files
authored
Merge pull request #106 from defanator/allow-parse-non-unicode-configs
Allow parsing non-Unicode nginx configurations
2 parents 56aa932 + 21220b0 commit 3e616a8

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

crossplane/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _balance_braces(tokens, filename=None):
145145

146146
def lex(filename):
147147
"""Generates tokens from an nginx config file"""
148-
with io.open(filename, mode='r', encoding='utf-8') as f:
148+
with io.open(filename, mode='r', encoding='utf-8', errors='replace') as f:
149149
it = _lex_file_object(f)
150150
it = _balance_braces(it, filename)
151151
for token, line, quoted in it:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
http {
2+
server {
3+
location /city {
4+
# Mölln
5+
return 200 "Mölln\n";
6+
}
7+
}
8+
}

tests/test_parse.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,3 +975,60 @@ def test_comments_between_args():
975975
}
976976
]
977977
}
978+
979+
def test_non_unicode():
980+
dirname = os.path.join(here, 'configs', 'non-unicode')
981+
config = os.path.join(dirname, 'nginx.conf')
982+
983+
payload = crossplane.parse(config, comments=True)
984+
985+
assert payload == {
986+
"errors": [],
987+
"status": "ok",
988+
"config": [
989+
{
990+
"status": "ok",
991+
"errors": [],
992+
"file": os.path.join(dirname, 'nginx.conf'),
993+
"parsed": [
994+
{
995+
"directive": "http",
996+
"line": 1,
997+
"args": [],
998+
"block": [
999+
{
1000+
"directive": "server",
1001+
"line": 2,
1002+
"args": [],
1003+
"block": [
1004+
{
1005+
"directive": "location",
1006+
"line": 3,
1007+
"args": [
1008+
"/city"
1009+
],
1010+
"block": [
1011+
{
1012+
"directive": "#",
1013+
"line": 4,
1014+
"args": [],
1015+
"comment": u" M\ufffdlln"
1016+
},
1017+
{
1018+
"directive": "return",
1019+
"line": 5,
1020+
"args": [
1021+
"200",
1022+
u"M\ufffdlln\\n"
1023+
]
1024+
}
1025+
]
1026+
}
1027+
]
1028+
}
1029+
]
1030+
}
1031+
]
1032+
}
1033+
]
1034+
}

0 commit comments

Comments
 (0)