mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
DataModel: add support for DictOfType, extend base manifest with ExampelsModel
This commit is contained in:
@ -39,12 +39,20 @@ class DataFieldException(DataModelException):
|
|||||||
self.field.name,
|
self.field.name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self)
|
||||||
|
|
||||||
|
|
||||||
class ListOfType(object):
|
class ListOfType(object):
|
||||||
def __init__(self, type):
|
def __init__(self, type):
|
||||||
self.type = type
|
self.type = type
|
||||||
|
|
||||||
|
|
||||||
|
class DictOfType(object):
|
||||||
|
def __init__(self, type):
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
|
||||||
class DataField(object):
|
class DataField(object):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -92,6 +100,8 @@ class DataField(object):
|
|||||||
return self.type(**value).as_dict()
|
return self.type(**value).as_dict()
|
||||||
if isinstance(self.type, ListOfType):
|
if isinstance(self.type, ListOfType):
|
||||||
return self._validate_list_of_type(self.type.type, value)
|
return self._validate_list_of_type(self.type.type, value)
|
||||||
|
if isinstance(self.type, DictOfType):
|
||||||
|
return self._validate_dict_of_type(self.type.type, value)
|
||||||
if issubclass(self.type, (str, bool)):
|
if issubclass(self.type, (str, bool)):
|
||||||
return getattr(self, "_validate_%s_value" % self.type.__name__)(value)
|
return getattr(self, "_validate_%s_value" % self.type.__name__)(value)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
@ -106,6 +116,12 @@ class DataField(object):
|
|||||||
assert issubclass(list_of_type, DataModel)
|
assert issubclass(list_of_type, DataModel)
|
||||||
return [list_of_type(**v).as_dict() for v in value]
|
return [list_of_type(**v).as_dict() for v in value]
|
||||||
|
|
||||||
|
def _validate_dict_of_type(self, dict_of_type, value):
|
||||||
|
if not isinstance(value, dict):
|
||||||
|
raise ValueError("Value should be a dict")
|
||||||
|
assert issubclass(dict_of_type, DataModel)
|
||||||
|
return {k: dict_of_type(**v).as_dict() for k, v in value.items()}
|
||||||
|
|
||||||
def _validate_str_value(self, value):
|
def _validate_str_value(self, value):
|
||||||
if not isinstance(value, string_types):
|
if not isinstance(value, string_types):
|
||||||
value = str(value)
|
value = str(value)
|
||||||
|
@ -14,7 +14,13 @@
|
|||||||
|
|
||||||
import semantic_version
|
import semantic_version
|
||||||
|
|
||||||
from platformio.datamodel import DataField, DataModel, ListOfType, StrictDataModel
|
from platformio.datamodel import (
|
||||||
|
DataField,
|
||||||
|
DataModel,
|
||||||
|
DictOfType,
|
||||||
|
ListOfType,
|
||||||
|
StrictDataModel,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def validate_semver_field(_, value):
|
def validate_semver_field(_, value):
|
||||||
@ -41,6 +47,11 @@ class ExportModel(DataModel):
|
|||||||
exclude = DataField(type=ListOfType(DataField()))
|
exclude = DataField(type=ListOfType(DataField()))
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleModel(DataModel):
|
||||||
|
base = DataField(required=True)
|
||||||
|
files = DataField(type=ListOfType(DataField()))
|
||||||
|
|
||||||
|
|
||||||
class ManifestModel(DataModel):
|
class ManifestModel(DataModel):
|
||||||
|
|
||||||
# Required fields
|
# Required fields
|
||||||
@ -66,6 +77,7 @@ class ManifestModel(DataModel):
|
|||||||
|
|
||||||
repository = DataField(type=RepositoryModel)
|
repository = DataField(type=RepositoryModel)
|
||||||
export = DataField(type=ExportModel)
|
export = DataField(type=ExportModel)
|
||||||
|
examples = DataField(type=DictOfType(ExampleModel))
|
||||||
|
|
||||||
|
|
||||||
class StrictManifestModel(ManifestModel, StrictDataModel):
|
class StrictManifestModel(ManifestModel, StrictDataModel):
|
||||||
|
@ -169,7 +169,7 @@ sentence=This is Arduino library
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_library_json_valid_model():
|
def test_library_json_model():
|
||||||
contents = """
|
contents = """
|
||||||
{
|
{
|
||||||
"name": "ArduinoJson",
|
"name": "ArduinoJson",
|
||||||
@ -193,7 +193,17 @@ def test_library_json_valid_model():
|
|||||||
],
|
],
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*",
|
"platforms": "*",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"examples": {
|
||||||
|
"JsonConfigFile": {
|
||||||
|
"base": "examples/JsonConfigFile",
|
||||||
|
"files": ["JsonConfigFile.ino"]
|
||||||
|
},
|
||||||
|
"JsonHttpClient": {
|
||||||
|
"base": "examples/JsonHttpClient",
|
||||||
|
"files": ["JsonHttpClient.ino"]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
data = parser.ManifestParserFactory.new(
|
data = parser.ManifestParserFactory.new(
|
||||||
@ -227,11 +237,21 @@ def test_library_json_valid_model():
|
|||||||
"frameworks": ["arduino"],
|
"frameworks": ["arduino"],
|
||||||
"platforms": ["*"],
|
"platforms": ["*"],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"examples": {
|
||||||
|
"JsonConfigFile": {
|
||||||
|
"base": "examples/JsonConfigFile",
|
||||||
|
"files": ["JsonConfigFile.ino"],
|
||||||
|
},
|
||||||
|
"JsonHttpClient": {
|
||||||
|
"base": "examples/JsonHttpClient",
|
||||||
|
"files": ["JsonHttpClient.ino"],
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_library_properties_valid_model():
|
def library_properties_model():
|
||||||
contents = """
|
contents = """
|
||||||
name=U8glib
|
name=U8glib
|
||||||
version=1.19.1
|
version=1.19.1
|
||||||
@ -282,7 +302,7 @@ architectures=avr,sam
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_broken_model():
|
def test_broken_models():
|
||||||
# non-strict mode
|
# non-strict mode
|
||||||
assert len(ManifestModel(name="MyPackage").get_exceptions()) == 4
|
assert len(ManifestModel(name="MyPackage").get_exceptions()) == 4
|
||||||
assert ManifestModel(name="MyPackage", version="broken_version").version is None
|
assert ManifestModel(name="MyPackage", version="broken_version").version is None
|
||||||
|
Reference in New Issue
Block a user