Allow to build a manifest parser from directory

This commit is contained in:
Ivan Kravets
2019-10-01 00:11:31 +03:00
parent 703912fdc9
commit af1a0f3587
3 changed files with 37 additions and 16 deletions

View File

@ -26,7 +26,7 @@ from platformio.commands import PlatformioCLI
from platformio.compat import dump_json_to_unicode
from platformio.managers.lib import LibraryManager, get_builtin_libs, is_builtin_lib
from platformio.package.manifest.model import StrictManifestModel
from platformio.package.manifest.parser import ManifestFactory
from platformio.package.manifest.parser import ManifestParserFactory
from platformio.proc import is_ci
from platformio.project.config import ProjectConfig
from platformio.project.helpers import get_project_dir, is_platformio_project
@ -495,7 +495,7 @@ def lib_register(config_url):
raise exception.InvalidLibConfURL(config_url)
# Validate manifest
StrictManifestModel(**ManifestFactory.new_from_url(config_url).as_dict())
StrictManifestModel(**ManifestParserFactory.new_from_url(config_url).as_dict())
result = util.get_api_result("/lib/register", data=dict(config_url=config_url))
if "message" in result and result["message"]:

View File

@ -56,33 +56,52 @@ class ManifestFileType(object):
return ManifestFileType.LIBRARY_JSON
class ManifestFactory(object):
class ManifestParserFactory(object):
@staticmethod
def type_to_clsname(type_):
type_ = type_.replace(".", " ")
type_ = type_.title()
return "%sManifestParser" % type_.replace(" ", "")
def type_to_clsname(t):
t = t.replace(".", " ")
t = t.title()
return "%sManifestParser" % t.replace(" ", "")
@staticmethod
def new_from_file(path):
def new_from_file(path, remote_url=False):
if not path or not os.path.isfile(path):
raise ManifestException("Manifest file does not exist %s" % path)
for type_ in get_class_attributes(ManifestFileType).values():
if path.endswith(type_):
return ManifestFactory.new(get_file_contents(path), type_)
for t in get_class_attributes(ManifestFileType).values():
if path.endswith(t):
return ManifestParserFactory.new(get_file_contents(path), t, remote_url)
raise ManifestException("Unknown manifest file type %s" % path)
@staticmethod
def new_from_dir(path, remote_url=None):
assert os.path.isdir(path), "Invalid directory %s" % path
file_order = [
ManifestFileType.PLATFORM_JSON,
ManifestFileType.LIBRARY_JSON,
ManifestFileType.LIBRARY_PROPERTIES,
ManifestFileType.MODULE_JSON,
ManifestFileType.PACKAGE_JSON,
]
for t in file_order:
if not os.path.isfile(os.path.join(path, t)):
continue
return ManifestParserFactory.new(
get_file_contents(os.path.join(path, t)), t, remote_url
)
raise ManifestException("Unknown manifest file type in %s directory" % path)
@staticmethod
def new_from_url(remote_url):
r = requests.get(remote_url)
r.raise_for_status()
return ManifestFactory.new(
return ManifestParserFactory.new(
r.text, ManifestFileType.from_uri(remote_url), remote_url
)
@staticmethod
def new(contents, type_, remote_url=None):
clsname = ManifestFactory.type_to_clsname(type_)
def new(contents, type, remote_url=None):
# pylint: disable=redefined-builtin
clsname = ManifestParserFactory.type_to_clsname(type)
if clsname not in globals():
raise ManifestException("Unknown manifest file type %s" % clsname)
return globals()[clsname](contents, remote_url)

View File

@ -176,7 +176,9 @@ def test_library_json_valid_model():
"license": "MIT"
}
"""
data = parser.ManifestFactory.new(contents, parser.ManifestFileType.LIBRARY_JSON)
data = parser.ManifestParserFactory.new(
contents, parser.ManifestFileType.LIBRARY_JSON
)
model = ManifestModel(**data.as_dict())
assert sorted(model.as_dict().items()) == sorted(
{
@ -221,7 +223,7 @@ category=Display
url=https://github.com/olikraus/u8glib
architectures=avr,sam
"""
data = parser.ManifestFactory.new(
data = parser.ManifestParserFactory.new(
contents, parser.ManifestFileType.LIBRARY_PROPERTIES
)
model = ManifestModel(**data.as_dict())