Skip to content

Commit

Permalink
Make ManifestSchema compatible with marshmallow >= 3
Browse files Browse the repository at this point in the history
Instead of suppressing the exception, checking for an error and raising
a PlatformIOException, directly raise the PlatformIOException from
handle_error().

Add a small wrapper for Schema.load() to cover the the return value
difference ((data, error) tuple vs data only).

For more information, see
https://marshmallow.readthedocs.io/en/stable/upgrading.html
  • Loading branch information
StefanBruens committed Dec 2, 2019
1 parent 4f1ccfe commit eb42f2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
9 changes: 3 additions & 6 deletions platformio/commands/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from platformio.compat import dump_json_to_unicode
from platformio.managers.lib import LibraryManager, get_builtin_libs, is_builtin_lib
from platformio.package.manifest.parser import ManifestParserFactory
from platformio.package.manifest.schema import ManifestSchema, ManifestValidationError
from platformio.package.manifest.schema import ManifestSchema
from platformio.proc import is_ci
from platformio.project.config import ProjectConfig
from platformio.project.helpers import get_project_dir, is_platformio_project
Expand Down Expand Up @@ -495,11 +495,8 @@ def lib_register(config_url):
raise exception.InvalidLibConfURL(config_url)

# Validate manifest
data, error = ManifestSchema(strict=False).load(
ManifestParserFactory.new_from_url(config_url).as_dict()
)
if error:
raise ManifestValidationError(error, data)
manifest = ManifestParserFactory.new_from_url(config_url).as_dict()
data = ManifestSchema().load_manifest(manifest)

result = util.get_api_result("/lib/register", data=dict(config_url=config_url))
if "message" in result and result["message"]:
Expand Down
21 changes: 16 additions & 5 deletions platformio/package/manifest/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import requests
import semantic_version
from marshmallow import Schema, ValidationError, fields, validate, validates
from marshmallow import __version_info__ as marshmallow_version

from platformio.package.exception import ManifestValidationError
from platformio.util import memoized


class StrictSchema(Schema):
def handle_error(self, error, data):
def handle_error(self, error, data, **kwargs):
# skip broken records
if self.many:
error.data = [
Expand All @@ -33,8 +34,10 @@ def handle_error(self, error, data):


class StrictListField(fields.List):
def _deserialize(self, value, attr, data):
def _deserialize(self, value, attr, data, **kwargs):
try:
if marshmallow_version >= (3, ):
return super(StrictListField, self)._deserialize(value, attr, data, **kwargs)
return super(StrictListField, self)._deserialize(value, attr, data)
except ValidationError as exc:
if exc.data:
Expand Down Expand Up @@ -142,9 +145,17 @@ class ManifestSchema(Schema):
)
)

def handle_error(self, error, data):
if self.strict:
raise ManifestValidationError(error, data)
# Wrapper to encapsulate differences between marshmallow 2 and 3
# load() method
def load_manifest(self, manifest):
if marshmallow_version >= (3, ):
data = self.load(manifest)
else:
data, _ = self.load(manifest)
return data

def handle_error(self, error, data, **kwargs):
raise ManifestValidationError(error, data)

@validates("version")
def validate_version(self, value): # pylint: disable=no-self-use
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"semantic_version>=2.8.1,<3",
"tabulate>=0.8.3,<1",
"pyelftools>=0.25,<1",
"marshmallow>=2.20.5,<3"
"marshmallow>=2.20.5"
]

setup(
Expand Down

0 comments on commit eb42f2c

Please sign in to comment.