Implement load_json for "util"

This commit is contained in:
Ivan Kravets
2016-04-09 14:15:59 +03:00
parent fe08e1f3f9
commit 669561782b
4 changed files with 19 additions and 17 deletions

View File

@ -20,9 +20,8 @@ from time import time
from lockfile import LockFile from lockfile import LockFile
from platformio import __version__ from platformio import __version__, util
from platformio.exception import InvalidSettingName, InvalidSettingValue from platformio.exception import InvalidSettingName, InvalidSettingValue
from platformio.util import get_home_dir, is_ci
DEFAULT_SETTINGS = { DEFAULT_SETTINGS = {
"check_platformio_interval": { "check_platformio_interval": {
@ -76,7 +75,7 @@ class State(object):
self.path = path self.path = path
self.lock = lock self.lock = lock
if not self.path: if not self.path:
self.path = join(get_home_dir(), "appstate.json") self.path = join(util.get_home_dir(), "appstate.json")
self._state = {} self._state = {}
self._prev_state = {} self._prev_state = {}
self._lockfile = None self._lockfile = None
@ -85,8 +84,7 @@ class State(object):
try: try:
self._lock_state_file() self._lock_state_file()
if isfile(self.path): if isfile(self.path):
with open(self.path, "r") as fp: self._state = util.load_json(self.path)
self._state = json.load(fp)
except ValueError: except ValueError:
self._state = {} self._state = {}
self._prev_state = deepcopy(self._state) self._prev_state = deepcopy(self._state)
@ -149,7 +147,7 @@ def get_setting(name):
if name == "enable_prompts": if name == "enable_prompts":
# disable prompts for Continuous Integration systems # disable prompts for Continuous Integration systems
# and when global "--force" option is set # and when global "--force" option is set
if any([is_ci(), get_session_var("force_option")]): if any([util.is_ci(), get_session_var("force_option")]):
return False return False
_env_name = "PLATFORMIO_SETTING_%s" % name.upper() _env_name = "PLATFORMIO_SETTING_%s" % name.upper()

View File

@ -94,6 +94,9 @@ WARNING! Don't use `sudo` for the rest PlatformIO commands.
else: else:
raise exception.UpgradeError( raise exception.UpgradeError(
"\n".join([str(cmd), r['out'], r['err']])) "\n".join([str(cmd), r['out'], r['err']]))
finally:
if r:
r.close()
def get_latest_version(): def get_latest_version():

View File

@ -51,8 +51,7 @@ class LibraryManager(object):
conf_path = join(self.lib_dir, dirname, self.CONFIG_NAME) conf_path = join(self.lib_dir, dirname, self.CONFIG_NAME)
if not isfile(conf_path): if not isfile(conf_path):
continue continue
with open(conf_path, "r") as f: items[dirname] = util.load_json(conf_path)
items[dirname] = json.load(f)
return items return items
def get_latest_versions(self): def get_latest_versions(self):

View File

@ -124,6 +124,11 @@ def singleton(cls):
return get_instance return get_instance
def load_json(file_path):
with open(file_path, "r") as f:
return json.load(f)
def get_systype(): def get_systype():
data = uname() data = uname()
type_ = data[0].lower() type_ = data[0].lower()
@ -343,21 +348,19 @@ def get_api_result(path, params=None, data=None, skipdns=False):
try: try:
if data: if data:
r = requests.post( r = requests.post(
url + path, params=params, data=data, headers=headers, url + path, params=params, data=data, headers=headers)
timeout=(5, 13)
)
else: else:
r = requests.get( r = requests.get(url + path, params=params, headers=headers)
url + path, params=params, headers=headers, timeout=(5, 13))
result = r.json()
r.raise_for_status() r.raise_for_status()
result = r.json()
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
if result and "errors" in result: if result and "errors" in result:
raise exception.APIRequestError(result['errors'][0]['title']) raise exception.APIRequestError(result['errors'][0]['title'])
else: else:
raise exception.APIRequestError(e) raise exception.APIRequestError(e)
except (requests.exceptions.ConnectionError, except (requests.exceptions.ConnectionError,
requests.exceptions.ConnectTimeout): requests.exceptions.ConnectTimeout,
requests.exceptions.ReadTimeout):
if not skipdns: if not skipdns:
return get_api_result(path, params, data, skipdns=True) return get_api_result(path, params, data, skipdns=True)
raise exception.APIRequestError( raise exception.APIRequestError(
@ -383,8 +386,7 @@ def _lookup_boards():
for json_file in sorted(os.listdir(bdir)): for json_file in sorted(os.listdir(bdir)):
if not json_file.endswith(".json"): if not json_file.endswith(".json"):
continue continue
with open(join(bdir, json_file)) as f: boards.update(load_json(join(bdir, json_file)))
boards.update(json.load(f))
return boards return boards