Skip to content

Commit

Permalink
[Core] Reset the configuration file when it is empty (Azure#7063)
Browse files Browse the repository at this point in the history
  • Loading branch information
troydai authored Aug 15, 2018
1 parent 61076ad commit f0221de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/azure-cli-core/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Release History
===============

2.0.45
++++++
* Minor fixes
* Fix issue of loading empty configuration file.

2.0.44
++++++
Expand Down
18 changes: 14 additions & 4 deletions src/azure-cli-core/azure/cli/core/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
import json
import os
import time

try:
import collections.abc as collections
except ImportError:
import collections


from codecs import open as codecs_open

from knack.log import get_logger

try:
t_JSONDecodeError = json.JSONDecodeError
except AttributeError: # in Python 2.7
t_JSONDecodeError = ValueError


class Session(collections.MutableMapping):
'''A simple dict-like class that is backed by a JSON file.
"""
A simple dict-like class that is backed by a JSON file.
All direct modifications will save the file. Indirect modifications should
be followed by a call to `save_with_retry` or `save`.
'''
"""

def __init__(self, encoding=None):
super(Session, self).__init__()
Expand All @@ -38,7 +46,9 @@ def load(self, filename, max_age=0):
self.save()
with codecs_open(self.filename, 'r', encoding=self._encoding) as f:
self.data = json.load(f)
except (OSError, IOError):
except (OSError, IOError, t_JSONDecodeError):
get_logger(__name__).warning("Fail to load or parse file %s. It is overridden by default settings.",
self.filename)
self.save()

def save(self):
Expand Down

0 comments on commit f0221de

Please sign in to comment.