Skip to content

Commit 4b76b26

Browse files
committed
.example.env command
fixed issue with windows directories removed debug echo statements Removed old try accept that is no longer needed Should be the end of my after the fact website commits Add missing trailing newline when adding new value Sometimes, the source file doesn't have a trailing newline. If we add a new binding in such a case, we need to add a newline before the new binding. Release version 0.19.2 updated to remove file error with broken file fixed minor bug tests and minor changes
1 parent fc138ce commit 4b76b26

File tree

7 files changed

+49
-5
lines changed

7 files changed

+49
-5
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this
66
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.19.2] - 2021-11-11
9+
10+
### Fixed
11+
12+
- In `set_key`, add missing newline character before new entry if necessary. (#361 by
13+
[@bbc2])
14+
815
## [0.19.1] - 2021-08-09
916

1017
### Added
@@ -292,7 +299,8 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
292299
[@yannham]: https://github.com/yannham
293300
[@zueve]: https://github.com/zueve
294301

295-
[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...HEAD
302+
[Unreleased]: https://github.com/theskumar/python-dotenv/compare/v0.19.2...HEAD
303+
[0.19.2]: https://github.com/theskumar/python-dotenv/compare/v0.19.1...v0.19.2
296304
[0.19.1]: https://github.com/theskumar/python-dotenv/compare/v0.19.0...v0.19.1
297305
[0.19.0]: https://github.com/theskumar/python-dotenv/compare/v0.18.0...v0.19.0
298306
[0.18.0]: https://github.com/theskumar/python-dotenv/compare/v0.17.1...v0.18.0

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.19.1
2+
current_version = 0.19.2
33
commit = True
44
tag = True
55

src/dotenv/cli.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def get(ctx: click.Context, key: Any) -> None:
8282
else:
8383
exit(1)
8484

85-
8685
@cli.command()
8786
@click.pass_context
8887
@click.argument('key', required=True)
@@ -125,6 +124,32 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None:
125124
ret = run_command(commandline, dotenv_as_dict)
126125
exit(ret)
127126

127+
@cli.command()
128+
@click.pass_context
129+
def example_file(ctx: click.Context) -> None:
130+
'''Generates a .example.env file without values.'''
131+
filedir = ctx.obj['FILE'].replace("\\", "/")
132+
if not os.path.isfile(filedir):
133+
raise click.BadParameter(
134+
'Path "%s" does not exist.' % (filedir),
135+
ctx=ctx
136+
)
137+
newFileList = []
138+
with open(filedir,'r') as file:
139+
for line in file:
140+
line = line.strip()
141+
if line[0] != "#":
142+
line = line.split("=", 1)[0] + "="
143+
newFileList.append(line + "\n")
144+
145+
while newFileList[-1] == "\n":
146+
newFileList.pop(-1)
147+
148+
newFileName = f"{os.path.basename(file.name).split('.', 1)[0]}.example.env"
149+
with open(newFileName, "w") as newFile:
150+
for line in newFileList:
151+
newFile.write(line)
152+
click.echo(f"{newFileName} exported.")
128153

129154
def run_command(command: List[str], env: Dict[str, str]) -> int:
130155
"""Run command in sub process.
@@ -161,4 +186,4 @@ def run_command(command: List[str], env: Dict[str, str]) -> int:
161186

162187

163188
if __name__ == "__main__":
164-
cli()
189+
cli()

src/dotenv/main.py

+4
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,17 @@ def set_key(
167167

168168
with rewrite(dotenv_path) as (source, dest):
169169
replaced = False
170+
missing_newline = False
170171
for mapping in with_warn_for_invalid_lines(parse_stream(source)):
171172
if mapping.key == key_to_set:
172173
dest.write(line_out)
173174
replaced = True
174175
else:
175176
dest.write(mapping.original.string)
177+
missing_newline = not mapping.original.string.endswith("\n")
176178
if not replaced:
179+
if missing_newline:
180+
dest.write("\n")
177181
dest.write(line_out)
178182

179183
return True, key_to_set, value_to_set

src/dotenv/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.19.1"
1+
__version__ = "0.19.2"

tests/test_cli.py

+6
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,9 @@ def test_run_with_version(cli):
205205

206206
assert result.exit_code == 0
207207
assert result.output.strip().endswith(__version__)
208+
209+
def test_example_file_non_existent_file(cli):
210+
result = cli.invoke(dotenv_cli, ['--file', 'nx_file', 'example_file'])
211+
212+
assert result.exit_code == 2, result.output
213+
assert "does not exist" in result.output

tests/test_main.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_set_key_no_file(tmp_path):
3737
("a=b\nc=d", "a", "e", (True, "a", "e"), "a='e'\nc=d"),
3838
("a=b\nc=d\ne=f", "c", "g", (True, "c", "g"), "a=b\nc='g'\ne=f"),
3939
("a=b\n", "c", "d", (True, "c", "d"), "a=b\nc='d'\n"),
40+
("a=b", "c", "d", (True, "c", "d"), "a=b\nc='d'\n"),
4041
],
4142
)
4243
def test_set_key(dotenv_file, before, key, value, expected, after):

0 commit comments

Comments
 (0)