-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from kosma/handle-yaml-os-errors
- Loading branch information
Showing
2 changed files
with
20 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import click | ||
import yaml | ||
|
||
|
||
def load(path): | ||
with open(path, 'r') as stream: | ||
return yaml.safe_load(stream=stream) | ||
|
||
try: | ||
with open(path, 'r') as stream: | ||
return yaml.safe_load(stream=stream) | ||
except FileNotFoundError: | ||
return None | ||
except OSError as exc: | ||
raise click.ClickException('Failed to read file: %s' % exc) | ||
|
||
def save(path, obj): | ||
with open(path, 'w') as stream: | ||
yaml.safe_dump(obj, stream=stream, default_flow_style=False) | ||
try: | ||
with open(path, 'w') as stream: | ||
yaml.safe_dump(obj, stream=stream, default_flow_style=False) | ||
except OSError as exc: | ||
raise click.ClickException('Failed to write file: %s' % exc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters