mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-29 17:47:14 +02:00
Refactor get_boards method with exception handler
This commit is contained in:
@ -10,9 +10,7 @@ from subprocess import PIPE, Popen
|
||||
|
||||
import requests
|
||||
|
||||
from platformio import __apiurl__, __version__
|
||||
from platformio.exception import (APIRequestError, GetSerialPortsError,
|
||||
NotPlatformProject)
|
||||
from platformio import __apiurl__, __version__, exception
|
||||
|
||||
try:
|
||||
from configparser import ConfigParser
|
||||
@ -33,7 +31,7 @@ def get_home_dir():
|
||||
if (config.has_section("platformio") and
|
||||
config.has_option("platformio", "home_dir")):
|
||||
home_dir = config.get("platformio", "home_dir")
|
||||
except NotPlatformProject:
|
||||
except exception.NotPlatformProject:
|
||||
pass
|
||||
|
||||
if not home_dir:
|
||||
@ -55,7 +53,7 @@ def get_lib_dir():
|
||||
if lib_dir.startswith("~"):
|
||||
lib_dir = expanduser(lib_dir)
|
||||
return abspath(lib_dir)
|
||||
except NotPlatformProject:
|
||||
except exception.NotPlatformProject:
|
||||
pass
|
||||
return join(get_home_dir(), "lib")
|
||||
|
||||
@ -75,7 +73,7 @@ def get_pioenvs_dir():
|
||||
def get_project_config():
|
||||
path = join(get_project_dir(), "platformio.ini")
|
||||
if not isfile(path):
|
||||
raise NotPlatformProject(get_project_dir())
|
||||
raise exception.NotPlatformProject(get_project_dir())
|
||||
cp = ConfigParser()
|
||||
cp.read(path)
|
||||
return cp
|
||||
@ -101,7 +99,7 @@ def get_serialports():
|
||||
elif os_name == "posix":
|
||||
from serial.tools.list_ports_posix import comports
|
||||
else:
|
||||
raise GetSerialPortsError(os_name)
|
||||
raise exception.GetSerialPortsError(os_name)
|
||||
return[{"port": p, "description": d, "hwid": h} for p, d, h in comports()]
|
||||
|
||||
|
||||
@ -127,14 +125,15 @@ def get_api_result(path, params=None, data=None):
|
||||
r.raise_for_status()
|
||||
except requests.exceptions.HTTPError as e:
|
||||
if result and "errors" in result:
|
||||
raise APIRequestError(result['errors'][0]['title'])
|
||||
raise exception.APIRequestError(result['errors'][0]['title'])
|
||||
else:
|
||||
raise APIRequestError(e)
|
||||
raise exception.APIRequestError(e)
|
||||
except requests.exceptions.ConnectionError:
|
||||
raise APIRequestError(
|
||||
raise exception.APIRequestError(
|
||||
"Could not connect to PlatformIO Registry Service")
|
||||
except ValueError:
|
||||
raise APIRequestError("Invalid response: %s" % r.text.encode("utf-8"))
|
||||
raise exception.APIRequestError(
|
||||
"Invalid response: %s" % r.text.encode("utf-8"))
|
||||
finally:
|
||||
if r:
|
||||
r.close()
|
||||
@ -143,15 +142,24 @@ def get_api_result(path, params=None, data=None):
|
||||
|
||||
def get_boards(type_=None):
|
||||
boards = {}
|
||||
bdirs = [join(get_source_dir(), "boards")]
|
||||
if isdir(join(get_home_dir(), "boards")):
|
||||
bdirs.append(join(get_home_dir(), "boards"))
|
||||
try:
|
||||
boards = get_boards._cache
|
||||
except AttributeError:
|
||||
bdirs = [join(get_source_dir(), "boards")]
|
||||
if isdir(join(get_home_dir(), "boards")):
|
||||
bdirs.append(join(get_home_dir(), "boards"))
|
||||
|
||||
for bdir in bdirs:
|
||||
for json_file in listdir(bdir):
|
||||
if not json_file.endswith(".json"):
|
||||
continue
|
||||
with open(join(bdir, json_file)) as f:
|
||||
boards.update(json.load(f))
|
||||
for bdir in bdirs:
|
||||
for json_file in listdir(bdir):
|
||||
if not json_file.endswith(".json"):
|
||||
continue
|
||||
with open(join(bdir, json_file)) as f:
|
||||
boards.update(json.load(f))
|
||||
get_boards._cache = boards
|
||||
|
||||
return boards[type_] if type_ is not None else boards
|
||||
if type_ is None:
|
||||
return boards
|
||||
else:
|
||||
if type_ not in boards:
|
||||
raise exception.UnknownBoard(type_)
|
||||
return boards[type_]
|
||||
|
Reference in New Issue
Block a user