Commit d33366c
Add support for newlines, backslashes, trailing comments and unquoted UTF-8 (theskumar#148)
* Fix deprecation warning for POSIX variable regex
This was also caught by Flake8 as:
./dotenv/main.py:19:2: W605 invalid escape sequence '\$'
./dotenv/main.py:19:4: W605 invalid escape sequence '\{'
./dotenv/main.py:19:8: W605 invalid escape sequence '\}'
./dotenv/main.py:19:12: W605 invalid escape sequence '\}'
* Turn get_stream into a context manager
This avoids the use of the `is_file` class variable by abstracting away
the difference between `StringIO` and a file stream.
* Deduplicate parsing code and abstract away lines
Parsing .env files is a critical part of this package. To make it
easier to change it and test it, it is important that it is done in only
one place.
Also, code that uses the parser now doesn't depend on the fact that each
key-value binding spans exactly one line. This will make it easier to
handle multiline bindings in the future.
* Parse newline, UTF-8, trailing comment, backslash
This adds support for:
* multiline values (i.e. containing newlines or escaped \n), fixes theskumar#89
* backslashes in values, fixes theskumar#112
* trailing comments, fixes theskumar#141
* UTF-8 in unquoted values, fixes theskumar#147
Parsing is no longer line-based. That's why `parse_line` was replaced
by `parse_binding`. Thanks to the previous commit, users of
`parse_stream` don't have to deal with this change.
This supersedes a previous pull-request, theskumar#142, which would add support for
multiline values in `Dotenv.parse` but not in the CLI (`dotenv get` and `dotenv
set`).
The key-value binding regular expression was inspired by
https://github.com/bkeepers/dotenv/blob/d749366b6009126b115fb7b63e0509566365859a/lib/dotenv/parser.rb#L14-L30
Parsing of escapes was fixed thanks to
https://stackoverflow.com/questions/4020539/process-escape-sequences-in-a-string-in-python/24519338#245193381 parent 3b7e60e commit d33366c
3 files changed
+240
-97
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | | - | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | | - | |
17 | | - | |
| 18 | + | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
19 | 25 | | |
20 | | - | |
21 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
22 | 29 | | |
| 30 | + | |
| 31 | + | |
23 | 32 | | |
24 | | - | |
25 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
26 | 38 | | |
27 | | - | |
28 | | - | |
29 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
30 | 46 | | |
31 | | - | |
| 47 | + | |
32 | 48 | | |
33 | | - | |
34 | | - | |
35 | 49 | | |
36 | | - | |
37 | | - | |
| 50 | + | |
38 | 51 | | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | 52 | | |
45 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
46 | 89 | | |
47 | 90 | | |
48 | 91 | | |
| |||
52 | 95 | | |
53 | 96 | | |
54 | 97 | | |
| 98 | + | |
55 | 99 | | |
56 | | - | |
57 | 100 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
68 | 109 | | |
69 | 110 | | |
70 | 111 | | |
| |||
76 | 117 | | |
77 | 118 | | |
78 | 119 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
90 | 124 | | |
91 | 125 | | |
92 | 126 | | |
| |||
126 | 160 | | |
127 | 161 | | |
128 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
129 | 177 | | |
130 | 178 | | |
131 | 179 | | |
| |||
141 | 189 | | |
142 | 190 | | |
143 | 191 | | |
144 | | - | |
| 192 | + | |
145 | 193 | | |
146 | 194 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
158 | 205 | | |
159 | 206 | | |
160 | 207 | | |
| |||
166 | 213 | | |
167 | 214 | | |
168 | 215 | | |
169 | | - | |
170 | | - | |
171 | 216 | | |
172 | 217 | | |
173 | 218 | | |
174 | 219 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
181 | 227 | | |
182 | 228 | | |
183 | 229 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
5 | 8 | | |
6 | | - | |
7 | 9 | | |
8 | | - | |
9 | | - | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
41 | 58 | | |
42 | 59 | | |
43 | 60 | | |
| |||
59 | 76 | | |
60 | 77 | | |
61 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
62 | 86 | | |
63 | 87 | | |
64 | 88 | | |
| |||
95 | 119 | | |
96 | 120 | | |
97 | 121 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
108 | 138 | | |
109 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
110 | 157 | | |
111 | 158 | | |
112 | 159 | | |
| |||
180 | 227 | | |
181 | 228 | | |
182 | 229 | | |
183 | | - | |
| 230 | + | |
184 | 231 | | |
185 | 232 | | |
186 | 233 | | |
| |||
194 | 241 | | |
195 | 242 | | |
196 | 243 | | |
197 | | - | |
| 244 | + | |
198 | 245 | | |
199 | 246 | | |
200 | | - | |
| 247 | + | |
201 | 248 | | |
202 | 249 | | |
203 | 250 | | |
| |||
0 commit comments