Handle ConfigParser erros

This commit is contained in:
Ivan Kravets
2018-06-20 16:31:03 +03:00
parent 3c6f57ac5c
commit ee69c13b2d
2 changed files with 18 additions and 6 deletions

View File

@ -192,6 +192,11 @@ class InvalidLibConfURL(PlatformioException):
MESSAGE = "Invalid library config URL '{0}'" MESSAGE = "Invalid library config URL '{0}'"
class InvalidProjectConf(PlatformioException):
MESSAGE = "Invalid `platformio.ini`, project configuration file: '{0}'"
class BuildScriptNotFound(PlatformioException): class BuildScriptNotFound(PlatformioException):
MESSAGE = "Invalid path '{0}' to build script" MESSAGE = "Invalid path '{0}' to build script"

View File

@ -36,23 +36,27 @@ from platformio import __apiurl__, __version__, exception
# pylint: disable=wrong-import-order, too-many-ancestors # pylint: disable=wrong-import-order, too-many-ancestors
try: try:
from configparser import ConfigParser import configparser as ConfigParser
except ImportError: except ImportError:
from ConfigParser import ConfigParser import ConfigParser as ConfigParser
class ProjectConfig(ConfigParser): class ProjectConfig(ConfigParser.ConfigParser):
VARTPL_RE = re.compile(r"\$\{([^\.\}]+)\.([^\}]+)\}") VARTPL_RE = re.compile(r"\$\{([^\.\}]+)\.([^\}]+)\}")
def items(self, section, **_): # pylint: disable=arguments-differ def items(self, section, **_): # pylint: disable=arguments-differ
items = [] items = []
for option in ConfigParser.options(self, section): for option in ConfigParser.ConfigParser.options(self, section):
items.append((option, self.get(section, option))) items.append((option, self.get(section, option)))
return items return items
def get(self, section, option, **kwargs): def get(self, section, option, **kwargs):
value = ConfigParser.get(self, section, option, **kwargs) try:
value = ConfigParser.ConfigParser.get(self, section, option,
**kwargs)
except ConfigParser.Error as e:
raise exception.InvalidProjectConf(str(e))
if "${" not in value or "}" not in value: if "${" not in value or "}" not in value:
return value return value
return self.VARTPL_RE.sub(self._re_sub_handler, value) return self.VARTPL_RE.sub(self._re_sub_handler, value)
@ -331,7 +335,10 @@ def load_project_config(path=None):
raise exception.NotPlatformIOProject( raise exception.NotPlatformIOProject(
dirname(path) if path.endswith("platformio.ini") else path) dirname(path) if path.endswith("platformio.ini") else path)
cp = ProjectConfig() cp = ProjectConfig()
cp.read(path) try:
cp.read(path)
except ConfigParser.Error as e:
raise exception.InvalidProjectConf(str(e))
return cp return cp